3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "jsfunctions.h"
22 #include "constants.h"
25 #include <QJSValueIterator>
27 JsFunctions *JsFunctions::instance() {
28 static JsFunctions *i = new JsFunctions(QLatin1String(Constants::WEBSITE) + "-ws/functions.js");
32 JsFunctions::JsFunctions(const QString &url, QObject *parent)
33 : QObject(parent), url(url), engine(nullptr) {
36 if (file.open(QIODevice::ReadOnly | QIODevice::Text))
37 parseJs(QString::fromUtf8(file.readAll()));
39 qWarning() << "Cannot open" << file.errorString() << file.fileName();
41 bool stale = info.size() == 0 || info.lastModified().toTime_t() <
42 QDateTime::currentDateTime().toTime_t() - 1800;
45 QFile resFile(QLatin1String(":/") + jsFilename());
46 resFile.open(QIODevice::ReadOnly | QIODevice::Text);
47 parseJs(QString::fromUtf8(resFile.readAll()));
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);
61 QString JsFunctions::jsFilename() {
62 return QFileInfo(url).fileName();
65 QString JsFunctions::jsDir() {
66 return QStandardPaths::writableLocation(QStandardPaths::DataLocation);
69 QString JsFunctions::jsPath() {
70 return jsDir() + QLatin1String("/") + jsFilename();
73 void JsFunctions::loadJs() {
76 q.addQueryItem("v", Constants::VERSION);
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)));
83 void JsFunctions::gotJs(const QByteArray &bytes) {
84 if (bytes.isEmpty()) {
85 qWarning() << "Got empty js";
88 QDir().mkpath(jsDir());
90 if (!file.open(QIODevice::WriteOnly)) {
91 qWarning() << "Cannot write" << file.errorString() << file.fileName();
94 QDataStream stream(&file);
95 stream.writeRawData(bytes.constData(), bytes.size());
96 parseJs(QString::fromUtf8(bytes));
99 void JsFunctions::errorJs(const QString &message) {
100 qWarning() << message;
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();
112 QString JsFunctions::string(const QString &js) {
113 return evaluate(js).toString();
116 QStringList JsFunctions::stringArray(const QString &js) {
118 QJSValue array = evaluate(js);
119 if (!array.isArray()) return items;
120 QJSValueIterator it(array);
121 while (it.hasNext()) {
123 QJSValue value = it.value();
124 if (!value.isString()) continue;
125 items << value.toString();
130 QString JsFunctions::decryptSignature(const QString &s) {
131 return string("decryptSignature('" + s + "')");
134 QString JsFunctions::decryptAgeSignature(const QString &s) {
135 return string("decryptAgeSignature('" + s + "')");
138 QString JsFunctions::videoIdRE() {
139 return string("videoIdRE()");
142 QString JsFunctions::videoTokenRE() {
143 return string("videoTokenRE()");
146 QString JsFunctions::videoInfoFmtMapRE() {
147 return string("videoInfoFmtMapRE()");
150 QString JsFunctions::webPageFmtMapRE() {
151 return string("webPageFmtMapRE()");
154 QString JsFunctions::ageGateRE() {
155 return string("ageGateRE()");
158 QString JsFunctions::jsPlayerRE() {
159 return string("jsPlayerRE()");
162 QString JsFunctions::signatureFunctionNameRE() {
163 return string("signatureFunctionNameRE()");
166 QStringList JsFunctions::signatureFunctionNameREs() {
167 return stringArray("signatureFunctionNameREs()");
170 QStringList JsFunctions::apiKeys() {
171 return stringArray("apiKeys()");