]> git.sur5r.net Git - minitube/blob - src/video.cpp
recent keywords avigable with keyboard
[minitube] / src / video.cpp
1 #include "video.h"
2 #include "networkaccess.h"
3 #include <QtNetwork>
4
5 namespace The {
6     NetworkAccess* http();
7 }
8
9 Video::Video() : m_thumbnailUrls(QList<QUrl>()) {
10     m_duration = 0;
11     m_viewCount = -1;
12 }
13
14 void Video::preloadThumbnail() {
15     if (m_thumbnailUrls.isEmpty()) return;
16     QObject *reply = The::http()->get(m_thumbnailUrls.first());
17     connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
18 }
19
20 void Video::setThumbnail(QByteArray bytes) {
21     m_thumbnail = QImage::fromData(bytes);
22     emit gotThumbnail();
23 }
24
25 const QImage Video::thumbnail() const {
26     return m_thumbnail;
27 }
28
29 void Video::scrapeStreamUrl() {
30
31     // https://develop.participatoryculture.org/trac/democracy/browser/trunk/tv/portable/flashscraper.py
32
33     QUrl webpage = m_webpage;
34     // qDebug() << webpage.toString();
35
36     // Get Video ID
37     // youtube-dl line 428
38     // QRegExp re("^((?:http://)?(?:\\w+\\.)?youtube\\.com/(?:(?:v/)|(?:(?:watch(?:\\.php)?)?\\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$");
39     QRegExp re("^http://www\\.youtube\\.com/watch\\?v=([0-9A-Za-z_-]+)$");
40     bool match = re.exactMatch(webpage.toString());
41     if (!match || re.numCaptures() < 1) {
42         emit errorStreamUrl();
43         return;
44     }
45     videoId = re.cap(1);
46     // if (!videoId) return false;
47     // qDebug() << videoId;
48
49     // Get Video Token
50     QUrl normalizedUrl = QUrl(QString("http://www.youtube.com/get_video_info?video_id=")
51                               .append(videoId).append("&el=embedded&ps=default&eurl="));
52
53     QObject *reply = The::http()->get(normalizedUrl);
54     connect(reply, SIGNAL(data(QByteArray)), SLOT(gotVideoInfo(QByteArray)));
55
56     // see you in gotVideoInfo...
57 }
58
59 void  Video::gotVideoInfo(QByteArray data) {
60     QString videoInfo = QString::fromUtf8(data);
61
62     QRegExp re = QRegExp("^.*&token=([^&]+).*$");
63     bool match = re.exactMatch(videoInfo);
64
65     // on regexp failure, stop and report error
66     if (!match || re.numCaptures() < 1) {
67         qDebug() << videoInfo;
68         re = QRegExp("^.*&reason=([^&]+).*$");
69         match = re.exactMatch(videoInfo);
70         if (match) {
71             // report the error in the status bar
72             QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
73             QString errorMessage = QUrl::fromEncoded(re.cap(1).toUtf8()).toString().replace("+", " ");
74             int indexOfTag = errorMessage.indexOf("<");
75             if (indexOfTag != -1) {
76                 errorMessage = errorMessage.left(indexOfTag);
77             }
78             if (mainWindow) mainWindow->statusBar()->showMessage(errorMessage);
79         }
80         emit errorStreamUrl();
81         return;
82     }
83
84     QString videoToken = re.cap(1);
85     // FIXME proper decode
86     videoToken = videoToken.replace("%3D", "=");
87     // qDebug() << "token" << videoToken;
88
89     QUrl videoUrl = QUrl(QString("http://www.youtube.com/get_video?video_id=")
90                          .append(videoId)
91                          .append("&t=").append(videoToken)
92                          .append("&eurl=&el=embedded&ps=default&fmt=18"));
93
94     m_streamUrl = videoUrl;
95
96     emit gotStreamUrl(videoUrl);
97 }