6 #include "jsfunctions.h"
10 #include "mainwindow.h"
17 #define STRINGIFY(x) STR(x)
19 YT3 &YT3::instance() {
20 static YT3 *i = new YT3();
24 const QString &YT3::baseUrl() {
25 static const QString base = "https://www.googleapis.com/youtube/v3/";
33 void YT3::initApiKeys() {
36 QByteArray customApiKey = qgetenv("GOOGLE_API_KEY");
37 if (!customApiKey.isEmpty()) {
38 keys << QString::fromUtf8(customApiKey);
39 qDebug() << "API key from environment" << keys;
44 if (settings.contains("googleApiKey")) {
45 keys << settings.value("googleApiKey").toString();
46 qDebug() << "API key from settings" << keys;
50 #ifdef APP_GOOGLE_API_KEY
52 keys << STRINGIFY(APP_GOOGLE_API_KEY);
53 qDebug() << "built-in API key" << keys;
59 keys << Extra::apiKeys();
63 qWarning() << "No available API keys";
65 QMetaObject::invokeMethod(MainWindow::instance(), "missingKeyWarning", Qt::QueuedConnection);
68 key = keys.takeFirst();
69 if (!keys.isEmpty()) testApiKey();
73 void YT3::testApiKey() {
74 QUrl url = method("videos");
76 q.addQueryItem("part", "id");
77 q.addQueryItem("chart", "mostPopular");
78 q.addQueryItem("maxResults", "1");
80 QObject *reply = HttpUtils::yt().get(url);
81 connect(reply, SIGNAL(finished(HttpReply)), SLOT(testResponse(HttpReply)));
84 void YT3::addApiKey(QUrl &url) {
86 qDebug() << __PRETTY_FUNCTION__ << "empty key";
92 q.addQueryItem("key", key);
96 QUrl YT3::method(const QString &name) {
97 QUrl url(baseUrl() + name);
102 void YT3::testResponse(const HttpReply &reply) {
103 int status = reply.statusCode();
105 if (keys.isEmpty()) {
106 qWarning() << "Fatal error: No working API keys!";
109 key = keys.takeFirst();
112 qDebug() << "Using key" << key;