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;
46 QFile resFile(QLatin1String(":/") + jsFilename());
47 resFile.open(QIODevice::ReadOnly | QIODevice::Text);
48 parseJs(QString::fromUtf8(resFile.readAll()));
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);
60 QTimer::singleShot(0, this, [this] {
61 qDebug() << "Emitting ready";
66 QString JsFunctions::jsFilename() {
67 return QFileInfo(url).fileName();
70 QString JsFunctions::jsDir() {
71 return QStandardPaths::writableLocation(QStandardPaths::DataLocation);
74 QString JsFunctions::jsPath() {
75 return jsDir() + QLatin1String("/") + jsFilename();
78 void JsFunctions::loadJs() {
79 qDebug() << "Js Loading" << url;
82 q.addQueryItem("v", Constants::VERSION);
84 QObject *reply = Http::instance().get(url);
85 connect(reply, SIGNAL(data(QByteArray)), SLOT(gotJs(QByteArray)));
86 connect(reply, SIGNAL(error(QString)), SLOT(errorJs(QString)));
89 void JsFunctions::gotJs(const QByteArray &bytes) {
90 if (bytes.isEmpty()) {
91 qWarning() << "Got empty js";
94 QDir().mkpath(jsDir());
96 if (!file.open(QIODevice::WriteOnly)) {
97 qWarning() << "Cannot write" << file.errorString() << file.fileName();
100 QDataStream stream(&file);
101 stream.writeRawData(bytes.constData(), bytes.size());
102 parseJs(QString::fromUtf8(bytes));
105 void JsFunctions::errorJs(const QString &message) {
106 qWarning() << message;
109 QJSValue JsFunctions::evaluate(const QString &js) {
110 if (!engine) return QString();
111 QJSValue value = engine->evaluate(js);
112 if (value.isUndefined()) qWarning() << "Undefined result for" << js;
113 if (value.isError()) qWarning() << "Error in" << js << value.toString();
118 QString JsFunctions::string(const QString &js) {
119 return evaluate(js).toString();
122 QStringList JsFunctions::stringArray(const QString &js) {
124 QJSValue array = evaluate(js);
125 if (!array.isArray()) return items;
126 QJSValueIterator it(array);
127 while (it.hasNext()) {
129 QJSValue value = it.value();
130 if (!value.isString()) continue;
131 items << value.toString();
136 QString JsFunctions::decryptSignature(const QString &s) {
137 return string("decryptSignature('" + s + "')");
140 QString JsFunctions::decryptAgeSignature(const QString &s) {
141 return string("decryptAgeSignature('" + s + "')");
144 QString JsFunctions::videoIdRE() {
145 return string("videoIdRE()");
148 QString JsFunctions::videoTokenRE() {
149 return string("videoTokenRE()");
152 QString JsFunctions::videoInfoFmtMapRE() {
153 return string("videoInfoFmtMapRE()");
156 QString JsFunctions::webPageFmtMapRE() {
157 return string("webPageFmtMapRE()");
160 QString JsFunctions::ageGateRE() {
161 return string("ageGateRE()");
164 QString JsFunctions::jsPlayerRE() {
165 return string("jsPlayerRE()");
168 QString JsFunctions::signatureFunctionNameRE() {
169 return string("signatureFunctionNameRE()");
172 QStringList JsFunctions::signatureFunctionNameREs() {
173 return stringArray("signatureFunctionNameREs()");
176 QStringList JsFunctions::apiKeys() {
177 return stringArray("apiKeys()");