| 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 "ytsig.h"
|
|---|
| 20 | #include <QtScript>
|
|---|
| 21 |
|
|---|
| 22 | QString YTSig::script_filename;
|
|---|
| 23 |
|
|---|
| 24 | QString YTSig::script;
|
|---|
| 25 | QString YTSig::default_script;
|
|---|
| 26 |
|
|---|
| 27 | QString YTSig::parsed_ts;
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | QString YTSig::aclara(const QString & text, const QString & player, const QString & function_name) {
|
|---|
| 31 | int dot = text.indexOf('.');
|
|---|
| 32 | qDebug("YTSig::aclara: length: %d (%d.%d) p: %d", text.size(), dot, text.size()-dot-1, !player.isEmpty());
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 60 | //qDebug() << "YTSig::aclara: function_name:" << function_name;
|
|---|
| 61 |
|
|---|
| 62 | QScriptValue aclarar = engine.globalObject().property(fname);
|
|---|
| 63 | QString res = aclarar.call(QScriptValue(), args).toString();
|
|---|
| 64 |
|
|---|
| 65 | //qDebug() << res;
|
|---|
| 66 |
|
|---|
| 67 | if (res.isEmpty()) {
|
|---|
| 68 | qDebug() << "YTSig::aclara: signature length not supported:" << text.size() << ":" << text;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | return res;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | void YTSig::reloadScriptFile() {
|
|---|
| 75 | qDebug() << "YTSig::reloadScriptFile:" << script_filename;
|
|---|
| 76 |
|
|---|
| 77 | if (!QFile::exists(script_filename)) {
|
|---|
| 78 | qDebug() << "YTSig::reloadScriptFile: file doesn't exist.";
|
|---|
| 79 | return;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | QFile f(script_filename);
|
|---|
| 83 | f.open(QIODevice::ReadOnly);
|
|---|
| 84 | QByteArray bytes = f.readAll();
|
|---|
| 85 | f.close();
|
|---|
| 86 |
|
|---|
| 87 | parsed_ts = "";
|
|---|
| 88 |
|
|---|
| 89 | if (!bytes.isEmpty()) {
|
|---|
| 90 | script = bytes;
|
|---|
| 91 |
|
|---|
| 92 | QRegExp rx("D: ([\\d,a-z,A-Z-]+)");
|
|---|
| 93 | if (rx.indexIn(bytes)) {
|
|---|
| 94 | QByteArray d = rx.cap(1).toLatin1();
|
|---|
| 95 | qDebug() << "YTSig::reloadScriptFile: d:" << d;
|
|---|
| 96 | parsed_ts = QByteArray::fromBase64(d);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | void YTSig::check(QString & u) {
|
|---|
| 103 | if (!parsed_ts.isEmpty()) {
|
|---|
| 104 | u.append(QString("&%1").arg(parsed_ts));
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|