-
Notifications
You must be signed in to change notification settings - Fork 3
/
video_dailymotion.cpp
executable file
·142 lines (120 loc) · 4.47 KB
/
video_dailymotion.cpp
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
/*
ClipGrab³
Copyright (C) Philipp Schmieder
http://clipgrab.de
feedback [at] clipgrab [dot] de
This file is part of ClipGrab.
ClipGrab 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 3 of the License, or
(at your option) any later version.
ClipGrab 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 ClipGrab. If not, see <http://www.gnu.org/licenses/>.
*/
#include "video_dailymotion.h"
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWebKitWidgets/QtWebKitWidgets>
#endif
video_dailymotion::video_dailymotion()
{
this->_name = "Dailymotion";
this->_supportsTitle = true;
this->_supportsDescription = true;
this->_supportsThumbnail = true;
this->_supportsSearch = true;
this->_icon = 0;
this->_urlRegExp << QRegExp("http[s]?://\\w*\\.dailymotion\\.com/video/.*", Qt::CaseInsensitive);
}
video* video_dailymotion::createNewInstance()
{
return new video_dailymotion();
}
bool video_dailymotion::setUrl(QString url)
{
_originalUrl = url;
if (_url.isEmpty())
{
this->_url = QUrl(url);
QNetworkCookieJar* cookieJar = new QNetworkCookieJar;
QList<QNetworkCookie> cookieList;
cookieList << QNetworkCookie("ff", "off");
cookieJar->setCookiesFromUrl(cookieList, _url);
this->handler->networkAccessManager->setCookieJar(cookieJar);
return _url.isValid();
}
else
{
return false;
}
}
void video_dailymotion::parseVideo(QString html)
{
QRegExp expression;
expression = QRegExp("video_title\":\"(.*)\"");
expression.setMinimal(true);
if (expression.indexIn(html) !=-1)
{
_title = QString(expression.cap(1));
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
html = QUrl::fromEncoded(html.toAscii()).toString(QUrl::None);
#else
html = QUrl::fromEncoded(html.toLatin1()).toString(QUrl::None);
#endif
expression = QRegExp("var config\\s*=\\s*(\\{.+\\});");
expression.setMinimal(true);
qDebug() << expression.indexIn(html);
if (expression.indexIn(html)!=-1)
{
QString json = expression.cap(1);
QList<dailymotion_quality> qualities;
qualities << dailymotion_quality("1080", tr("HD (1080p)"));
qualities << dailymotion_quality("720", tr("HD (720p)"));
qualities << dailymotion_quality("480", tr("480p"));
qualities << dailymotion_quality("380", tr("380p"));
qualities << dailymotion_quality("240", tr("240p"));
QListIterator<dailymotion_quality> i(qualities);
while (i.hasNext())
{
dailymotion_quality quality = i.next();
QString url = getQualityUrl(json, quality.key);
if (!url.isEmpty())
{
videoQuality newQuality;
newQuality.quality = quality.name;
newQuality.videoUrl = url;
newQuality.containerName = url.contains(".mp4") ? ".mp4" : ".flv";
newQuality.resolution = quality.key.toInt();
_supportedQualities.append(newQuality);
}
}
}
}
if (_supportedQualities.isEmpty() | _title.isEmpty())
{
emit error("Could not retrieve video title.", this);
}
emit analysingFinished();
}
QString video_dailymotion::getQualityUrl(QString json, QString quality)
{
QWebView* view = new QWebView();
QString script;
script.append( "function getUrl() {\n");
script.append( " var json = " + json + ";\n");
script.append( " var sources = json.metadata.qualities['" + quality + "'];\n");
script.append( " for (var i in sources) {\n");
script.append( " if (sources[i].type == 'video\\/mp4') {\n");
script.append( " return sources[i].url\n");
script.append( " }\n");
script.append( " }\n");
script.append( "}\n");
view->setHtml("<script>" + script + "</script>");
QString result = view->page()->mainFrame()->evaluateJavaScript("getUrl()").toString();
qDebug() << script;
view->deleteLater();
return result;
}