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