]> git.sur5r.net Git - minitube/blob - src/jsfunctions.cpp
Upload 3.9.3-2 to unstable
[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 "constants.h"
23 #include "http.h"
24
25 #include <QJSValueIterator>
26
27 JsFunctions *JsFunctions::instance() {
28     static JsFunctions *i = new JsFunctions(QLatin1String(Constants::WEBSITE) + "-ws/functions.js");
29     return i;
30 }
31
32 JsFunctions::JsFunctions(const QString &url, QObject *parent)
33     : QObject(parent), url(url), engine(nullptr) {
34     QFile file(jsPath());
35     if (file.exists()) {
36         if (file.open(QIODevice::ReadOnly | QIODevice::Text))
37             parseJs(QString::fromUtf8(file.readAll()));
38         else
39             qWarning() << "Cannot open" << file.errorString() << file.fileName();
40         QFileInfo info(file);
41         bool stale = info.size() == 0 || info.lastModified().toTime_t() <
42                                                  QDateTime::currentDateTime().toTime_t() - 1800;
43         if (stale) loadJs();
44     } else {
45         /*
46         QFile resFile(QLatin1String(":/") + jsFilename());
47         resFile.open(QIODevice::ReadOnly | QIODevice::Text);
48         parseJs(QString::fromUtf8(resFile.readAll()));
49         */
50         loadJs();
51     }
52 }
53
54 void JsFunctions::parseJs(const QString &js) {
55     // qDebug() << "Js Parsing" << js;
56     if (js.isEmpty()) return;
57     if (engine) delete engine;
58     engine = new QJSEngine(this);
59     engine->evaluate(js);
60     emit ready();
61 }
62
63 QString JsFunctions::jsFilename() {
64     return QFileInfo(url).fileName();
65 }
66
67 QString JsFunctions::jsDir() {
68     return QStandardPaths::writableLocation(QStandardPaths::DataLocation);
69 }
70
71 QString JsFunctions::jsPath() {
72     return jsDir() + QLatin1String("/") + jsFilename();
73 }
74
75 void JsFunctions::loadJs() {
76     qDebug() << "Js Loading" << url;
77     QUrl url(this->url);
78     QUrlQuery q;
79     q.addQueryItem("v", Constants::VERSION);
80     url.setQuery(q);
81     QObject *reply = Http::instance().get(url);
82     connect(reply, SIGNAL(data(QByteArray)), SLOT(gotJs(QByteArray)));
83     connect(reply, SIGNAL(error(QString)), SLOT(errorJs(QString)));
84 }
85
86 void JsFunctions::gotJs(const QByteArray &bytes) {
87     if (bytes.isEmpty()) {
88         qWarning() << "Got empty js";
89         return;
90     }
91     QDir().mkpath(jsDir());
92     QFile file(jsPath());
93     if (!file.open(QIODevice::WriteOnly)) {
94         qWarning() << "Cannot write" << file.errorString() << file.fileName();
95         return;
96     }
97     QDataStream stream(&file);
98     stream.writeRawData(bytes.constData(), bytes.size());
99     parseJs(QString::fromUtf8(bytes));
100 }
101
102 void JsFunctions::errorJs(const QString &message) {
103     qWarning() << message;
104 }
105
106 QJSValue JsFunctions::evaluate(const QString &js) {
107     if (!engine) return QString();
108     QJSValue value = engine->evaluate(js);
109     if (value.isUndefined()) qWarning() << "Undefined result for" << js;
110     if (value.isError()) 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     QJSValue array = evaluate(js);
122     if (!array.isArray()) return items;
123     QJSValueIterator it(array);
124     while (it.hasNext()) {
125         it.next();
126         QJSValue 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::signatureFunctionNameREs() {
170     return stringArray("signatureFunctionNameREs()");
171 }
172
173 QStringList JsFunctions::apiKeys() {
174     return stringArray("apiKeys()");
175 }