2 #include "networkaccess.h"
4 #include "videodefinition.h"
10 Video::Video() : m_duration(0),
14 loadingStreamUrl(false)
17 Video* Video::clone() {
18 Video* cloneVideo = new Video();
19 cloneVideo->m_title = m_title;
20 cloneVideo->m_description = m_description;
21 cloneVideo->m_author = m_author;
22 cloneVideo->m_authorUri = m_authorUri;
23 cloneVideo->m_webpage = m_webpage;
24 cloneVideo->m_streamUrl = m_streamUrl;
25 cloneVideo->m_thumbnail = m_thumbnail;
26 cloneVideo->m_thumbnailUrls = m_thumbnailUrls;
27 cloneVideo->m_duration = m_duration;
28 cloneVideo->m_published = m_published;
29 cloneVideo->m_viewCount = m_viewCount;
30 cloneVideo->videoId = videoId;
31 cloneVideo->videoToken = videoToken;
32 cloneVideo->definitionCode = definitionCode;
36 void Video::preloadThumbnail() {
37 if (m_thumbnailUrls.isEmpty()) return;
38 QObject *reply = The::http()->get(m_thumbnailUrls.first());
39 connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
42 void Video::setThumbnail(QByteArray bytes) {
43 m_thumbnail = QImage::fromData(bytes);
47 const QImage Video::thumbnail() const {
51 void Video::loadStreamUrl() {
52 if (loadingStreamUrl) {
53 qDebug() << "Already loading stream URL for" << this->title();
56 loadingStreamUrl = true;
58 // https://develop.participatoryculture.org/trac/democracy/browser/trunk/tv/portable/flashscraper.py
61 // youtube-dl line 428
62 // QRegExp re("^((?:http://)?(?:\\w+\\.)?youtube\\.com/(?:(?:v/)|(?:(?:watch(?:\\.php)?)?\\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$");
63 QRegExp re("^http://www\\.youtube\\.com/watch\\?v=([0-9A-Za-z_-]+).*");
64 bool match = re.exactMatch(m_webpage.toString());
65 if (!match || re.numCaptures() < 1) {
66 emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
67 loadingStreamUrl = false;
76 void Video::getVideoInfo() {
77 static const QStringList elTypes = QStringList() << "&el=embedded" << "&el=vevo" << "&el=detailpage" << "";
79 if (elIndex > elTypes.size() - 1) {
80 loadingStreamUrl = false;
81 emit errorStreamUrl("Cannot get video info");
83 // Don't panic! We have a plan B.
84 // get the youtube video webpage
85 qDebug() << "Scraping" << webpage().toString();
86 QObject *reply = The::http()->get(webpage().toString());
87 connect(reply, SIGNAL(data(QByteArray)), SLOT(scrapeWebPage(QByteArray)));
88 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
89 // see you in scrapWebPage(QByteArray)
95 QUrl videoInfoUrl = QUrl(QString(
96 "http://www.youtube.com/get_video_info?video_id=%1%2&ps=default&eurl=&gl=US&hl=en"
97 ).arg(videoId, elTypes.at(elIndex)));
99 QObject *reply = The::http()->get(videoInfoUrl);
100 connect(reply, SIGNAL(data(QByteArray)), SLOT(gotVideoInfo(QByteArray)));
101 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
103 // see you in gotVideoInfo...
107 void Video::gotVideoInfo(QByteArray data) {
108 QString videoInfo = QString::fromUtf8(data);
110 // qDebug() << "videoInfo" << videoInfo;
113 QRegExp re = QRegExp("^.*&token=([^&]+).*$");
114 bool match = re.exactMatch(videoInfo);
115 // handle regexp failure
116 if (!match || re.numCaptures() < 1) {
117 // Don't panic! We're gonna try another magic "el" param
122 QString videoToken = re.cap(1);
123 while (videoToken.contains('%'))
124 videoToken = QByteArray::fromPercentEncoding(videoToken.toAscii());
125 // qDebug() << "videoToken" << videoToken;
126 this->videoToken = videoToken;
129 re = QRegExp("^.*&url_encoded_fmt_stream_map=([^&]+).*$");
130 match = re.exactMatch(videoInfo);
131 // handle regexp failure
132 if (!match || re.numCaptures() < 1) {
133 // Don't panic! We're gonna try another magic "el" param
139 QString fmtUrlMap = re.cap(1);
140 fmtUrlMap = QByteArray::fromPercentEncoding(fmtUrlMap.toUtf8());
143 QString definitionName = settings.value("definition").toString();
144 int definitionCode = VideoDefinition::getDefinitionCode(definitionName);
146 // qDebug() << "fmtUrlMap" << fmtUrlMap;
147 QStringList formatUrls = fmtUrlMap.split(",", QString::SkipEmptyParts);
148 QHash<int, QString> urlMap;
149 foreach(QString formatUrl, formatUrls) {
150 // qDebug() << "formatUrl" << formatUrl;
151 QStringList urlParams = formatUrl.split("&", QString::SkipEmptyParts);
152 // qDebug() << "urlParams" << urlParams;
157 foreach(QString urlParam, urlParams) {
158 if (urlParam.startsWith("itag=")) {
159 int separator = urlParam.indexOf("=");
160 format = urlParam.mid(separator + 1).toInt();
161 } else if (urlParam.startsWith("url=")) {
162 int separator = urlParam.indexOf("=");
163 url = urlParam.mid(separator + 1);
164 url = QByteArray::fromPercentEncoding(url.toUtf8());
165 } else if (urlParam.startsWith("sig=")) {
166 int separator = urlParam.indexOf("=");
167 sig = urlParam.mid(separator + 1);
168 sig = QByteArray::fromPercentEncoding(sig.toUtf8());
171 if (format == -1 || url.isNull()) continue;
173 url += "&signature=" + sig;
175 if (format == definitionCode) {
176 qDebug() << "Found format" << definitionCode;
177 QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
178 m_streamUrl = videoUrl;
179 this->definitionCode = definitionCode;
180 emit gotStreamUrl(videoUrl);
181 loadingStreamUrl = false;
185 urlMap.insert(format, url);
188 QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
189 int currentIndex = definitionCodes.indexOf(definitionCode);
190 int previousIndex = 0;
191 while (currentIndex >= 0) {
192 previousIndex = currentIndex - 1;
193 if (previousIndex < 0) previousIndex = 0;
194 int definitionCode = definitionCodes.at(previousIndex);
195 if (urlMap.contains(definitionCode)) {
196 qDebug() << "Found format" << definitionCode;
197 QString url = urlMap.value(definitionCode);
198 QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
199 m_streamUrl = videoUrl;
200 this->definitionCode = definitionCode;
201 emit gotStreamUrl(videoUrl);
202 loadingStreamUrl = false;
208 emit errorStreamUrl(tr("Cannot get video stream for %1").arg(m_webpage.toString()));
212 void Video::foundVideoUrl(QString videoToken, int definitionCode) {
213 // qDebug() << "foundVideoUrl" << videoToken << definitionCode;
215 QUrl videoUrl = QUrl(QString(
216 "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
217 ).arg(videoId, videoToken, QString::number(definitionCode)));
219 m_streamUrl = videoUrl;
220 loadingStreamUrl = false;
221 emit gotStreamUrl(videoUrl);
224 void Video::errorVideoInfo(QNetworkReply *reply) {
225 loadingStreamUrl = false;
226 emit errorStreamUrl(tr("Network error: %1 for %2").arg(reply->errorString(), reply->url().toString()));
229 void Video::scrapeWebPage(QByteArray data) {
231 QString videoHTML = QString::fromUtf8(data);
232 QRegExp re(".*, \"t\": \"([^\"]+)\".*");
233 bool match = re.exactMatch(videoHTML);
235 // on regexp failure, stop and report error
236 if (!match || re.numCaptures() < 1) {
237 emit errorStreamUrl("Error parsing video page");
238 loadingStreamUrl = false;
242 QString videoToken = re.cap(1);
243 // FIXME proper decode
244 videoToken = videoToken.replace("%3D", "=");
246 // we'll need this in gotHeadHeaders()
247 this->videoToken = videoToken;
249 // qDebug() << "token" << videoToken;
252 QString definitionName = settings.value("definition").toString();
253 int definitionCode = VideoDefinition::getDefinitionCode(definitionName);
254 if (definitionCode == 18) {
255 // This is assumed always available
256 foundVideoUrl(videoToken, 18);
258 findVideoUrl(definitionCode);
263 void Video::gotHeadHeaders(QNetworkReply* reply) {
264 int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
265 // qDebug() << "gotHeaders" << statusCode;
266 if (statusCode == 200) {
267 foundVideoUrl(videoToken, definitionCode);
270 // try next (lower quality) definition
272 QStringList definitionNames = VideoDefinition::getDefinitionNames();
273 int currentIndex = definitionNames.indexOf(currentDefinition);
274 int previousIndex = 0;
275 if (currentIndex > 0) {
276 previousIndex = currentIndex - 1;
278 if (previousIndex > 0) {
279 QString nextDefinitionName = definitionNames.at(previousIndex);
280 findVideoUrl(nextDefinitionName);
282 foundVideoUrl(videoToken, 18);
286 QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
287 int currentIndex = definitionCodes.indexOf(definitionCode);
288 int previousIndex = 0;
289 if (currentIndex > 0) {
290 previousIndex = currentIndex - 1;
291 int definitionCode = definitionCodes.at(previousIndex);
292 if (definitionCode == 18) {
293 // This is assumed always available
294 foundVideoUrl(videoToken, 18);
296 findVideoUrl(definitionCode);
300 foundVideoUrl(videoToken, 18);
306 void Video::findVideoUrl(int definitionCode) {
307 this->definitionCode = definitionCode;
309 QUrl videoUrl = QUrl(QString(
310 "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
311 ).arg(videoId, videoToken, QString::number(definitionCode)));
313 QObject *reply = The::http()->head(videoUrl);
314 connect(reply, SIGNAL(finished(QNetworkReply*)), SLOT(gotHeadHeaders(QNetworkReply*)));
315 // connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
317 // see you in gotHeadHeaders()