source: smplayer/trunk/src/youtube/sig.cpp@ 176

Last change on this file since 176 was 176, checked in by Silvan Scherrer, 9 years ago

smplayer: update trunk to version 16.4

File size: 4.8 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2016 Ricardo Villalba <rvm@users.sourceforge.net>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "sig.h"
20#include <QRegExp>
21#include <QStringList>
22#include <QtScript>
23#include <QSettings>
24#include <QDebug>
25
26//#define ULTRAVERBOSE
27
28QString Sig::findText(const QString & text, const QString & begin, const QString & end) {
29 QString res;
30
31 int pos = text.indexOf(begin);
32 if (pos > -1) {
33 int pos2 = text.indexOf(end, pos);
34 if (pos2 > -1) {
35 pos = pos + begin.length();
36 res = text.mid(pos, pos2 - pos);
37 }
38 }
39
40 return res;
41}
42
43QString Sig::findFunctions(const QString & text) {
44 QString res;
45
46 //QString sts;
47 QRegExp rx_sts("sts:(\\d+)");
48 if (rx_sts.indexIn(text) != -1) {
49 sts = rx_sts.cap(1);
50 }
51 qDebug() << "Sig::findFunctions: sts:" << sts;
52
53 QString sig_name;
54 QRegExp rx_sig("\\.sig\\|\\|([a-zA-Z0-9\\$]+)\\(");
55 if (rx_sig.indexIn(text) != -1) {
56 sig_name = rx_sig.cap(1);
57 }
58 qDebug() << "Sig::findFunctions: sig_name:" << sig_name;
59
60 QString sig_code;
61 if (!sig_name.isEmpty()) {
62 //int pos = text.indexOf("function " + sig_name);
63 int pos = text.indexOf("var " + sig_name + "=function");
64 if (pos == -1) pos = text.indexOf("," + sig_name + "=function");
65 if (pos > -1) {
66 int endpos = text.indexOf("}", pos);
67 #ifdef ULTRAVERBOSE
68 qDebug() << "Sig::findFunctions: pos:" << pos << "endpos:" << endpos;
69 #endif
70 if (endpos > -1) {
71 sig_code = text.mid(pos, (endpos-pos)+1);
72 if (sig_code.startsWith("var ")) sig_code.replace("var " + sig_name + "=function", "function " + sig_name);
73 else
74 if (sig_code.startsWith(",")) sig_code.replace("," + sig_name + "=function", "function " + sig_name);
75 }
76 }
77 }
78 qDebug() << "Sig::findFunctions: sig_code:" << sig_code;
79
80 // Find the 2nd function
81 QString function2;
82 if (!sig_code.isEmpty()) {
83 QStringList parts = sig_code.split(";");
84 #ifdef ULTRAVERBOSE
85 qDebug() << "Sig::findFunctions: parts:" << parts;
86 #endif
87 foreach(QString part, parts) {
88 //qDebug() << "Sig::findFunctions: part:" << part;
89 QRegExp rx("([a-zA-Z\\$]+)\\.([a-zA-Z0-9]+)\\(");
90 if (rx.indexIn(part) != -1) {
91 QString possible_var = rx.cap(1);
92 #ifdef ULTRAVERBOSE
93 qDebug() << "Sig::findFunctions: possible var:" << possible_var;
94 #endif
95 if ((possible_var != "a") && (possible_var != "")) {
96 QString s = findText(text, "var "+ possible_var, "};");
97 #ifdef ULTRAVERBOSE
98 qDebug() << "Sig::findFunctions: s:" << s;
99 #endif
100 if (!s.isEmpty()) {
101 function2 = "var "+ possible_var +" "+ s +" };";
102 break;
103 }
104 }
105 }
106 }
107 }
108 qDebug() << "Sig::findFunctions: function2:" << function2;
109
110 if (!sig_code.isEmpty()) {
111 //sig_code = sig_code.replace("function "+ sig_name + "(", "function aclara(");
112 function_name = sig_name;
113 #ifdef ULTRAVERBOSE
114 qDebug() << "Sig::findFunctions: sig_code:" << sig_code;
115 #endif
116 res = sig_code + " " + function2;
117 }
118
119 qDebug() << "Sig::findFunctions: res:" << res;
120
121 decrypt_function = res;
122 return res;
123}
124
125QString Sig::aclara(const QString & text) {
126 int dot = text.indexOf('.');
127 qDebug("Sig::aclara: length: %d (%d.%d)", text.size(), dot, text.size()-dot-1);
128
129 QString res;
130
131 if (!decrypt_function.isEmpty()) {
132 QString fname = function_name;
133 QScriptEngine engine;
134 engine.evaluate(decrypt_function);
135
136 QScriptValueList args;
137 args << text;
138 QScriptValue aclarar = engine.globalObject().property(fname);
139 res = aclarar.call(QScriptValue(), args).toString();
140 }
141
142 qDebug() << "Sig::aclara: res:" << res;
143
144 if (res.isEmpty()) {
145 qDebug() << "Sig::aclara: signature length not supported:" << text.size() << ":" << text;
146 }
147
148 return res;
149}
150
151void Sig::save(QSettings * set) {
152 qDebug() << "Sig::save";
153
154 set->beginGroup("sig");
155 set->setValue("player", html5_player);
156 set->setValue("sts", sts);
157 set->setValue("function_name", function_name);
158 set->setValue("function", decrypt_function);
159 set->endGroup();
160}
161
162void Sig::load(QSettings * set) {
163 qDebug() << "Sig::load";
164
165 set->beginGroup("sig");
166 html5_player = set->value("player", "").toString();
167 sts = set->value("sts", "").toString();
168 function_name = set->value("function_name", "").toString();
169 decrypt_function = set->value("function", "").toString();
170 set->endGroup();
171}
Note: See TracBrowser for help on using the repository browser.