-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.cpp
More file actions
190 lines (174 loc) · 6.64 KB
/
utility.cpp
File metadata and controls
190 lines (174 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
Copyright (C) 2009-2013 jakago
This file is part of CaptureStream, the flv downloader for NHK radio
language courses.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utility.h"
#include "urldownloader.h"
#include "mainwindow.h"
#include "qt4qt5.h"
#include <QUrl>
#include <QRegExp>
#include <QCoreApplication>
#include <QDir>
#include <QTemporaryFile>
#include <QProcess>
#include <QFileInfo>
#include <QFile>
#include <QByteArray>
#include <QXmlQuery>
#include <QScriptEngine>
#include <QScriptValue>
#include <QDebug>
#include <QDateTime>
namespace {
const QString UPUPUP( "/../../.." );
const QString FLARE( "flare" );
const QString GNASH( "gnash -r0 -v http://www.nhk.or.jp/gogaku/common/swf/streaming.swf" );
const QUrl STREAMINGSWF( "http://www.nhk.or.jp/gogaku/common/swf/streaming.swf" );
const QString TEMPLATE( "streamingXXXXXX.swf" );
const QRegExp REGEXP( "function startInit\\(\\) \\{[^}]*\\}\\s*function (\\w*).*startInit\\(\\);" );
const QRegExp PREFIX( "load\\('([A-Z0-9]*)' \\+ CONNECT_DIRECTORY" );
const QRegExp SUFFIX( "CONNECT_DIRECTORY \\+ '(.*)/' \\+ INIT_URI" );
const QString LISTDATAFLV( "http://www.nhk.or.jp/gogaku/common/swf/(\\w+)/listdataflv.xml" );
const QString WIKIXML1( "doc('" );
const QString WIKIXML2( "')/flv/scramble[@date=\"" );
const QString WIKIXML3( "\"]/@code/string()" );
}
// Macの場合はアプリケーションバンドル、それ以外はアプリケーションが含まれるディレクトリを返す
QString Utility::applicationBundlePath() {
QString result = QCoreApplication::applicationDirPath();
//#ifdef QT4_QT5_MAC //Macのffmpegパス不正対策 2022/04/14
// result = QDir::cleanPath( result + UPUPUP );
//#endif
result += QDir::separator();
return result;
}
// flareの出力を利用してスクランブル文字列を解析する
QString Utility::flare( QString& error ) {
QString result;
UrlDownloader urldownloader;
urldownloader.doDownload( STREAMINGSWF );
if ( urldownloader.contents().length() ) {
QTemporaryFile file;
file.setFileTemplate( QDir::tempPath() + QDir::separator() + TEMPLATE );
if ( file.open() ) {
file.write( urldownloader.contents() );
file.close();
QProcess process;
process.start( applicationBundlePath() + FLARE, QStringList( file.fileName() ) );
if ( process.waitForStarted() && process.waitForFinished() ) {
QFileInfo info( file.fileName() );
QString flr = info.absolutePath() + QDir::separator() + info.completeBaseName() + ".flr";
QFile scriptFile( flr );
if ( scriptFile.open( QIODevice::ReadOnly ) ) {
QByteArray bytes = scriptFile.readAll();
QString contents = QString::fromUtf8( bytes.constData() );
scriptFile.close();
if ( REGEXP.indexIn( contents, 0 ) != -1 )
contents = REGEXP.cap();
QString prefix;
if ( PREFIX.indexIn( contents, 0 ) != -1 )
prefix = PREFIX.cap( 1 );
QString suffix;
if ( SUFFIX.indexIn( contents, 0 ) != -1 )
suffix = SUFFIX.cap( 1 );
QScriptEngine myEngine;
myEngine.evaluate( contents );
QScriptValue generator = myEngine.globalObject().property( REGEXP.cap( 1 ) );
if ( !generator.isValid() ) {
contents.replace( ".", " ." );
myEngine.evaluate( contents );
generator = myEngine.globalObject().property( REGEXP.cap( 1 ) );
}
if ( generator.isValid() ) {
generator.call();
QRegExp variable( "(\\w+)[^\\n=]*=[^\\n]*\\n[^\\n]*\\}" );
if ( variable.indexIn( generator.toString(), 0 ) != -1 )
result = prefix + myEngine.globalObject().property( variable.cap( 1 ) ).toString() + suffix;
}
}
QFile::remove( flr );
if ( !result.length() )
error = QString::fromUtf8( "コードの取得に失敗しました。" );
} else
error = QString::fromUtf8( "flareが存在しないか実行に失敗しました。" );
}
}
return result;
}
// gnashの出力を利用してスクランブル文字列を解析する
QString Utility::gnash( QString& error ) {
QString result;
QProcess process;
process.start( GNASH );
bool started = process.waitForStarted();
if ( !started ) {
process.start( "sdl-" + GNASH );
started = process.waitForStarted();
}
if ( !started ) {
process.start( applicationBundlePath() + GNASH );
started = process.waitForStarted();
}
if ( !started ) {
process.start( applicationBundlePath() + "sdl-" + GNASH );
started = process.waitForStarted();
}
if ( started ) {
QRegExp regexp( LISTDATAFLV );
while ( process.waitForReadyRead( 5000 ) ) {
QByteArray bytes = process.readAllStandardOutput();
QString output = QString::fromUtf8( bytes.data() );
if ( regexp.indexIn( output ) != -1 ) {
result = regexp.cap(1);
process.terminate();
break;
}
}
if ( !result.length() )
error = QString::fromUtf8( "コードの取得に失敗しました。" );
} else
error = QString::fromUtf8( "gnashが存在しないか実行に失敗しました。" );
return result;
}
// ウィキからスクランブル文字列を取得する
QString Utility::wiki() {
QDate today = QDate::currentDate();
int offset = 1 - today.dayOfWeek(); //直前の月曜までのオフセット。月曜日なら0
if ( offset == 0 && QTime::currentTime().hour() <= 9 ) //月曜日で10時より前なら1週間前の月曜日に
offset = -7;
QDate monday = today.addDays( offset );
QString result;
QStringList attributeList;
QXmlQuery query;
query.setQuery( WIKIXML1 + MainWindow::scrambleUrl1 + WIKIXML2 + monday.toString( "yyyyMMdd" ) + WIKIXML3 );
if ( query.isValid() ) {
query.evaluateTo( &attributeList );
if ( attributeList.count() > 0 )
result = attributeList[0];
}
// urlが転送されるようになった問題に対応
if ( result.length() <= 0 ) {
query.setQuery( WIKIXML1 + MainWindow::scrambleUrl2 + WIKIXML2 + monday.toString( "yyyyMMdd" ) + WIKIXML3 );
if ( query.isValid() ) {
query.evaluateTo( &attributeList );
if ( attributeList.count() > 0 )
result = attributeList[0];
}
}
return result;
}
bool Utility::nogui() {
return QCoreApplication::arguments().contains( "-nogui" );
}