]> git.sur5r.net Git - minitube/blob - src/yt3.cpp
3703a8cf2b127ce70b464785d8c8b3d50b82083c
[minitube] / src / yt3.cpp
1 #include "yt3.h"
2
3 #include <algorithm>
4 #include <ctime>
5
6 #include "jsfunctions.h"
7 #include "http.h"
8 #include "httputils.h"
9 #include "constants.h"
10 #include "mainwindow.h"
11
12 #ifdef APP_EXTRA
13 #include "extra.h"
14 #endif
15
16 #define STR(x) #x
17 #define STRINGIFY(x) STR(x)
18
19 YT3 &YT3::instance() {
20     static YT3 *i = new YT3();
21     return *i;
22 }
23
24 const QString &YT3::baseUrl() {
25     static const QString base = "https://www.googleapis.com/youtube/v3/";
26     return base;
27 }
28
29 YT3::YT3() {
30     initApiKeys();
31 }
32
33 void YT3::initApiKeys() {
34     keys.clear();
35
36     QByteArray customApiKey = qgetenv("GOOGLE_API_KEY");
37     if (!customApiKey.isEmpty()) {
38         keys << QString::fromUtf8(customApiKey);
39         qDebug() << "API key from environment" << keys;
40     }
41
42     if (keys.isEmpty()) {
43         QSettings settings;
44         if (settings.contains("googleApiKey")) {
45             keys << settings.value("googleApiKey").toString();
46             qDebug() << "API key from settings" << keys;
47         }
48     }
49
50 #ifdef APP_GOOGLE_API_KEY
51     if (keys.isEmpty()) {
52         keys << STRINGIFY(APP_GOOGLE_API_KEY);
53         qDebug() << "built-in API key" << keys;
54     }
55 #endif
56
57 #ifdef APP_EXTRA
58     if (keys.isEmpty())
59         keys << Extra::apiKeys();
60 #endif
61
62     if (keys.isEmpty()) {
63         qWarning() << "No available API keys";
64 #ifdef APP_LINUX
65         QMetaObject::invokeMethod(MainWindow::instance(), "missingKeyWarning", Qt::QueuedConnection);
66 #endif
67     } else {
68         key = keys.takeFirst();
69         if (!keys.isEmpty()) testApiKey();
70     }
71 }
72
73 void YT3::testApiKey() {
74     QUrl url = method("videos");
75     QUrlQuery q(url);
76     q.addQueryItem("part", "id");
77     q.addQueryItem("chart", "mostPopular");
78     q.addQueryItem("maxResults", "1");
79     url.setQuery(q);
80     QObject *reply = HttpUtils::yt().get(url);
81     connect(reply, SIGNAL(finished(HttpReply)), SLOT(testResponse(HttpReply)));
82 }
83
84 void YT3::addApiKey(QUrl &url) {
85     if (key.isEmpty()) {
86         qDebug() << __PRETTY_FUNCTION__ << "empty key";
87         initApiKeys();
88         return;
89     }
90
91     QUrlQuery q(url);
92     q.addQueryItem("key", key);
93     url.setQuery(q);
94 }
95
96 QUrl YT3::method(const QString &name) {
97     QUrl url(baseUrl() + name);
98     addApiKey(url);
99     return url;
100 }
101
102 void YT3::testResponse(const HttpReply &reply) {
103     int status = reply.statusCode();
104     if (status != 200) {
105         if (keys.isEmpty()) {
106             qWarning() << "Fatal error: No working API keys!";
107             return;
108         }
109         key = keys.takeFirst();
110         testApiKey();
111     } else {
112         qDebug() << "Using key" << key;
113     }
114 }