Changeset 170 for smplayer/trunk/src/youtube/retrieveyoutubeurl.cpp
- Timestamp:
- Oct 9, 2014, 2:54:21 PM (11 years ago)
- Location:
- smplayer/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
smplayer/trunk
- Property svn:mergeinfo changed
/smplayer/vendor/current merged: 168
- Property svn:mergeinfo changed
-
smplayer/trunk/src/youtube/retrieveyoutubeurl.cpp
r165 r170 32 32 33 33 QString RetrieveYoutubeUrl::user_agent; 34 bool RetrieveYoutubeUrl::use_https_main = false; 35 bool RetrieveYoutubeUrl::use_https_vi = false; 34 36 35 37 RetrieveYoutubeUrl::RetrieveYoutubeUrl( QObject* parent ) : QObject(parent) … … 45 47 46 48 void RetrieveYoutubeUrl::fetchPage(const QString & url) { 47 QString agent = user_agent; 48 if (agent.isEmpty()) agent = "Mozilla/5.0 (X11; Linux x86_64; rv:5.0.1) Gecko/20100101 Firefox/5.0.1"; 49 qDebug("RetrieveYoutubeUrl::fetchPage: user agent: %s", agent.toLatin1().constData()); 49 qDebug("RetrieveYoutubeUrl::fetchPage: url: %s", url.toUtf8().constData()); 50 qDebug("RetrieveYoutubeUrl::fetchPage: user agent: '%s'", user_agent.toLatin1().constData()); 50 51 51 52 QNetworkRequest req(url); 52 req.setRawHeader("User-Agent", agent.toLatin1()); 53 req.setRawHeader("User-Agent", user_agent.toLatin1()); 54 req.setRawHeader("Accept-Language", "en-us,en;q=0.5"); 53 55 reply = manager->get(req); 54 56 connect(reply, SIGNAL(finished()), this, SLOT(gotResponse())); … … 63 65 64 66 #ifdef YT_GET_VIDEOINFO 65 void RetrieveYoutubeUrl::fetchVideoInfoPage() { 66 QString url = QString("http://www.youtube.com/get_video_info?el=detailpage&ps=default&eurl=&gl=US&hl=en&video_id=%1").arg(video_id); 67 //qDebug("RetrieveYoutubeUrl::fetchVideoInfoPage: url: %s", url.toUtf8().constData()); 67 void RetrieveYoutubeUrl::fetchVideoInfoPage(QString url) { 68 if (url.isEmpty()) { 69 QString scheme = use_https_vi ? "https" : "http"; 70 url = QString("%2://www.youtube.com/get_video_info?el=detailpage&ps=default&eurl=&gl=US&hl=en&video_id=%1").arg(video_id).arg(scheme); 71 } 72 qDebug("RetrieveYoutubeUrl::fetchVideoInfoPage: url: %s...", url.left(20).toUtf8().constData()); 73 74 qDebug("RetrieveYoutubeUrl::fetchPage: user agent: '%s'", user_agent.toLatin1().constData()); 68 75 69 76 YTSig::check(url); 70 77 QNetworkRequest req(url); 71 78 req.setRawHeader("User-Agent", user_agent.toLatin1()); 79 req.setRawHeader("Accept-Language", "en-us,en;q=0.5"); 72 80 reply = manager->get(req); 73 81 connect(reply, SIGNAL(finished()), this, SLOT(gotVideoInfoResponse())); … … 82 90 83 91 void RetrieveYoutubeUrl::gotResponse() { 92 qDebug("RetrieveYoutubeUrl::gotResponse"); 93 84 94 QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); 85 95 … … 97 107 } 98 108 } else { 109 qDebug("RetrieveYoutubeUrl::gotResponse: error %d: '%s'", (int)reply->error(), reply->errorString().toUtf8().constData()); 99 110 emit errorOcurred((int)reply->error(), reply->errorString()); 100 111 return; … … 109 120 QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); 110 121 111 if (reply->error() != QNetworkReply::NoError) { 122 if (reply->error() == QNetworkReply::NoError) { 123 int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); 124 qDebug("RetrieveYoutubeUrl::gotVideoInfoResponse: status: %d", status); 125 switch (status) { 126 case 301: 127 case 302: 128 case 307: 129 QString r_url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().toString(); 130 //qDebug("RetrieveYoutubeUrl::gotVideoInfoResponse: redirected: %s", r_url.toLatin1().constData()); 131 fetchVideoInfoPage(r_url); 132 return; 133 } 134 } else { 135 qDebug("RetrieveYoutubeUrl::gotVideoInfoResponse: error %d: '%s'", (int)reply->error(), reply->errorString().toUtf8().constData()); 112 136 emit errorOcurred((int)reply->error(), reply->errorString()); 113 137 return; … … 136 160 137 161 QString player; 138 QRegExp rxplayer("html5player-([\\d,\\w,-]+)\\ .js");162 QRegExp rxplayer("html5player-([\\d,\\w,-]+)\\\\"); 139 163 if (rxplayer.indexIn(replyString) != -1) { 140 164 player = rxplayer.cap(1); … … 142 166 } 143 167 168 QString fmtArray; 144 169 QRegExp regex("\\\"url_encoded_fmt_stream_map\\\"\\s*:\\s*\\\"([^\\\"]*)"); 145 regex.indexIn(replyString); 146 QString fmtArray = regex.cap(1); 170 if (regex.indexIn(replyString) != -1) { 171 fmtArray = regex.cap(1); 172 } 173 174 #ifdef YT_DASH_SUPPORT 175 QRegExp regex2("\\\"adaptive_fmts\\\"\\s*:\\s*\\\"([^\\\"]*)"); 176 if (regex2.indexIn(replyString) != -1) { 177 if (!fmtArray.isEmpty()) fmtArray += ","; 178 fmtArray += regex2.cap(1); 179 } 180 #endif 181 147 182 fmtArray = sanitizeForUnicodePoint(fmtArray); 148 183 fmtArray.replace(QRegExp("\\\\(.)"), "\\1"); … … 216 251 q->removeAllQueryItems("type"); 217 252 253 if (!q->hasQueryItem("ratebypass")) q->addQueryItem("ratebypass", "yes"); 254 218 255 if ((q->hasQueryItem("itag")) && (q->hasQueryItem("signature"))) { 219 256 QString itag = q->queryItemValue("itag"); 220 q->removeAllQueryItems("itag"); // Remove duplicated itag 221 q->addQueryItem("itag", itag); 257 258 // Remove duplicated queries 259 QPair <QString,QString> item; 260 QList<QPair<QString, QString> > items = q->queryItems(); 261 foreach(item, items) { 262 q->removeAllQueryItems(item.first); 263 q->addQueryItem(item.first, item.second); 264 } 265 222 266 #if QT_VERSION >= 0x050000 223 267 line.setQuery(q->query(QUrl::FullyDecoded)); 224 268 #endif 269 270 #ifdef YT_GET_VIDEOINFO 271 if (!line.toString().startsWith("https")) { 272 urlMap[itag.toInt()] = line.toString(); 273 } 274 #else 225 275 urlMap[itag.toInt()] = line.toString(); 226 //qDebug("line: %s", line.toString().toLatin1().constData()); 276 #endif 277 278 //qDebug("itag: %s line: %s", itag.toLatin1().constData(), line.toString().toLatin1().constData()); 227 279 } 228 280 } … … 255 307 emit gotUrls(urlMap); 256 308 emit gotPreferredUrl(p_url); 309 #ifdef YT_GET_VIDEOINFO 310 emit gotVideoInfo(urlMap, url_title, video_id); 311 #endif 257 312 } else { 258 313 emit gotEmptyList(); … … 318 373 QString r; 319 374 QString ID = getVideoID(url); 320 if (!ID.isEmpty()) r = "http://www.youtube.com/watch?v=" + ID; 375 if (!ID.isEmpty()) { 376 QString scheme = use_https_main ? "https" : "http"; 377 r = scheme + "://www.youtube.com/watch?v=" + ID; 378 } 321 379 return r; 322 380 } … … 329 387 QUrlQuery all; 330 388 all.setQuery(text); 331 QByteArray fmtArray = all.queryItemValue("url_encoded_fmt_stream_map").toLatin1();332 389 #else 333 390 QUrl all; 334 391 all.setEncodedQuery(text); 335 QByteArray fmtArray = all.queryItemValue("url_encoded_fmt_stream_map").toLatin1();336 392 #endif 393 394 QByteArray fmtArray; 395 fmtArray = all.queryItemValue("url_encoded_fmt_stream_map").toLatin1(); 396 397 #ifdef YT_DASH_SUPPORT 398 if (!fmtArray.isEmpty()) fmtArray += ","; 399 fmtArray += all.queryItemValue("adaptive_fmts").toLatin1(); 400 #endif 337 401 338 402 /* … … 400 464 q->removeAllQueryItems("fallback_host"); 401 465 q->removeAllQueryItems("type"); 466 467 if (!q->hasQueryItem("ratebypass")) q->addQueryItem("ratebypass", "yes"); 468 402 469 if ((q->hasQueryItem("itag")) && (q->hasQueryItem("signature"))) { 403 470 QString itag = q->queryItemValue("itag"); 404 q->removeAllQueryItems("itag"); // Remove duplicated itag 405 q->addQueryItem("itag", itag); 471 472 // Remove duplicated queries 473 QPair <QString,QString> item; 474 QList<QPair<QString, QString> > items = q->queryItems(); 475 foreach(item, items) { 476 q->removeAllQueryItems(item.first); 477 q->addQueryItem(item.first, item.second); 478 } 479 406 480 #if QT_VERSION >= 0x050000 407 481 line.setQuery(q->query(QUrl::FullyDecoded)); 408 482 #endif 409 483 urlMap[itag.toInt()] = line.toString(); 410 //qDebug(" line: %s", line.toString().toLatin1().constData());484 //qDebug("itag: %s line: %s", itag.toLatin1().constData(), line.toString().toLatin1().constData()); 411 485 } 412 486 } … … 431 505 emit gotUrls(urlMap); 432 506 emit gotPreferredUrl(p_url); 507 #ifdef YT_GET_VIDEOINFO 508 emit gotVideoInfo(urlMap, url_title, video_id); 509 #endif 433 510 } else { 434 511 emit gotEmptyList(); … … 506 583 return p_url; 507 584 } 585 586 #ifdef YT_DASH_SUPPORT 587 QString RetrieveYoutubeUrl::findBestAudio(const QMap<int, QString>& urlMap) { 588 QString url; 589 590 url = urlMap.value(DASH_AUDIO_MP4_256, QString()); 591 if (!url.isEmpty()) return url; 592 593 url = urlMap.value(DASH_AUDIO_WEBM_192, QString()); 594 if (!url.isEmpty()) return url; 595 596 url = urlMap.value(DASH_AUDIO_MP4_128, QString()); 597 if (!url.isEmpty()) return url; 598 599 url = urlMap.value(DASH_AUDIO_WEBM_128, QString()); 600 if (!url.isEmpty()) return url; 601 602 url = urlMap.value(DASH_AUDIO_MP4_48, QString()); 603 if (!url.isEmpty()) return url; 604 605 return url; 606 } 607 #endif 508 608 509 609 QString RetrieveYoutubeUrl::sanitizeForUnicodePoint(QString string) {
Note:
See TracChangeset
for help on using the changeset viewer.