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"
23 #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) : QObject(parent), url(url), engine(0) {
35 if (file.open(QIODevice::ReadOnly | QIODevice::Text))
36 parseJs(QString::fromUtf8(file.readAll()));
38 qWarning() << "Cannot open" << file.errorString() << file.fileName();
40 bool stale = info.size() == 0 || info.lastModified().toTime_t() < QDateTime::currentDateTime().toTime_t() - 1800;
43 QFile resFile(QLatin1String(":/") + jsFilename());
44 resFile.open(QIODevice::ReadOnly | QIODevice::Text);
45 parseJs(QString::fromUtf8(resFile.readAll()));
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);
59 QString JsFunctions::jsFilename() {
60 return QFileInfo(url).fileName();
63 QString JsFunctions::jsDir() {
64 return QStandardPaths::writableLocation(QStandardPaths::DataLocation);
67 QString JsFunctions::jsPath() {
68 return jsDir() + QLatin1String("/") + jsFilename();
71 void JsFunctions::loadJs() {
74 q.addQueryItem("v", Constants::VERSION);
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)));
81 void JsFunctions::gotJs(const QByteArray &bytes) {
82 if (bytes.isEmpty()) {
83 qWarning() << "Got empty js";
86 QDir().mkpath(jsDir());
88 if (!file.open(QIODevice::WriteOnly)) {
89 qWarning() << "Cannot write" << file.errorString() << file.fileName();
92 QDataStream stream(&file);
93 stream.writeRawData(bytes.constData(), bytes.size());
94 parseJs(QString::fromUtf8(bytes));
97 void JsFunctions::errorJs(const QString &message) {
98 qWarning() << message;
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;
107 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::apiKeys() {
167 return stringArray("apiKeys()");