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 <QDesktopServices>
24 #include "constants.h"
27 NetworkAccess* http();
30 JsFunctions* JsFunctions::instance() {
31 static JsFunctions *i = new JsFunctions();
35 JsFunctions::JsFunctions(QObject *parent) : QObject(parent), engine(0) {
38 if (file.open(QIODevice::ReadOnly | QIODevice::Text))
39 parseJs(QString::fromUtf8(file.readAll()));
41 qWarning() << file.errorString() << file.fileName();
43 if (info.lastModified().toTime_t() < QDateTime::currentDateTime().toTime_t() - 1800)
46 QFile resFile(QLatin1String(":/") + jsFilename());
47 resFile.open(QIODevice::ReadOnly | QIODevice::Text);
48 parseJs(QString::fromUtf8(resFile.readAll()));
53 void JsFunctions::parseJs(const QString &js) {
54 if (js.isEmpty()) return;
55 if (engine) delete engine;
56 engine = new QScriptEngine();
60 const QLatin1String & JsFunctions::jsFilename() {
61 static const QLatin1String filename("functions.js");
65 const QString & JsFunctions::jsPath() {
66 static const QString path(
67 #if QT_VERSION >= 0x050000
68 QStandardPaths::writableLocation(QStandardPaths::DataLocation)
70 QDesktopServices::storageLocation(QDesktopServices::DataLocation)
72 + "/" + jsFilename());
76 void JsFunctions::loadJs() {
77 QUrl url(QLatin1String(Constants::WEBSITE) + "-ws/" + jsFilename());
78 url.addQueryItem("v", Constants::VERSION);
79 NetworkReply* reply = The::http()->get(url);
80 connect(reply, SIGNAL(data(QByteArray)), SLOT(gotJs(QByteArray)));
81 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorJs(QNetworkReply*)));
84 void JsFunctions::gotJs(QByteArray bytes) {
85 parseJs(QString::fromUtf8(bytes));
87 if (!file.open(QIODevice::WriteOnly))
88 qWarning() << file.errorString() << file.fileName();
89 QDataStream stream(&file);
90 stream.writeRawData(bytes.constData(), bytes.size());
93 void JsFunctions::errorJs(QNetworkReply *reply) {
94 qWarning() << "Cannot get" << jsFilename() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()
95 << reply->url().toString() << reply->errorString();
98 QString JsFunctions::evaluate(const QString &js) {
99 if (!engine) return QString();
100 QScriptValue value = engine->evaluate(js);
101 if (value.isUndefined())
102 qWarning() << "Undefined result for" << js;
104 qWarning() << "Error in" << js << value.toString();
106 return value.toString();
109 QString JsFunctions::decryptSignature(const QString &s) {
110 return evaluate("decryptSignature('" + s + "')");
113 QString JsFunctions::decryptAgeSignature(const QString &s) {
114 return evaluate("decryptAgeSignature('" + s + "')");
117 QString JsFunctions::videoIdRE() {
118 return evaluate("videoIdRE()");
121 QString JsFunctions::videoTokenRE() {
122 return evaluate("videoTokenRE()");
125 QString JsFunctions::videoInfoFmtMapRE() {
126 return evaluate("videoInfoFmtMapRE()");
129 QString JsFunctions::webPageFmtMapRE() {
130 return evaluate("webPageFmtMapRE()");
133 QString JsFunctions::ageGateRE() {
134 return evaluate("ageGateRE()");
137 QString JsFunctions::jsPlayerRE() {
138 return evaluate("jsPlayerRE()");
141 QString JsFunctions::signatureFunctionNameRE() {
142 return evaluate("signatureFunctionNameRE()");