]> git.sur5r.net Git - minitube/blob - src/video.cpp
Better signature decrypting
[minitube] / src / video.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "video.h"
22 #include "networkaccess.h"
23 #include <QtNetwork>
24 #include "videodefinition.h"
25 #include "jsfunctions.h"
26
27 namespace The {
28 NetworkAccess* http();
29 }
30
31 Video::Video() : m_duration(0),
32     m_viewCount(-1),
33     definitionCode(0),
34     elIndex(0),
35     ageGate(false),
36     m_license(LicenseYouTube),
37     loadingStreamUrl(false),
38     loadingThumbnail(false)
39 { }
40
41 Video* Video::clone() {
42     Video* cloneVideo = new Video();
43     cloneVideo->m_title = m_title;
44     cloneVideo->m_description = m_description;
45     cloneVideo->m_author = m_author;
46     cloneVideo->m_userId = m_userId;
47     cloneVideo->m_webpage = m_webpage;
48     cloneVideo->m_streamUrl = m_streamUrl;
49     cloneVideo->m_thumbnail = m_thumbnail;
50     cloneVideo->m_thumbnailUrl = m_thumbnailUrl;
51     cloneVideo->m_mediumThumbnailUrl = m_mediumThumbnailUrl;
52     cloneVideo->m_duration = m_duration;
53     cloneVideo->m_published = m_published;
54     cloneVideo->m_viewCount = m_viewCount;
55     cloneVideo->videoId = videoId;
56     cloneVideo->videoToken = videoToken;
57     cloneVideo->definitionCode = definitionCode;
58     return cloneVideo;
59 }
60
61 void Video::setWebpage(QUrl webpage) {
62     m_webpage = webpage;
63
64     // Get Video ID
65     // youtube-dl line 428
66     // QRegExp re("^((?:http://)?(?:\\w+\\.)?youtube\\.com/(?:(?:v/)|(?:(?:watch(?:\\.php)?)?\\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$");
67     QRegExp re("^https?://www\\.youtube\\.com/watch\\?v=([0-9A-Za-z_-]+).*");
68     bool match = re.exactMatch(m_webpage.toString());
69     if (!match || re.numCaptures() < 1) {
70         qWarning() << QString("Cannot get video id for %1").arg(m_webpage.toString());
71         // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
72         // loadingStreamUrl = false;
73         return;
74     }
75     videoId = re.cap(1);
76 }
77
78 void Video::loadThumbnail() {
79     if (m_thumbnailUrl.isEmpty() || loadingThumbnail) return;
80     loadingThumbnail = true;
81     QObject *reply = The::http()->get(m_thumbnailUrl);
82     connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
83 }
84
85 void Video::setThumbnail(QByteArray bytes) {
86     loadingThumbnail = false;
87     m_thumbnail.loadFromData(bytes);
88     if (m_thumbnail.width() > 160)
89         m_thumbnail = m_thumbnail.scaledToWidth(160, Qt::SmoothTransformation);
90     emit gotThumbnail();
91 }
92
93 void Video::loadMediumThumbnail() {
94     if (m_mediumThumbnailUrl.isEmpty()) return;
95     QObject *reply = The::http()->get(m_mediumThumbnailUrl);
96     connect(reply, SIGNAL(data(QByteArray)), SIGNAL(gotMediumThumbnail(QByteArray)));
97 }
98
99 void Video::loadStreamUrl() {
100     if (loadingStreamUrl) {
101         qDebug() << "Already loading stream URL for" << this->title();
102         return;
103     }
104     loadingStreamUrl = true;
105     elIndex = 0;
106     ageGate = false;
107
108     getVideoInfo();
109 }
110
111 void  Video::getVideoInfo() {
112     static const QStringList elTypes = QStringList() << "&el=embedded" << "&el=detailpage" << "&el=vevo" << "";
113
114     QUrl videoInfoUrl;
115
116     if (elIndex == elTypes.size()) {
117         // qDebug() << "Trying special embedded el param";
118         videoInfoUrl = QUrl("http://www.youtube.com/get_video_info");
119         videoInfoUrl.addQueryItem("video_id", videoId);
120         videoInfoUrl.addQueryItem("el", "embedded");
121         videoInfoUrl.addQueryItem("gl", "US");
122         videoInfoUrl.addQueryItem("hl", "en");
123         videoInfoUrl.addQueryItem("eurl", "https://youtube.googleapis.com/v/" + videoId);
124         videoInfoUrl.addQueryItem("asv", "3");
125         videoInfoUrl.addQueryItem("sts", "1588");
126     } else if (elIndex > elTypes.size() - 1) {
127         qWarning() << "Cannot get video info";
128         loadingStreamUrl = false;
129         emit errorStreamUrl("Cannot get video info");
130         return;
131     } else {
132         // qDebug() << "Trying el param:" << elTypes.at(elIndex) << elIndex;
133         videoInfoUrl = QUrl(QString(
134                                 "http://www.youtube.com/get_video_info?video_id=%1%2&ps=default&eurl=&gl=US&hl=en"
135                                 ).arg(videoId, elTypes.at(elIndex)));
136     }
137
138     QObject *reply = The::http()->get(videoInfoUrl);
139     connect(reply, SIGNAL(data(QByteArray)), SLOT(gotVideoInfo(QByteArray)));
140     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
141
142     // see you in gotVideoInfo...
143 }
144
145 void  Video::gotVideoInfo(QByteArray data) {
146     QString videoInfo = QString::fromUtf8(data);
147     // qDebug() << "videoInfo" << videoInfo;
148
149     // get video token
150     QRegExp re = QRegExp("^.*&token=([^&]+).*$");
151     bool match = re.exactMatch(videoInfo);
152     // handle regexp failure
153     if (!match || re.numCaptures() < 1) {
154         // qDebug() << "Cannot get token. Trying next el param";
155         // Don't panic! We're gonna try another magic "el" param
156         elIndex++;
157         getVideoInfo();
158         return;
159     }
160
161     QString videoToken = re.cap(1);
162     while (videoToken.contains('%'))
163         videoToken = QByteArray::fromPercentEncoding(videoToken.toAscii());
164     // qDebug() << "videoToken" << videoToken;
165     this->videoToken = videoToken;
166
167     // get fmt_url_map
168     re = QRegExp("^.*&url_encoded_fmt_stream_map=([^&]+).*$");
169     match = re.exactMatch(videoInfo);
170     // handle regexp failure
171     if (!match || re.numCaptures() < 1) {
172         // qDebug() << "Cannot get urlMap. Trying next el param";
173         // Don't panic! We're gonna try another magic "el" param
174         elIndex++;
175         getVideoInfo();
176         return;
177     }
178
179     // qDebug() << "Got token and urlMap" << elIndex;
180
181     QString fmtUrlMap = re.cap(1);
182     fmtUrlMap = QByteArray::fromPercentEncoding(fmtUrlMap.toUtf8());
183     parseFmtUrlMap(fmtUrlMap);
184 }
185
186 void Video::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
187     QSettings settings;
188     QString definitionName = settings.value("definition", "360p").toString();
189     int definitionCode = VideoDefinition::getDefinitionCode(definitionName);
190
191     // qDebug() << "fmtUrlMap" << fmtUrlMap;
192     QStringList formatUrls = fmtUrlMap.split(',', QString::SkipEmptyParts);
193     QHash<int, QString> urlMap;
194     foreach(QString formatUrl, formatUrls) {
195         // qDebug() << "formatUrl" << formatUrl;
196         QStringList urlParams = formatUrl.split('&', QString::SkipEmptyParts);
197         // qDebug() << "urlParams" << urlParams;
198
199         int format = -1;
200         QString url;
201         QString sig;
202         foreach(QString urlParam, urlParams) {
203             // qWarning() << urlParam;
204             if (urlParam.startsWith("itag=")) {
205                 int separator = urlParam.indexOf("=");
206                 format = urlParam.mid(separator + 1).toInt();
207                 qWarning() << "itag" << format;
208
209             } else if (urlParam.startsWith("url=")) {
210                 int separator = urlParam.indexOf("=");
211                 url = urlParam.mid(separator + 1);
212                 url = QByteArray::fromPercentEncoding(url.toUtf8());
213             } else if (urlParam.startsWith("sig=")) {
214                 int separator = urlParam.indexOf("=");
215                 sig = urlParam.mid(separator + 1);
216                 sig = QByteArray::fromPercentEncoding(sig.toUtf8());
217             } else if (urlParam.startsWith("s=")) {
218                 if (fromWebPage || ageGate) {
219                     int separator = urlParam.indexOf("=");
220                     sig = urlParam.mid(separator + 1);
221                     sig = QByteArray::fromPercentEncoding(sig.toUtf8());
222                     if (ageGate)
223                         sig = JsFunctions::instance()->decryptAgeSignature(sig);
224                     else {
225                         sig = decryptSignature(sig);
226                         if (sig.isEmpty())
227                             sig = JsFunctions::instance()->decryptSignature(sig);
228                     }
229                 } else {
230                     // qDebug() << "Loading webpage";
231                     QUrl url("http://www.youtube.com/watch");
232                     url.addQueryItem("v", videoId);
233                     url.addQueryItem("gl", "US");
234                     url.addQueryItem("hl", "en");
235                     url.addQueryItem("has_verified", "1");
236                     QObject *reply = The::http()->get(url);
237                     connect(reply, SIGNAL(data(QByteArray)), SLOT(scrapeWebPage(QByteArray)));
238                     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
239                     // see you in scrapWebPage(QByteArray)
240                     return;
241                 }
242             }
243         }
244         if (format == -1 || url.isNull()) continue;
245
246         url += "&signature=" + sig;
247
248         if (!url.contains("ratebypass"))
249             url += "&ratebypass=yes";
250
251         // qWarning() << url;
252
253         if (format == definitionCode) {
254             qDebug() << "Found format" << definitionCode;
255             QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
256             m_streamUrl = videoUrl;
257             this->definitionCode = definitionCode;
258             emit gotStreamUrl(videoUrl);
259             loadingStreamUrl = false;
260             return;
261         }
262
263         urlMap.insert(format, url);
264     }
265
266     QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
267     int currentIndex = definitionCodes.indexOf(definitionCode);
268     int previousIndex = 0;
269     while (currentIndex >= 0) {
270         previousIndex = currentIndex - 1;
271         if (previousIndex < 0) previousIndex = 0;
272         int definitionCode = definitionCodes.at(previousIndex);
273         if (urlMap.contains(definitionCode)) {
274             qDebug() << "Found format" << definitionCode;
275             QString url = urlMap.value(definitionCode);
276             QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
277             m_streamUrl = videoUrl;
278             this->definitionCode = definitionCode;
279             emit gotStreamUrl(videoUrl);
280             loadingStreamUrl = false;
281             return;
282         }
283         currentIndex--;
284     }
285
286     emit errorStreamUrl(tr("Cannot get video stream for %1").arg(m_webpage.toString()));
287 }
288
289 void Video::foundVideoUrl(QString videoToken, int definitionCode) {
290     // qDebug() << "foundVideoUrl" << videoToken << definitionCode;
291
292     QUrl videoUrl = QUrl(QString(
293                              "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
294                              ).arg(videoId, videoToken, QString::number(definitionCode)));
295
296     m_streamUrl = videoUrl;
297     loadingStreamUrl = false;
298     emit gotStreamUrl(videoUrl);
299 }
300
301 void Video::errorVideoInfo(QNetworkReply *reply) {
302     loadingStreamUrl = false;
303     emit errorStreamUrl(tr("Network error: %1 for %2").arg(reply->errorString(), reply->url().toString()));
304 }
305
306 void Video::scrapeWebPage(QByteArray data) {
307     QString html = QString::fromUtf8(data);
308     // qWarning() << html;
309
310     if (html.contains("player-age-gate-content\"")) {
311         // qDebug() << "Found ageGate";
312         ageGate = true;
313         elIndex = 4;
314         getVideoInfo();
315         return;
316     }
317
318     QRegExp re(".*\"url_encoded_fmt_stream_map\":\\s+\"([^\"]+)\".*");
319     bool match = re.exactMatch(html);
320     // on regexp failure, stop and report error
321     if (!match || re.numCaptures() < 1) {
322         qWarning() << "Error parsing video page";
323         // emit errorStreamUrl("Error parsing video page");
324         // loadingStreamUrl = false;
325         elIndex++;
326         getVideoInfo();
327         return;
328     }
329     fmtUrlMap = re.cap(1);
330     fmtUrlMap.replace("\\u0026", "&");
331     // parseFmtUrlMap(fmtUrlMap, true);
332
333     QRegExp jsPlayerRe("\"assets\":.+\"js\":\\s*\"([^\"]+)\"");
334     if (jsPlayerRe.indexIn(html) != -1) {
335         QString jsPlayerUrl = jsPlayerRe.cap(1);
336         jsPlayerUrl.remove('\\');
337         jsPlayerUrl = "http:" + jsPlayerUrl;
338         qWarning() << "jsPlayerUrl" << jsPlayerUrl;
339         /*
340         QRegExp jsPlayerIdRe("-(.+)\\.js");
341         jsPlayerIdRe.indexIn(jsPlayerUrl);
342         QString jsPlayerId = jsPlayerRe.cap(1);
343         */
344         QObject *reply = The::http()->get(jsPlayerUrl);
345         connect(reply, SIGNAL(data(QByteArray)), SLOT(parseJsPlayer(QByteArray)));
346         connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
347     }
348 }
349
350 void Video::gotHeadHeaders(QNetworkReply* reply) {
351     int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
352     // qDebug() << "gotHeaders" << statusCode;
353     if (statusCode == 200) {
354         foundVideoUrl(videoToken, definitionCode);
355     } else {
356
357         // try next (lower quality) definition
358         /*
359         QStringList definitionNames = VideoDefinition::getDefinitionNames();
360         int currentIndex = definitionNames.indexOf(currentDefinition);
361         int previousIndex = 0;
362         if (currentIndex > 0) {
363             previousIndex = currentIndex - 1;
364         }
365         if (previousIndex > 0) {
366             QString nextDefinitionName = definitionNames.at(previousIndex);
367             findVideoUrl(nextDefinitionName);
368         } else {
369             foundVideoUrl(videoToken, 18);
370         }*/
371
372
373         QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
374         int currentIndex = definitionCodes.indexOf(definitionCode);
375         int previousIndex = 0;
376         if (currentIndex > 0) {
377             previousIndex = currentIndex - 1;
378             int definitionCode = definitionCodes.at(previousIndex);
379             if (definitionCode == 18) {
380                 // This is assumed always available
381                 foundVideoUrl(videoToken, 18);
382             } else {
383                 findVideoUrl(definitionCode);
384             }
385
386         } else {
387             foundVideoUrl(videoToken, 18);
388         }
389
390     }
391 }
392
393 void Video::parseJsPlayer(QByteArray bytes) {
394     QString js = QString::fromUtf8(bytes);
395
396     QRegExp funcNameRe("signature=([a-zA-Z0-9]+)");
397     if (funcNameRe.indexIn(js) == -1) {
398         qWarning() << "Cannot capture signature function name";
399         return;
400     }
401     sigFuncName = funcNameRe.cap(1);
402     captureFunction(sigFuncName, js);
403     qWarning() << sigFunctions;
404     parseFmtUrlMap(fmtUrlMap, true);
405 }
406
407 void Video::captureFunction(const QString &name, const QString &js) {
408     QRegExp funcRe("function\\s+" + name + "\\s*\\([a-zA-Z0-9,\\s]*\\)\\s*\\{[^\\}]+\\}");
409     if (funcRe.indexIn(js) == -1) {
410         qWarning() << "Cannot capture function" << name;
411         return;
412     }
413     QString func = funcRe.cap(0);
414     sigFunctions.insert(name, func);
415
416     // capture inner functions
417     QRegExp invokedFuncRe("[\\s=;\\(]([a-zA-Z0-9]+)\\s*\\([a-zA-Z0-9, ]+\\)");
418     int pos = name.length() + 9;
419     while ((pos = invokedFuncRe.indexIn(func, pos)) != -1) {
420         QString funcName = invokedFuncRe.cap(1);
421         if (!sigFunctions.contains(funcName))
422             captureFunction(funcName, js);
423         pos += invokedFuncRe.matchedLength();
424     }
425 }
426
427 QString Video::decryptSignature(const QString &s) {
428     QScriptEngine engine;
429     foreach (QString f, sigFunctions.values()) {
430         QScriptValue value = engine.evaluate(f);
431         if (value.isError())
432             qWarning() << "Error in" << f << value.toString();
433     }
434     QString js = sigFuncName + "('" + s + "');";
435     QScriptValue value = engine.evaluate(js);
436     if (value.isUndefined()) {
437         qWarning() << "Undefined result for" << js;
438         return QString();
439     }
440     if (value.isError()) {
441         qWarning() << "Error in" << js << value.toString();
442         return QString();
443     }
444     return value.toString();
445 }
446
447 void Video::findVideoUrl(int definitionCode) {
448     this->definitionCode = definitionCode;
449
450     QUrl videoUrl = QUrl(QString(
451                              "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
452                              ).arg(videoId, videoToken, QString::number(definitionCode)));
453
454     QObject *reply = The::http()->head(videoUrl);
455     connect(reply, SIGNAL(finished(QNetworkReply*)), SLOT(gotHeadHeaders(QNetworkReply*)));
456     // connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
457
458     // see you in gotHeadHeaders()
459 }
460
461 QString Video::formattedDuration() const {
462     QString format = m_duration > 3600 ? "h:mm:ss" : "m:ss";
463     return QTime().addSecs(m_duration).toString(format);
464 }