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 "networkaccess.h"
23 #include "constants.h"
24 #include "compatibility/qurlqueryhelper.h"
25 #include "compatibility/pathsservice.h"
28 NetworkAccess* http();
31 JsFunctions* JsFunctions::instance() {
32 static JsFunctions *i = new JsFunctions(QLatin1String(Constants::WEBSITE) + "-ws/functions.js");
36 JsFunctions::JsFunctions(const QString &url, QObject *parent) : QObject(parent), url(url), engine(0) {
39 if (file.open(QIODevice::ReadOnly | QIODevice::Text))
40 parseJs(QString::fromUtf8(file.readAll()));
42 qWarning() << "Cannot open" << file.errorString() << file.fileName();
44 bool stale = info.size() == 0 || info.lastModified().toTime_t() < QDateTime::currentDateTime().toTime_t() - 1800;
47 QFile resFile(QLatin1String(":/") + jsFilename());
48 resFile.open(QIODevice::ReadOnly | QIODevice::Text);
49 parseJs(QString::fromUtf8(resFile.readAll()));
54 void JsFunctions::parseJs(const QString &js) {
55 if (js.isEmpty()) return;
56 // qDebug() << "Parsing" << js;
57 if (engine) delete engine;
58 engine = new QScriptEngine(this);
63 QString JsFunctions::jsFilename() {
64 return QFileInfo(url).fileName();
67 QString JsFunctions::jsPath() {
68 return Paths::getDataLocation() + "/" + jsFilename();
71 void JsFunctions::loadJs() {
74 QUrlQueryHelper urlHelper(url);
75 urlHelper.addQueryItem("v", Constants::VERSION);
78 NetworkReply* reply = The::http()->get(url);
79 connect(reply, SIGNAL(data(QByteArray)), SLOT(gotJs(QByteArray)));
80 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorJs(QNetworkReply*)));
83 void JsFunctions::gotJs(const QByteArray &bytes) {
84 if (bytes.isEmpty()) {
85 qWarning() << "Got empty js";
89 if (!file.open(QIODevice::WriteOnly)) {
90 qWarning() << "Cannot write" << file.errorString() << file.fileName();
93 QDataStream stream(&file);
94 stream.writeRawData(bytes.constData(), bytes.size());
95 parseJs(QString::fromUtf8(bytes));
98 void JsFunctions::errorJs(QNetworkReply *reply) {
99 qWarning() << "Cannot get" << jsFilename() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()
100 << reply->url().toString() << reply->errorString();
103 QScriptValue JsFunctions::evaluate(const QString &js) {
104 if (!engine) return QString();
105 QScriptValue value = engine->evaluate(js);
106 if (value.isUndefined())
107 qWarning() << "Undefined result for" << js;
109 qWarning() << "Error in" << js << value.toString();
114 QString JsFunctions::string(const QString &js) {
115 return evaluate(js).toString();
118 QStringList JsFunctions::stringArray(const QString &js) {
120 QScriptValue array = evaluate(js);
121 if (!array.isArray()) return items;
122 QScriptValueIterator it(array);
123 while (it.hasNext()) {
125 QScriptValue value = it.value();
126 if (!value.isString()) continue;
127 items << value.toString();
132 QString JsFunctions::decryptSignature(const QString &s) {
133 return string("decryptSignature('" + s + "')");
136 QString JsFunctions::decryptAgeSignature(const QString &s) {
137 return string("decryptAgeSignature('" + s + "')");
140 QString JsFunctions::videoIdRE() {
141 return string("videoIdRE()");
144 QString JsFunctions::videoTokenRE() {
145 return string("videoTokenRE()");
148 QString JsFunctions::videoInfoFmtMapRE() {
149 return string("videoInfoFmtMapRE()");
152 QString JsFunctions::webPageFmtMapRE() {
153 return string("webPageFmtMapRE()");
156 QString JsFunctions::ageGateRE() {
157 return string("ageGateRE()");
160 QString JsFunctions::jsPlayerRE() {
161 return string("jsPlayerRE()");
164 QString JsFunctions::signatureFunctionNameRE() {
165 return string("signatureFunctionNameRE()");
168 QStringList JsFunctions::apiKeys() {
169 return stringArray("apiKeys()");