[154] | 1 | /* smplayer, GUI front-end for mplayer.
|
---|
[188] | 2 | Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
[154] | 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 "ytsig.h"
|
---|
| 20 | #include <QtScript>
|
---|
| 21 |
|
---|
[176] | 22 | QString YTSig::script_filename;
|
---|
| 23 |
|
---|
| 24 | QString YTSig::script;
|
---|
| 25 | QString YTSig::default_script;
|
---|
| 26 |
|
---|
| 27 | QString YTSig::parsed_ts;
|
---|
| 28 |
|
---|
| 29 |
|
---|
[165] | 30 | QString YTSig::aclara(const QString & text, const QString & player, const QString & function_name) {
|
---|
[154] | 31 | int dot = text.indexOf('.');
|
---|
[165] | 32 | qDebug("YTSig::aclara: length: %d (%d.%d) p: %d", text.size(), dot, text.size()-dot-1, !player.isEmpty());
|
---|
[154] | 33 |
|
---|
| 34 | if (script.isEmpty()) script = default_script;
|
---|
| 35 |
|
---|
| 36 | QScriptEngine engine;
|
---|
| 37 |
|
---|
| 38 | //QScriptSyntaxCheckResult r = engine.checkSyntax(script);
|
---|
| 39 | //qDebug() << (int) r.state();
|
---|
| 40 |
|
---|
| 41 | engine.evaluate(script);
|
---|
| 42 |
|
---|
[165] | 43 | QScriptValueList args;
|
---|
| 44 | QString fname;
|
---|
| 45 |
|
---|
| 46 | if (!function_name.isEmpty()) {
|
---|
| 47 | fname = function_name;
|
---|
| 48 | args << text;
|
---|
| 49 | }
|
---|
| 50 | else
|
---|
| 51 | if (player.isEmpty()) {
|
---|
| 52 | fname = "aclara";
|
---|
| 53 | args << text;
|
---|
| 54 | }
|
---|
| 55 | else {
|
---|
| 56 | fname = "aclara_p";
|
---|
| 57 | args << text << player;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[176] | 60 | //qDebug() << "YTSig::aclara: function_name:" << function_name;
|
---|
[165] | 61 |
|
---|
| 62 | QScriptValue aclarar = engine.globalObject().property(fname);
|
---|
| 63 | QString res = aclarar.call(QScriptValue(), args).toString();
|
---|
| 64 |
|
---|
[154] | 65 | //qDebug() << res;
|
---|
| 66 |
|
---|
| 67 | if (res.isEmpty()) {
|
---|
[176] | 68 | qDebug() << "YTSig::aclara: signature length not supported:" << text.size() << ":" << text;
|
---|
[154] | 69 | }
|
---|
| 70 |
|
---|
| 71 | return res;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void YTSig::reloadScriptFile() {
|
---|
[176] | 75 | qDebug() << "YTSig::reloadScriptFile:" << script_filename;
|
---|
[154] | 76 |
|
---|
[176] | 77 | if (!QFile::exists(script_filename)) {
|
---|
| 78 | qDebug() << "YTSig::reloadScriptFile: file doesn't exist.";
|
---|
[154] | 79 | return;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[176] | 82 | QFile f(script_filename);
|
---|
[154] | 83 | f.open(QIODevice::ReadOnly);
|
---|
| 84 | QByteArray bytes = f.readAll();
|
---|
| 85 | f.close();
|
---|
| 86 |
|
---|
[165] | 87 | parsed_ts = "";
|
---|
| 88 |
|
---|
[154] | 89 | if (!bytes.isEmpty()) {
|
---|
| 90 | script = bytes;
|
---|
[165] | 91 |
|
---|
| 92 | QRegExp rx("D: ([\\d,a-z,A-Z-]+)");
|
---|
| 93 | if (rx.indexIn(bytes)) {
|
---|
| 94 | QByteArray d = rx.cap(1).toLatin1();
|
---|
[176] | 95 | qDebug() << "YTSig::reloadScriptFile: d:" << d;
|
---|
[165] | 96 | parsed_ts = QByteArray::fromBase64(d);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[154] | 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[165] | 102 | void YTSig::check(QString & u) {
|
---|
| 103 | if (!parsed_ts.isEmpty()) {
|
---|
| 104 | u.append(QString("&%1").arg(parsed_ts));
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|