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

Last change on this file since 189 was 188, checked in by Silvan Scherrer, 8 years ago

SMPlayer: update trunk to version 17.1.0

File size: 5.0 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2017 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) pos = text.indexOf(sig_name + "=function");
66 if (pos > -1) {
67 int endpos = text.indexOf("}", pos);
68 #ifdef ULTRAVERBOSE
69 qDebug() << "Sig::findFunctions: pos:" << pos << "endpos:" << endpos;
70 #endif
71 if (endpos > -1) {
72 sig_code = text.mid(pos, (endpos-pos)+1);
73 if (sig_code.startsWith("var ")) sig_code.replace("var " + sig_name + "=function", "function " + sig_name);
74 else
75 if (sig_code.startsWith(",")) sig_code.replace("," + sig_name + "=function", "function " + sig_name);
76 else
77 if (sig_code.startsWith(sig_name+"=")) sig_code.replace(sig_name + "=function", "function " + sig_name);
78 }
79 }
80 }
81 qDebug() << "Sig::findFunctions: sig_code:" << sig_code;
82
83 // Find the 2nd function
84 QString function2;
85 if (!sig_code.isEmpty()) {
86 QStringList parts = sig_code.split(";");
87 #ifdef ULTRAVERBOSE
88 qDebug() << "Sig::findFunctions: parts:" << parts;
89 #endif
90 foreach(QString part, parts) {
91 //qDebug() << "Sig::findFunctions: part:" << part;
92 QRegExp rx("([a-zA-Z\\$]+)\\.([a-zA-Z0-9]+)\\(");
93 if (rx.indexIn(part) != -1) {
94 QString possible_var = rx.cap(1);
95 #ifdef ULTRAVERBOSE
96 qDebug() << "Sig::findFunctions: possible var:" << possible_var;
97 #endif
98 if ((possible_var != "a") && (possible_var != "")) {
99 QString s = findText(text, "var "+ possible_var +"=", "};");
100 #ifdef ULTRAVERBOSE
101 qDebug() << "Sig::findFunctions: s:" << s;
102 #endif
103 if (!s.isEmpty()) {
104 function2 = "var "+ possible_var +" = "+ s +" };";
105 break;
106 }
107 }
108 }
109 }
110 }
111 qDebug() << "Sig::findFunctions: function2:" << function2;
112
113 if (!sig_code.isEmpty()) {
114 //sig_code = sig_code.replace("function "+ sig_name + "(", "function aclara(");
115 function_name = sig_name;
116 #ifdef ULTRAVERBOSE
117 qDebug() << "Sig::findFunctions: sig_code:" << sig_code;
118 #endif
119 res = sig_code + " " + function2;
120 }
121
122 qDebug() << "Sig::findFunctions: res:" << res;
123
124 decrypt_function = res;
125 return res;
126}
127
128QString Sig::aclara(const QString & text) {
129 int dot = text.indexOf('.');
130 qDebug("Sig::aclara: length: %d (%d.%d)", text.size(), dot, text.size()-dot-1);
131
132 QString res;
133
134 if (!decrypt_function.isEmpty()) {
135 QString fname = function_name;
136 QScriptEngine engine;
137 engine.evaluate(decrypt_function);
138
139 QScriptValueList args;
140 args << text;
141 QScriptValue aclarar = engine.globalObject().property(fname);
142 res = aclarar.call(QScriptValue(), args).toString();
143 }
144
145 qDebug() << "Sig::aclara: res:" << res;
146
147 if (res.isEmpty()) {
148 qDebug() << "Sig::aclara: signature length not supported:" << text.size() << ":" << text;
149 }
150
151 return res;
152}
153
154void Sig::save(QSettings * set) {
155 qDebug() << "Sig::save";
156
157 set->beginGroup("sig");
158 set->setValue("player", html5_player);
159 set->setValue("sts", sts);
160 set->setValue("function_name", function_name);
161 set->setValue("function", decrypt_function);
162 set->endGroup();
163}
164
165void Sig::load(QSettings * set) {
166 qDebug() << "Sig::load";
167
168 set->beginGroup("sig");
169 html5_player = set->value("player", "").toString();
170 sts = set->value("sts", "").toString();
171 function_name = set->value("function_name", "").toString();
172 decrypt_function = set->value("function", "").toString();
173 set->endGroup();
174}
Note: See TracBrowser for help on using the repository browser.