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