6 #include "jsfunctions.h"
7 #include "networkaccess.h"
15 #define STRINGIFY(x) STR(x)
18 NetworkAccess* http();
21 YT3 &YT3::instance() {
22 static YT3 *i = new YT3();
27 QByteArray customApiKey = qgetenv("GOOGLE_API_KEY");
28 if (!customApiKey.isEmpty()) {
29 keys << QString::fromUtf8(customApiKey);
30 qDebug() << "API key from environment" << keys;
35 if (settings.contains("googleApiKey")) {
36 keys << settings.value("googleApiKey").toString();
37 qDebug() << "API key from settings" << keys;
41 #ifdef APP_GOOGLE_API_KEY
43 keys << STRINGIFY(APP_GOOGLE_API_KEY);
44 qDebug() << "built-in API key" << keys;
50 keys << Extra::apiKeys();
54 qWarning() << "No available API keys";
56 key = keys.takeFirst();
61 const QString &YT3::baseUrl() {
62 static const QString base = "https://www.googleapis.com/youtube/v3/";
66 void YT3::testApiKey() {
67 QUrl url = method("videos");
68 #if QT_VERSION >= 0x050000
73 url.addQueryItem("part", "id");
74 url.addQueryItem("chart", "mostPopular");
75 url.addQueryItem("maxResults", "1");
76 #if QT_VERSION >= 0x050000
80 QObject *reply = The::http()->get(url);
81 connect(reply, SIGNAL(finished(QNetworkReply*)), SLOT(testResponse(QNetworkReply*)));
84 void YT3::addApiKey(QUrl &url) {
86 qDebug() << __PRETTY_FUNCTION__ << "empty key";
89 #if QT_VERSION >= 0x050000
94 url.addQueryItem("key", key);
95 #if QT_VERSION >= 0x050000
101 QUrl YT3::method(const QString &name) {
102 QUrl url(baseUrl() + name);
107 void YT3::testResponse(QNetworkReply *reply) {
108 int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
110 if (keys.isEmpty()) {
111 qWarning() << "Fatal error: No working API keys!";
114 key = keys.takeFirst();
117 qDebug() << "Using key" << key;