]> git.sur5r.net Git - minitube/blob - src/video.cpp
Imported Upstream version 1.9
[minitube] / src / video.cpp
1 #include "video.h"
2 #include "networkaccess.h"
3 #include <QtNetwork>
4 #include "videodefinition.h"
5
6 namespace The {
7     NetworkAccess* http();
8 }
9
10 Video::Video() : m_duration(0),
11 m_viewCount(-1),
12 definitionCode(0),
13 elIndex(0),
14 loadingStreamUrl(false)
15 { }
16
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_webpage = m_webpage;
23     cloneVideo->m_streamUrl = m_streamUrl;
24     cloneVideo->m_thumbnail = m_thumbnail;
25     cloneVideo->m_thumbnailUrls = m_thumbnailUrls;
26     cloneVideo->m_duration = m_duration;
27     cloneVideo->m_published = m_published;
28     cloneVideo->m_viewCount = m_viewCount;
29     cloneVideo->videoId = videoId;
30     cloneVideo->videoToken = videoToken;
31     cloneVideo->definitionCode = definitionCode;
32     return cloneVideo;
33 }
34
35 void Video::preloadThumbnail() {
36     if (m_thumbnailUrls.isEmpty()) return;
37     QObject *reply = The::http()->get(m_thumbnailUrls.first());
38     connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
39 }
40
41 void Video::setThumbnail(QByteArray bytes) {
42     m_thumbnail = QImage::fromData(bytes);
43     emit gotThumbnail();
44 }
45
46 const QImage Video::thumbnail() const {
47     return m_thumbnail;
48 }
49
50 void Video::loadStreamUrl() {
51     if (loadingStreamUrl) {
52         qDebug() << "Already loading stream URL for" << this->title();
53         return;
54     }
55     loadingStreamUrl = true;
56
57     // https://develop.participatoryculture.org/trac/democracy/browser/trunk/tv/portable/flashscraper.py
58
59     // Get Video ID
60     // youtube-dl line 428
61     // QRegExp re("^((?:http://)?(?:\\w+\\.)?youtube\\.com/(?:(?:v/)|(?:(?:watch(?:\\.php)?)?\\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$");
62     QRegExp re("^http://www\\.youtube\\.com/watch\\?v=([0-9A-Za-z_-]+).*");
63     bool match = re.exactMatch(m_webpage.toString());
64     if (!match || re.numCaptures() < 1) {
65         emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
66         loadingStreamUrl = false;
67         return;
68     }
69     videoId = re.cap(1);
70
71     getVideoInfo();
72
73 }
74
75 void  Video::getVideoInfo() {
76     static const QStringList elTypes = QStringList() << "&el=embedded" << "&el=vevo" << "&el=detailpage" << "";
77
78     if (elIndex > elTypes.size() - 1) {
79         loadingStreamUrl = false;
80         emit errorStreamUrl("Cannot get video info");
81         /*
82         // Don't panic! We have a plan B.
83         // get the youtube video webpage
84         qDebug() << "Scraping" << webpage().toString();
85         QObject *reply = The::http()->get(webpage().toString());
86         connect(reply, SIGNAL(data(QByteArray)), SLOT(scrapeWebPage(QByteArray)));
87         connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
88         // see you in scrapWebPage(QByteArray)
89         */
90         return;
91     }
92
93     // Get Video Token
94     QUrl videoInfoUrl = QUrl(QString(
95             "http://www.youtube.com/get_video_info?video_id=%1%2&ps=default&eurl=&gl=US&hl=en"
96             ).arg(videoId, elTypes.at(elIndex)));
97
98     QObject *reply = The::http()->get(videoInfoUrl);
99     connect(reply, SIGNAL(data(QByteArray)), SLOT(gotVideoInfo(QByteArray)));
100     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
101
102     // see you in gotVideoInfo...
103
104 }
105
106 void  Video::gotVideoInfo(QByteArray data) {
107     QString videoInfo = QString::fromUtf8(data);
108
109     // qDebug() << "videoInfo" << videoInfo;
110
111     // get video token
112     QRegExp re = QRegExp("^.*&token=([^&]+).*$");
113     bool match = re.exactMatch(videoInfo);
114     // handle regexp failure
115     if (!match || re.numCaptures() < 1) {
116         // Don't panic! We're gonna try another magic "el" param
117         elIndex++;
118         getVideoInfo();
119         return;
120     }
121     QString videoToken = re.cap(1);
122     while (videoToken.contains('%'))
123         videoToken = QByteArray::fromPercentEncoding(videoToken.toAscii());
124     // qDebug() << "videoToken" << videoToken;
125     this->videoToken = videoToken;
126
127     // get fmt_url_map
128     re = QRegExp("^.*&url_encoded_fmt_stream_map=([^&]+).*$");
129     match = re.exactMatch(videoInfo);
130     // handle regexp failure
131     if (!match || re.numCaptures() < 1) {
132         // Don't panic! We're gonna try another magic "el" param
133         elIndex++;
134         getVideoInfo();
135         return;
136     }
137
138     QString fmtUrlMap = re.cap(1);
139     fmtUrlMap = QByteArray::fromPercentEncoding(fmtUrlMap.toUtf8());
140
141     QSettings settings;
142     QString definitionName = settings.value("definition").toString();
143     int definitionCode = VideoDefinition::getDefinitionCode(definitionName);
144
145     // qDebug() << "fmtUrlMap" << fmtUrlMap;
146     QStringList formatUrls = fmtUrlMap.split(",", QString::SkipEmptyParts);
147     QHash<int, QString> urlMap;
148     foreach(QString formatUrl, formatUrls) {
149         // qDebug() << "formatUrl" << formatUrl;
150         QStringList urlParams = formatUrl.split("&", QString::SkipEmptyParts);
151         // qDebug() << "urlParams" << urlParams;
152
153         int format = -1;
154         QString url;
155         QString sig;
156         foreach(QString urlParam, urlParams) {
157             if (urlParam.startsWith("itag=")) {
158                 int separator = urlParam.indexOf("=");
159                 format = urlParam.mid(separator + 1).toInt();
160             } else if (urlParam.startsWith("url=")) {
161                 int separator = urlParam.indexOf("=");
162                 url = urlParam.mid(separator + 1);
163                 url = QByteArray::fromPercentEncoding(url.toUtf8());
164             } else if (urlParam.startsWith("sig=")) {
165                 int separator = urlParam.indexOf("=");
166                 sig = urlParam.mid(separator + 1);
167                 sig = QByteArray::fromPercentEncoding(sig.toUtf8());
168             }
169         }
170         if (format == -1 || url.isNull()) continue;
171
172         url += "&signature=" + sig;
173
174         if (format == definitionCode) {
175             qDebug() << "Found format" << definitionCode;
176             QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
177             m_streamUrl = videoUrl;
178             this->definitionCode = definitionCode;
179             emit gotStreamUrl(videoUrl);
180             loadingStreamUrl = false;
181             return;
182         }
183
184         urlMap.insert(format, url);
185     }
186
187     QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
188     int currentIndex = definitionCodes.indexOf(definitionCode);
189     int previousIndex = 0;
190     while (currentIndex >= 0) {
191         previousIndex = currentIndex - 1;
192         if (previousIndex < 0) previousIndex = 0;
193         int definitionCode = definitionCodes.at(previousIndex);
194         if (urlMap.contains(definitionCode)) {
195             qDebug() << "Found format" << definitionCode;
196             QString url = urlMap.value(definitionCode);
197             QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
198             m_streamUrl = videoUrl;
199             this->definitionCode = definitionCode;
200             emit gotStreamUrl(videoUrl);
201             loadingStreamUrl = false;
202             return;
203         }
204         currentIndex--;
205     }
206
207     emit errorStreamUrl(tr("Cannot get video stream for %1").arg(m_webpage.toString()));
208
209 }
210
211 void Video::foundVideoUrl(QString videoToken, int definitionCode) {
212     // qDebug() << "foundVideoUrl" << videoToken << definitionCode;
213
214     QUrl videoUrl = QUrl(QString(
215             "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
216             ).arg(videoId, videoToken, QString::number(definitionCode)));
217
218     m_streamUrl = videoUrl;
219     loadingStreamUrl = false;
220     emit gotStreamUrl(videoUrl);
221 }
222
223 void Video::errorVideoInfo(QNetworkReply *reply) {
224     loadingStreamUrl = false;
225     emit errorStreamUrl(tr("Network error: %1 for %2").arg(reply->errorString(), reply->url().toString()));
226 }
227
228 void Video::scrapeWebPage(QByteArray data) {
229
230     QString videoHTML = QString::fromUtf8(data);
231     QRegExp re(".*, \"t\": \"([^\"]+)\".*");
232     bool match = re.exactMatch(videoHTML);
233
234     // on regexp failure, stop and report error
235     if (!match || re.numCaptures() < 1) {
236         emit errorStreamUrl("Error parsing video page");
237         loadingStreamUrl = false;
238         return;
239     }
240
241     QString videoToken = re.cap(1);
242     // FIXME proper decode
243     videoToken = videoToken.replace("%3D", "=");
244
245     // we'll need this in gotHeadHeaders()
246     this->videoToken = videoToken;
247
248     // qDebug() << "token" << videoToken;
249
250     QSettings settings;
251     QString definitionName = settings.value("definition").toString();
252     int definitionCode = VideoDefinition::getDefinitionCode(definitionName);
253     if (definitionCode == 18) {
254         // This is assumed always available
255         foundVideoUrl(videoToken, 18);
256     } else {
257         findVideoUrl(definitionCode);
258     }
259
260 }
261
262 void Video::gotHeadHeaders(QNetworkReply* reply) {
263     int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
264     // qDebug() << "gotHeaders" << statusCode;
265     if (statusCode == 200) {
266         foundVideoUrl(videoToken, definitionCode);
267     } else {
268
269         // try next (lower quality) definition
270         /*
271         QStringList definitionNames = VideoDefinition::getDefinitionNames();
272         int currentIndex = definitionNames.indexOf(currentDefinition);
273         int previousIndex = 0;
274         if (currentIndex > 0) {
275             previousIndex = currentIndex - 1;
276         }
277         if (previousIndex > 0) {
278             QString nextDefinitionName = definitionNames.at(previousIndex);
279             findVideoUrl(nextDefinitionName);
280         } else {
281             foundVideoUrl(videoToken, 18);
282         }*/
283
284
285         QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
286         int currentIndex = definitionCodes.indexOf(definitionCode);
287         int previousIndex = 0;
288         if (currentIndex > 0) {
289             previousIndex = currentIndex - 1;
290             int definitionCode = definitionCodes.at(previousIndex);
291             if (definitionCode == 18) {
292                 // This is assumed always available
293                 foundVideoUrl(videoToken, 18);
294             } else {
295                 findVideoUrl(definitionCode);
296             }
297
298         } else {
299             foundVideoUrl(videoToken, 18);
300         }
301
302     }
303 }
304
305 void Video::findVideoUrl(int definitionCode) {
306     this->definitionCode = definitionCode;
307
308     QUrl videoUrl = QUrl(QString(
309             "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
310             ).arg(videoId, videoToken, QString::number(definitionCode)));
311
312     QObject *reply = The::http()->head(videoUrl);
313     connect(reply, SIGNAL(finished(QNetworkReply*)), SLOT(gotHeadHeaders(QNetworkReply*)));
314     // connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
315
316     // see you in gotHeadHeaders()
317
318 }