]> git.sur5r.net Git - minitube/blob - src/jsfunctions.cpp
Imported Upstream version 2.4
[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
26 namespace The {
27 NetworkAccess* http();
28 }
29
30 JsFunctions* JsFunctions::instance() {
31     static JsFunctions *i = new JsFunctions(QLatin1String(Constants::WEBSITE) + "-ws/functions.js");
32     return i;
33 }
34
35 JsFunctions::JsFunctions(const QString &url, QObject *parent) : QObject(parent), url(url), engine(0) {
36     QFile file(jsPath());
37     if (file.exists()) {
38         if (file.open(QIODevice::ReadOnly | QIODevice::Text))
39             parseJs(QString::fromUtf8(file.readAll()));
40         else
41             qWarning() << "Cannot open" << file.errorString() << file.fileName();
42         QFileInfo info(file);
43         bool stale = info.size() == 0 || info.lastModified().toTime_t() < QDateTime::currentDateTime().toTime_t() - 1800;
44         if (stale) loadJs();
45     } else {
46         QFile resFile(QLatin1String(":/") + jsFilename());
47         resFile.open(QIODevice::ReadOnly | QIODevice::Text);
48         parseJs(QString::fromUtf8(resFile.readAll()));
49         loadJs();
50     }
51 }
52
53 void JsFunctions::parseJs(const QString &js) {
54     if (js.isEmpty()) return;
55     // qDebug() << "Parsing" << js;
56     if (engine) delete engine;
57     engine = new QScriptEngine(this);
58     engine->evaluate(js);
59     emit ready();
60 }
61
62 QString JsFunctions::jsFilename() {
63     return QFileInfo(url).fileName();
64 }
65
66 QString JsFunctions::jsPath() {
67     return QString(
68             #if QT_VERSION >= 0x050000
69                 QStandardPaths::writableLocation(QStandardPaths::DataLocation)
70             #else
71                 QDesktopServices::storageLocation(QDesktopServices::DataLocation)
72             #endif
73                 + "/" + jsFilename());
74 }
75
76 void JsFunctions::loadJs() {
77     QUrl url(this->url);
78     url.addQueryItem("v", Constants::VERSION);
79     NetworkReply* reply = The::http()->get(url);
80     connect(reply, SIGNAL(data(QByteArray)), SLOT(gotJs(QByteArray)));
81     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorJs(QNetworkReply*)));
82 }
83
84 void JsFunctions::gotJs(const QByteArray &bytes) {
85     if (bytes.isEmpty()) {
86         qWarning() << "Got empty js";
87         return;
88     }
89     QFile file(jsPath());
90     if (!file.open(QIODevice::WriteOnly)) {
91         qWarning() << "Cannot write" << file.errorString() << file.fileName();
92         return;
93     }
94     QDataStream stream(&file);
95     stream.writeRawData(bytes.constData(), bytes.size());
96     parseJs(QString::fromUtf8(bytes));
97 }
98
99 void JsFunctions::errorJs(QNetworkReply *reply) {
100     qWarning() << "Cannot get" << jsFilename() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()
101                << reply->url().toString() << reply->errorString();
102 }
103
104 QScriptValue JsFunctions::evaluate(const QString &js) {
105     if (!engine) return QString();
106     QScriptValue value = engine->evaluate(js);
107     if (value.isUndefined())
108         qWarning() << "Undefined result for" << js;
109     if (value.isError())
110         qWarning() << "Error in" << js << value.toString();
111
112     return value;
113 }
114
115 QString JsFunctions::string(const QString &js) {
116     return evaluate(js).toString();
117 }
118
119 QStringList JsFunctions::stringArray(const QString &js) {
120     QStringList items;
121     QScriptValue array = evaluate(js);
122     if (!array.isArray()) return items;
123     QScriptValueIterator it(array);
124     while (it.hasNext()) {
125         it.next();
126         QScriptValue value = it.value();
127         if (!value.isString()) continue;
128         items << value.toString();
129     }
130     return items;
131 }
132
133 QString JsFunctions::decryptSignature(const QString &s) {
134     return string("decryptSignature('" + s + "')");
135 }
136
137 QString JsFunctions::decryptAgeSignature(const QString &s) {
138     return string("decryptAgeSignature('" + s + "')");
139 }
140
141 QString JsFunctions::videoIdRE() {
142     return string("videoIdRE()");
143 }
144
145 QString JsFunctions::videoTokenRE() {
146     return string("videoTokenRE()");
147 }
148
149 QString JsFunctions::videoInfoFmtMapRE() {
150     return string("videoInfoFmtMapRE()");
151 }
152
153 QString JsFunctions::webPageFmtMapRE() {
154     return string("webPageFmtMapRE()");
155 }
156
157 QString JsFunctions::ageGateRE() {
158     return string("ageGateRE()");
159 }
160
161 QString JsFunctions::jsPlayerRE() {
162     return string("jsPlayerRE()");
163 }
164
165 QString JsFunctions::signatureFunctionNameRE() {
166     return string("signatureFunctionNameRE()");
167 }
168
169 QStringList JsFunctions::apiKeys() {
170     return stringArray("apiKeys()");
171 }