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(QString("Cannot get video id for %1").arg(webpage.toString()));
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)));
55 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
57 // see you in gotVideoInfo...
60 void Video::gotVideoInfo(QByteArray data) {
61 QString videoInfo = QString::fromUtf8(data);
63 QRegExp re = QRegExp("^.*&token=([^&]+).*$");
64 bool match = re.exactMatch(videoInfo);
66 // on regexp failure, stop and report error
67 if (!match || re.numCaptures() < 1) {
68 qDebug() << videoInfo;
69 re = QRegExp("^.*&reason=([^&]+).*$");
70 match = re.exactMatch(videoInfo);
72 // report the error in the status bar
73 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
74 QString errorMessage = QUrl::fromEncoded(re.cap(1).toUtf8()).toString().replace("+", " ");
75 int indexOfTag = errorMessage.indexOf("<");
76 if (indexOfTag != -1) {
77 errorMessage = errorMessage.left(indexOfTag);
79 if (mainWindow) mainWindow->statusBar()->showMessage(errorMessage);
80 emit errorStreamUrl(errorMessage);
82 emit errorStreamUrl("Error parsing video info");
86 QString videoToken = re.cap(1);
87 // FIXME proper decode
88 videoToken = videoToken.replace("%3D", "=");
89 // qDebug() << "token" << videoToken;
91 QUrl videoUrl = QUrl(QString("http://www.youtube.com/get_video?video_id=")
93 .append("&t=").append(videoToken)
94 .append("&eurl=&el=embedded&ps=default&fmt=18"));
96 m_streamUrl = videoUrl;
98 emit gotStreamUrl(videoUrl);
101 void Video::errorVideoInfo(QNetworkReply *reply) {
102 emit errorStreamUrl(tr("Network error: %1 for %2").arg(reply->errorString(), reply->url().toString()));