]> git.sur5r.net Git - minitube/blob - src/jsfunctions.cpp
Fix updating QUrl query parameters with Qt5.
[minitube] / src / jsfunctions.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "jsfunctions.h"
22 #include "networkaccess.h"
23 #include <QDesktopServices>
24 #include "constants.h"
25 #include "compatibility/qurlqueryhelper.h"
26
27 namespace The {
28 NetworkAccess* http();
29 }
30
31 JsFunctions* JsFunctions::instance() {
32     static JsFunctions *i = new JsFunctions(QLatin1String(Constants::WEBSITE) + "-ws/functions.js");
33     return i;
34 }
35
36 JsFunctions::JsFunctions(const QString &url, QObject *parent) : QObject(parent), url(url), engine(0) {
37     QFile file(jsPath());
38     if (file.exists()) {
39         if (file.open(QIODevice::ReadOnly | QIODevice::Text))
40             parseJs(QString::fromUtf8(file.readAll()));
41         else
42             qWarning() << "Cannot open" << file.errorString() << file.fileName();
43         QFileInfo info(file);
44         bool stale = info.size() == 0 || info.lastModified().toTime_t() < QDateTime::currentDateTime().toTime_t() - 1800;
45         if (stale) loadJs();
46     } else {
47         QFile resFile(QLatin1String(":/") + jsFilename());
48         resFile.open(QIODevice::ReadOnly | QIODevice::Text);
49         parseJs(QString::fromUtf8(resFile.readAll()));
50         loadJs();
51     }
52 }
53
54 void JsFunctions::parseJs(const QString &js) {
55     if (js.isEmpty()) return;
56     // qDebug() << "Parsing" << js;
57     if (engine) delete engine;
58     engine = new QScriptEngine(this);
59     engine->evaluate(js);
60     emit ready();
61 }
62
63 QString JsFunctions::jsFilename() {
64     return QFileInfo(url).fileName();
65 }
66
67 QString JsFunctions::jsPath() {
68     return QString(
69             #if QT_VERSION >= 0x050000
70                 QStandardPaths::writableLocation(QStandardPaths::DataLocation)
71             #else
72                 QDesktopServices::storageLocation(QDesktopServices::DataLocation)
73             #endif
74                 + "/" + jsFilename());
75 }
76
77 void JsFunctions::loadJs() {
78     QUrl url(this->url);
79     {
80         QUrlQueryHelper urlHelper(url);
81         urlHelper.addQueryItem("v", Constants::VERSION);
82     }
83
84     NetworkReply* reply = The::http()->get(url);
85     connect(reply, SIGNAL(data(QByteArray)), SLOT(gotJs(QByteArray)));
86     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorJs(QNetworkReply*)));
87 }
88
89 void JsFunctions::gotJs(const QByteArray &bytes) {
90     if (bytes.isEmpty()) {
91         qWarning() << "Got empty js";
92         return;
93     }
94     QFile file(jsPath());
95     if (!file.open(QIODevice::WriteOnly)) {
96         qWarning() << "Cannot write" << file.errorString() << file.fileName();
97         return;
98     }
99     QDataStream stream(&file);
100     stream.writeRawData(bytes.constData(), bytes.size());
101     parseJs(QString::fromUtf8(bytes));
102 }
103
104 void JsFunctions::errorJs(QNetworkReply *reply) {
105     qWarning() << "Cannot get" << jsFilename() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()
106                << reply->url().toString() << reply->errorString();
107 }
108
109 QScriptValue JsFunctions::evaluate(const QString &js) {
110     if (!engine) return QString();
111     QScriptValue value = engine->evaluate(js);
112     if (value.isUndefined())
113         qWarning() << "Undefined result for" << js;
114     if (value.isError())
115         qWarning() << "Error in" << js << value.toString();
116
117     return value;
118 }
119
120 QString JsFunctions::string(const QString &js) {
121     return evaluate(js).toString();
122 }
123
124 QStringList JsFunctions::stringArray(const QString &js) {
125     QStringList items;
126     QScriptValue array = evaluate(js);
127     if (!array.isArray()) return items;
128     QScriptValueIterator it(array);
129     while (it.hasNext()) {
130         it.next();
131         QScriptValue value = it.value();
132         if (!value.isString()) continue;
133         items << value.toString();
134     }
135     return items;
136 }
137
138 QString JsFunctions::decryptSignature(const QString &s) {
139     return string("decryptSignature('" + s + "')");
140 }
141
142 QString JsFunctions::decryptAgeSignature(const QString &s) {
143     return string("decryptAgeSignature('" + s + "')");
144 }
145
146 QString JsFunctions::videoIdRE() {
147     return string("videoIdRE()");
148 }
149
150 QString JsFunctions::videoTokenRE() {
151     return string("videoTokenRE()");
152 }
153
154 QString JsFunctions::videoInfoFmtMapRE() {
155     return string("videoInfoFmtMapRE()");
156 }
157
158 QString JsFunctions::webPageFmtMapRE() {
159     return string("webPageFmtMapRE()");
160 }
161
162 QString JsFunctions::ageGateRE() {
163     return string("ageGateRE()");
164 }
165
166 QString JsFunctions::jsPlayerRE() {
167     return string("jsPlayerRE()");
168 }
169
170 QString JsFunctions::signatureFunctionNameRE() {
171     return string("signatureFunctionNameRE()");
172 }
173
174 QStringList JsFunctions::apiKeys() {
175     return stringArray("apiKeys()");
176 }