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