2 #include "networkaccess.h"
4 #include "videodefinition.h"
10 Video::Video() : m_duration(0),
14 loadingStreamUrl(false)
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;
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)));
41 void Video::setThumbnail(QByteArray bytes) {
42 m_thumbnail = QImage::fromData(bytes);
46 const QImage Video::thumbnail() const {
50 void Video::loadStreamUrl() {
51 if (loadingStreamUrl) {
52 qDebug() << "Already loading stream URL for" << this->title();
55 loadingStreamUrl = true;
57 // https://develop.participatoryculture.org/trac/democracy/browser/trunk/tv/portable/flashscraper.py
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;
75 void Video::getVideoInfo() {
76 static const QStringList elTypes = QStringList() << "&el=embedded" << "&el=vevo" << "&el=detailpage" << "";
78 if (elIndex > elTypes.size() - 1) {
79 loadingStreamUrl = false;
80 emit errorStreamUrl("Cannot get video info");
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)
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)));
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*)));
102 // see you in gotVideoInfo...
106 void Video::gotVideoInfo(QByteArray data) {
107 QString videoInfo = QString::fromUtf8(data);
109 // qDebug() << "videoInfo" << videoInfo;
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
121 QString videoToken = re.cap(1);
122 while (videoToken.contains('%'))
123 videoToken = QByteArray::fromPercentEncoding(videoToken.toAscii());
124 // qDebug() << "videoToken" << videoToken;
125 this->videoToken = videoToken;
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
138 QString fmtUrlMap = re.cap(1);
139 fmtUrlMap = QByteArray::fromPercentEncoding(fmtUrlMap.toUtf8());
142 QString definitionName = settings.value("definition").toString();
143 int definitionCode = VideoDefinition::getDefinitionCode(definitionName);
145 // qDebug() << "fmtUrlMap" << fmtUrlMap;
146 QStringList formatUrls = fmtUrlMap.split(",", QString::SkipEmptyParts);
147 QHash<int, QString> urlMap;
148 foreach(QString formatUrl, formatUrls) {
149 // formatUrl = QByteArray::fromPercentEncoding(formatUrl.toUtf8());
150 qDebug() << "formatUrl" << formatUrl;
151 QStringList urlParams = formatUrl.split("&", QString::SkipEmptyParts);
152 // qDebug() << "urlParams" << urlParams;
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());
166 if (format == -1 || url.isNull()) continue;
168 if (format == definitionCode) {
169 qDebug() << "Found format" << definitionCode;
170 QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
171 m_streamUrl = videoUrl;
172 this->definitionCode = definitionCode;
173 emit gotStreamUrl(videoUrl);
174 loadingStreamUrl = false;
178 urlMap.insert(format, url);
181 QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
182 int currentIndex = definitionCodes.indexOf(definitionCode);
183 int previousIndex = 0;
184 while (currentIndex >= 0) {
185 previousIndex = currentIndex - 1;
186 if (previousIndex < 0) previousIndex = 0;
187 int definitionCode = definitionCodes.at(previousIndex);
188 if (urlMap.contains(definitionCode)) {
189 qDebug() << "Found format" << definitionCode;
190 QString url = urlMap.value(definitionCode);
191 QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
192 m_streamUrl = videoUrl;
193 this->definitionCode = definitionCode;
194 emit gotStreamUrl(videoUrl);
195 loadingStreamUrl = false;
201 emit errorStreamUrl(tr("Cannot get video stream for %1").arg(m_webpage.toString()));
205 void Video::foundVideoUrl(QString videoToken, int definitionCode) {
206 // qDebug() << "foundVideoUrl" << videoToken << definitionCode;
208 QUrl videoUrl = QUrl(QString(
209 "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
210 ).arg(videoId, videoToken, QString::number(definitionCode)));
212 m_streamUrl = videoUrl;
213 loadingStreamUrl = false;
214 emit gotStreamUrl(videoUrl);
217 void Video::errorVideoInfo(QNetworkReply *reply) {
218 loadingStreamUrl = false;
219 emit errorStreamUrl(tr("Network error: %1 for %2").arg(reply->errorString(), reply->url().toString()));
222 void Video::scrapeWebPage(QByteArray data) {
224 QString videoHTML = QString::fromUtf8(data);
225 QRegExp re(".*, \"t\": \"([^\"]+)\".*");
226 bool match = re.exactMatch(videoHTML);
228 // on regexp failure, stop and report error
229 if (!match || re.numCaptures() < 1) {
230 emit errorStreamUrl("Error parsing video page");
231 loadingStreamUrl = false;
235 QString videoToken = re.cap(1);
236 // FIXME proper decode
237 videoToken = videoToken.replace("%3D", "=");
239 // we'll need this in gotHeadHeaders()
240 this->videoToken = videoToken;
242 // qDebug() << "token" << videoToken;
245 QString definitionName = settings.value("definition").toString();
246 int definitionCode = VideoDefinition::getDefinitionCode(definitionName);
247 if (definitionCode == 18) {
248 // This is assumed always available
249 foundVideoUrl(videoToken, 18);
251 findVideoUrl(definitionCode);
256 void Video::gotHeadHeaders(QNetworkReply* reply) {
257 int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
258 // qDebug() << "gotHeaders" << statusCode;
259 if (statusCode == 200) {
260 foundVideoUrl(videoToken, definitionCode);
263 // try next (lower quality) definition
265 QStringList definitionNames = VideoDefinition::getDefinitionNames();
266 int currentIndex = definitionNames.indexOf(currentDefinition);
267 int previousIndex = 0;
268 if (currentIndex > 0) {
269 previousIndex = currentIndex - 1;
271 if (previousIndex > 0) {
272 QString nextDefinitionName = definitionNames.at(previousIndex);
273 findVideoUrl(nextDefinitionName);
275 foundVideoUrl(videoToken, 18);
279 QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
280 int currentIndex = definitionCodes.indexOf(definitionCode);
281 int previousIndex = 0;
282 if (currentIndex > 0) {
283 previousIndex = currentIndex - 1;
284 int definitionCode = definitionCodes.at(previousIndex);
285 if (definitionCode == 18) {
286 // This is assumed always available
287 foundVideoUrl(videoToken, 18);
289 findVideoUrl(definitionCode);
293 foundVideoUrl(videoToken, 18);
299 void Video::findVideoUrl(int definitionCode) {
300 this->definitionCode = definitionCode;
302 QUrl videoUrl = QUrl(QString(
303 "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
304 ).arg(videoId, videoToken, QString::number(definitionCode)));
306 QObject *reply = The::http()->head(videoUrl);
307 connect(reply, SIGNAL(finished(QNetworkReply*)), SLOT(gotHeadHeaders(QNetworkReply*)));
308 // connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
310 // see you in gotHeadHeaders()