From 647888ca9b6ae746834bb7c36547006fb9d909d5 Mon Sep 17 00:00:00 2001 From: Flavio Tordini Date: Sun, 11 Oct 2009 22:50:36 +0200 Subject: [PATCH] When a video token cannot be obtained from the getInfo YT service, fallback to scraping the webpage. --- src/video.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ src/video.h | 1 + 2 files changed, 43 insertions(+) diff --git a/src/video.cpp b/src/video.cpp index 0fb30b3..3345445 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -65,6 +65,17 @@ void Video::gotVideoInfo(QByteArray data) { // on regexp failure, stop and report error if (!match || re.numCaptures() < 1) { + + // Don't panic! We have a plan B. + + // get the youtube video webpage + QObject *reply = The::http()->get(webpage().toString()); + connect(reply, SIGNAL(data(QByteArray)), SLOT(scrapWebPage(QByteArray))); + connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*))); + + // see you in scrapWebPage(QByteArray) + + /* qDebug() << videoInfo; re = QRegExp("^.*&reason=([^&]+).*$"); match = re.exactMatch(videoInfo); @@ -78,8 +89,11 @@ void Video::gotVideoInfo(QByteArray data) { } if (mainWindow) mainWindow->statusBar()->showMessage(errorMessage); emit errorStreamUrl(errorMessage); + } else emit errorStreamUrl("Error parsing video info"); + */ + return; } @@ -101,3 +115,31 @@ void Video::gotVideoInfo(QByteArray data) { void Video::errorVideoInfo(QNetworkReply *reply) { emit errorStreamUrl(tr("Network error: %1 for %2").arg(reply->errorString(), reply->url().toString())); } + +void Video::scrapWebPage(QByteArray data) { + + QString videoHTML = QString::fromUtf8(data); + QRegExp re(".*, \"t\": \"([^\"]+)\".*"); + bool match = re.exactMatch(videoHTML); + + // on regexp failure, stop and report error + if (!match || re.numCaptures() < 1) { + emit errorStreamUrl("Error parsing video page"); + return; + } + + QString videoToken = re.cap(1); + // FIXME proper decode + videoToken = videoToken.replace("%3D", "="); + qDebug() << "token" << videoToken; + + QUrl videoUrl = QUrl(QString("http://www.youtube.com/get_video?video_id=") + .append(videoId) + .append("&t=").append(videoToken) + .append("&eurl=&el=detailpage&ps=default&fmt=18")); + + m_streamUrl = videoUrl; + + emit gotStreamUrl(videoUrl); + +} diff --git a/src/video.h b/src/video.h index 974f49b..bdd555e 100644 --- a/src/video.h +++ b/src/video.h @@ -57,6 +57,7 @@ signals: private slots: void gotVideoInfo(QByteArray); void errorVideoInfo(QNetworkReply*); + void scrapWebPage(QByteArray); private: void scrapeStreamUrl(); -- 2.39.5