]> git.sur5r.net Git - minitube/blob - src/jsfunctions.cpp
New upstream version 3.1
[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         QFile resFile(QLatin1String(":/") + jsFilename());
46         resFile.open(QIODevice::ReadOnly | QIODevice::Text);
47         parseJs(QString::fromUtf8(resFile.readAll()));
48         loadJs();
49     }
50 }
51
52 void JsFunctions::parseJs(const QString &js) {
53     if (js.isEmpty()) return;
54     // qDebug() << "Parsing" << js;
55     if (engine) delete engine;
56     engine = new QJSEngine(this);
57     engine->evaluate(js);
58     emit ready();
59 }
60
61 QString JsFunctions::jsFilename() {
62     return QFileInfo(url).fileName();
63 }
64
65 QString JsFunctions::jsDir() {
66     return QStandardPaths::writableLocation(QStandardPaths::DataLocation);
67 }
68
69 QString JsFunctions::jsPath() {
70     return jsDir() + QLatin1String("/") + jsFilename();
71 }
72
73 void JsFunctions::loadJs() {
74     QUrl url(this->url);
75     QUrlQuery q;
76     q.addQueryItem("v", Constants::VERSION);
77     url.setQuery(q);
78     QObject *reply = Http::instance().get(url);
79     connect(reply, SIGNAL(data(QByteArray)), SLOT(gotJs(QByteArray)));
80     connect(reply, SIGNAL(error(QString)), SLOT(errorJs(QString)));
81 }
82
83 void JsFunctions::gotJs(const QByteArray &bytes) {
84     if (bytes.isEmpty()) {
85         qWarning() << "Got empty js";
86         return;
87     }
88     QDir().mkpath(jsDir());
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(const QString &message) {
100     qWarning() << message;
101 }
102
103 QJSValue JsFunctions::evaluate(const QString &js) {
104     if (!engine) return QString();
105     QJSValue value = engine->evaluate(js);
106     if (value.isUndefined()) qWarning() << "Undefined result for" << js;
107     if (value.isError()) qWarning() << "Error in" << js << value.toString();
108
109     return value;
110 }
111
112 QString JsFunctions::string(const QString &js) {
113     return evaluate(js).toString();
114 }
115
116 QStringList JsFunctions::stringArray(const QString &js) {
117     QStringList items;
118     QJSValue array = evaluate(js);
119     if (!array.isArray()) return items;
120     QJSValueIterator it(array);
121     while (it.hasNext()) {
122         it.next();
123         QJSValue value = it.value();
124         if (!value.isString()) continue;
125         items << value.toString();
126     }
127     return items;
128 }
129
130 QString JsFunctions::decryptSignature(const QString &s) {
131     return string("decryptSignature('" + s + "')");
132 }
133
134 QString JsFunctions::decryptAgeSignature(const QString &s) {
135     return string("decryptAgeSignature('" + s + "')");
136 }
137
138 QString JsFunctions::videoIdRE() {
139     return string("videoIdRE()");
140 }
141
142 QString JsFunctions::videoTokenRE() {
143     return string("videoTokenRE()");
144 }
145
146 QString JsFunctions::videoInfoFmtMapRE() {
147     return string("videoInfoFmtMapRE()");
148 }
149
150 QString JsFunctions::webPageFmtMapRE() {
151     return string("webPageFmtMapRE()");
152 }
153
154 QString JsFunctions::ageGateRE() {
155     return string("ageGateRE()");
156 }
157
158 QString JsFunctions::jsPlayerRE() {
159     return string("jsPlayerRE()");
160 }
161
162 QString JsFunctions::signatureFunctionNameRE() {
163     return string("signatureFunctionNameRE()");
164 }
165
166 QStringList JsFunctions::signatureFunctionNameREs() {
167     return stringArray("signatureFunctionNameREs()");
168 }
169
170 QStringList JsFunctions::apiKeys() {
171     return stringArray("apiKeys()");
172 }