2 #include "networkaccess.h"
9 Video::Video() : m_thumbnailUrls(QList<QUrl>()) {
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)));
20 void Video::setThumbnail(QByteArray bytes) {
21 m_thumbnail = QImage::fromData(bytes);
25 const QImage Video::thumbnail() const {
29 void Video::scrapeStreamUrl() {
31 // https://develop.participatoryculture.org/trac/democracy/browser/trunk/tv/portable/flashscraper.py
33 QUrl webpage = m_webpage;
34 // qDebug() << webpage.toString();
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();
46 // if (!videoId) return false;
47 // qDebug() << videoId;
50 QUrl normalizedUrl = QUrl(QString("http://www.youtube.com/get_video_info?video_id=")
51 .append(videoId).append("&el=embedded&ps=default&eurl="));
53 QObject *reply = The::http()->get(normalizedUrl);
54 connect(reply, SIGNAL(data(QByteArray)), SLOT(gotVideoInfo(QByteArray)));
56 // see you in gotVideoInfo...
59 void Video::gotVideoInfo(QByteArray data) {
60 QString videoInfo = QString::fromUtf8(data);
62 QRegExp re = QRegExp("^.*&token=([^&]+).*$");
63 bool match = re.exactMatch(videoInfo);
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);
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);
78 if (mainWindow) mainWindow->statusBar()->showMessage(errorMessage);
80 emit errorStreamUrl();
84 QString videoToken = re.cap(1);
85 // FIXME proper decode
86 videoToken = videoToken.replace("%3D", "=");
87 // qDebug() << "token" << videoToken;
89 QUrl videoUrl = QUrl(QString("http://www.youtube.com/get_video?video_id=")
91 .append("&t=").append(videoToken)
92 .append("&eurl=&el=embedded&ps=default&fmt=18"));
94 m_streamUrl = videoUrl;
96 emit gotStreamUrl(videoUrl);