]> 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;
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 }
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()) keys << Extra::apiKeys();
59 #endif
60
61     if (keys.isEmpty()) {
62         qWarning() << "No available API keys";
63 #ifdef APP_LINUX_NO
64         QMetaObject::invokeMethod(MainWindow::instance(), "missingKeyWarning",
65                                   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::stealthAndNotCached().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 const VideoDefinition &YT3::maxVideoDefinition() {
103     const QString name = QSettings().value("definition", "720p").toString();
104     const VideoDefinition &definition = VideoDefinition::forName(name);
105     return definition;
106 }
107
108 void YT3::setMaxVideoDefinition(const QString &name) {
109     QSettings settings;
110     settings.setValue("definition", name);
111     emit maxVideoDefinitionChanged(name);
112 }
113
114 void YT3::testResponse(const HttpReply &reply) {
115     int status = reply.statusCode();
116     if (status != 200) {
117         if (keys.isEmpty()) {
118             qWarning() << "Fatal error: No working API keys!";
119             return;
120         }
121         key = keys.takeFirst();
122         testApiKey();
123     } else {
124         qDebug() << "Using key" << key;
125     }
126 }