]> git.sur5r.net Git - minitube/blobdiff - src/video.cpp
Imported Upstream version 2.4
[minitube] / src / video.cpp
index 64cbf6dcfd174dbd49774f79ddd1dbd360202382..29b74b8125731e125886a0de2e4dc9a3beb99c90 100644 (file)
@@ -35,20 +35,20 @@ static const QString jsNameChars = "a-zA-Z0-9\\$_";
 
 Video::Video() : m_duration(0),
     m_viewCount(-1),
+    m_license(LicenseYouTube),
     definitionCode(0),
     elIndex(0),
     ageGate(false),
-    m_license(LicenseYouTube),
     loadingStreamUrl(false),
-    loadingThumbnail(false)
-}
+    loadingThumbnail(false) {
+}
 
 Video* Video::clone() {
     Video* cloneVideo = new Video();
     cloneVideo->m_title = m_title;
     cloneVideo->m_description = m_description;
-    cloneVideo->m_author = m_author;
-    cloneVideo->m_userId = m_userId;
+    cloneVideo->m_channelTitle = m_channelTitle;
+    cloneVideo->m_channelId = m_channelId;
     cloneVideo->m_webpage = m_webpage;
     cloneVideo->m_streamUrl = m_streamUrl;
     cloneVideo->m_thumbnail = m_thumbnail;
@@ -63,21 +63,26 @@ Video* Video::clone() {
     return cloneVideo;
 }
 
-void Video::setWebpage(QUrl webpage) {
-    m_webpage = webpage;
+const QString &Video::webpage() {
+    if (m_webpage.isEmpty() && !videoId.isEmpty())
+        m_webpage.append("https://www.youtube.com/watch?v=").append(videoId);
+    return m_webpage;
+}
+
+void Video::setWebpage(const QString &value) {
+    m_webpage = value;
 
     // Get Video ID
-    // youtube-dl line 428
-    // QRegExp re("^((?:http://)?(?:\\w+\\.)?youtube\\.com/(?:(?:v/)|(?:(?:watch(?:\\.php)?)?\\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$");
-    QRegExp re("^https?://www\\.youtube\\.com/watch\\?v=([0-9A-Za-z_-]+).*");
-    bool match = re.exactMatch(m_webpage.toString());
-    if (!match) {
-        qWarning() << QString("Cannot get video id for %1").arg(m_webpage.toString());
-        // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
-        // loadingStreamUrl = false;
-        return;
+    if (videoId.isEmpty()) {
+        QRegExp re(JsFunctions::instance()->videoIdRE());
+        if (re.indexIn(m_webpage) == -1) {
+            qWarning() << QString("Cannot get video id for %1").arg(m_webpage);
+            // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
+            // loadingStreamUrl = false;
+            return;
+        }
+        videoId = re.cap(1);
     }
-    videoId = re.cap(1);
 }
 
 void Video::loadThumbnail() {
@@ -89,6 +94,7 @@ void Video::loadThumbnail() {
 
 void Video::setThumbnail(QByteArray bytes) {
     loadingThumbnail = false;
+    m_thumbnail = QPixmap();
     m_thumbnail.loadFromData(bytes);
     if (m_thumbnail.width() > 160)
         m_thumbnail = m_thumbnail.scaledToWidth(160, Qt::SmoothTransformation);
@@ -120,7 +126,7 @@ void  Video::getVideoInfo() {
 
     if (elIndex == elTypes.size()) {
         // qDebug() << "Trying special embedded el param";
-        url = QUrl("http://www.youtube.com/get_video_info");
+        url = QUrl("https://www.youtube.com/get_video_info");
 
 #if QT_VERSION >= 0x050000
         {
@@ -147,7 +153,7 @@ void  Video::getVideoInfo() {
     } else {
         // qDebug() << "Trying el param:" << elTypes.at(elIndex) << elIndex;
         url = QUrl(QString(
-                       "http://www.youtube.com/get_video_info?video_id=%1%2&ps=default&eurl=&gl=US&hl=en"
+                       "https://www.youtube.com/get_video_info?video_id=%1%2&ps=default&eurl=&gl=US&hl=en"
                        ).arg(videoId, elTypes.at(elIndex)));
     }
 
@@ -163,28 +169,25 @@ void  Video::gotVideoInfo(QByteArray data) {
     // qDebug() << "videoInfo" << videoInfo;
 
     // get video token
-    QRegExp re = QRegExp("^.*&token=([^&]+).*$");
-    bool match = re.exactMatch(videoInfo);
-    // handle regexp failure
-    if (!match) {
-        // qDebug() << "Cannot get token. Trying next el param";
+    QRegExp videoTokeRE(JsFunctions::instance()->videoTokenRE());
+    if (videoTokeRE.indexIn(videoInfo) == -1) {
+        qDebug() << "Cannot get token. Trying next el param" << videoInfo << videoTokeRE.pattern();
         // Don't panic! We're gonna try another magic "el" param
         elIndex++;
         getVideoInfo();
         return;
     }
 
-    QString videoToken = re.cap(1);
+    QString videoToken = videoTokeRE.cap(1);
+    // qDebug() << "got token" << videoToken;
     while (videoToken.contains('%'))
         videoToken = QByteArray::fromPercentEncoding(videoToken.toLatin1());
     // qDebug() << "videoToken" << videoToken;
     this->videoToken = videoToken;
 
     // get fmt_url_map
-    re = QRegExp("^.*&url_encoded_fmt_stream_map=([^&]+).*$");
-    match = re.exactMatch(videoInfo);
-    // handle regexp failure
-    if (!match) {
+    QRegExp fmtMapRE(JsFunctions::instance()->videoInfoFmtMapRE());
+    if (fmtMapRE.indexIn(videoInfo) == -1) {
         // qDebug() << "Cannot get urlMap. Trying next el param";
         // Don't panic! We're gonna try another magic "el" param
         elIndex++;
@@ -194,7 +197,8 @@ void  Video::gotVideoInfo(QByteArray data) {
 
     // qDebug() << "Got token and urlMap" << elIndex;
 
-    QString fmtUrlMap = re.cap(1);
+    QString fmtUrlMap = fmtMapRE.cap(1);
+    // qDebug() << "got fmtUrlMap" << fmtUrlMap;
     fmtUrlMap = QByteArray::fromPercentEncoding(fmtUrlMap.toUtf8());
     parseFmtUrlMap(fmtUrlMap);
 }
@@ -241,7 +245,7 @@ void Video::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
                             sig = JsFunctions::instance()->decryptSignature(sig);
                     }
                 } else {
-                    // qDebug() << "Loading webpage";
+
                     QUrl url("http://www.youtube.com/watch");
 
 #if QT_VERSION >= 0x050000
@@ -257,6 +261,7 @@ void Video::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
                         u.setQuery(url);
                     }
 #endif
+                    // qDebug() << "Loading webpage" << url;
                     QObject *reply = The::http()->get(url);
                     connect(reply, SIGNAL(data(QByteArray)), SLOT(scrapeWebPage(QByteArray)));
                     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
@@ -275,7 +280,7 @@ void Video::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
         // qWarning() << url;
 
         if (format == definitionCode) {
-            qDebug() << "Found format" << definitionCode;
+            // qDebug() << "Found format" << definitionCode;
             QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
             m_streamUrl = videoUrl;
             this->definitionCode = definitionCode;
@@ -295,7 +300,7 @@ void Video::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
         if (previousIndex < 0) previousIndex = 0;
         int definitionCode = definitionCodes.at(previousIndex);
         if (urlMap.contains(definitionCode)) {
-            qDebug() << "Found format" << definitionCode;
+            // qDebug() << "Found format" << definitionCode;
             QString url = urlMap.value(definitionCode);
             QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
             m_streamUrl = videoUrl;
@@ -307,19 +312,7 @@ void Video::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
         currentIndex--;
     }
 
-    emit errorStreamUrl(tr("Cannot get video stream for %1").arg(m_webpage.toString()));
-}
-
-void Video::foundVideoUrl(QString videoToken, int definitionCode) {
-    // qDebug() << "foundVideoUrl" << videoToken << definitionCode;
-
-    QUrl videoUrl = QUrl(QString(
-                             "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
-                             ).arg(videoId, videoToken, QString::number(definitionCode)));
-
-    m_streamUrl = videoUrl;
-    loadingStreamUrl = false;
-    emit gotStreamUrl(videoUrl);
+    emit errorStreamUrl(tr("Cannot get video stream for %1").arg(m_webpage));
 }
 
 void Video::errorVideoInfo(QNetworkReply *reply) {
@@ -329,9 +322,9 @@ void Video::errorVideoInfo(QNetworkReply *reply) {
 
 void Video::scrapeWebPage(QByteArray data) {
     QString html = QString::fromUtf8(data);
-    // qWarning() << html;
 
-    if (html.contains("player-age-gate-content\"")) {
+    QRegExp ageGateRE(JsFunctions::instance()->ageGateRE());
+    if (ageGateRE.indexIn(html) != -1) {
         // qDebug() << "Found ageGate";
         ageGate = true;
         elIndex = 4;
@@ -339,10 +332,8 @@ void Video::scrapeWebPage(QByteArray data) {
         return;
     }
 
-    QRegExp re(".*\"url_encoded_fmt_stream_map\":\\s+\"([^\"]+)\".*");
-    bool match = re.exactMatch(html);
-    // on regexp failure, stop and report error
-    if (!match) {
+    QRegExp fmtMapRE(JsFunctions::instance()->webPageFmtMapRE());
+    if (fmtMapRE.indexIn(html) == -1) {
         qWarning() << "Error parsing video page";
         // emit errorStreamUrl("Error parsing video page");
         // loadingStreamUrl = false;
@@ -350,14 +341,14 @@ void Video::scrapeWebPage(QByteArray data) {
         getVideoInfo();
         return;
     }
-    fmtUrlMap = re.cap(1);
+    fmtUrlMap = fmtMapRE.cap(1);
     fmtUrlMap.replace("\\u0026", "&");
     // parseFmtUrlMap(fmtUrlMap, true);
 
 #ifdef APP_DASH
     QSettings settings;
     QString definitionName = settings.value("definition", "360p").toString();
-    if (definitionName == QLatin1String("1080p") {
+    if (definitionName == QLatin1String("1080p")) {
         QRegExp dashManifestRe("\"dashmpd\":\\s*\"([^\"]+)\"");
         if (dashManifestRe.indexIn(html) != -1) {
             dashManifestUrl = dashManifestRe.cap(1);
@@ -367,72 +358,32 @@ void Video::scrapeWebPage(QByteArray data) {
     }
 #endif
 
-    QRegExp jsPlayerRe("\"assets\":.+\"js\":\\s*\"([^\"]+)\"");
+    QRegExp jsPlayerRe(JsFunctions::instance()->jsPlayerRE());
     if (jsPlayerRe.indexIn(html) != -1) {
         QString jsPlayerUrl = jsPlayerRe.cap(1);
         jsPlayerUrl.remove('\\');
         jsPlayerUrl = "http:" + jsPlayerUrl;
         // qDebug() << "jsPlayerUrl" << jsPlayerUrl;
         /*
-        QRegExp jsPlayerIdRe("-(.+)\\.js");
-        jsPlayerIdRe.indexIn(jsPlayerUrl);
-        QString jsPlayerId = jsPlayerRe.cap(1);
-        */
+                    QRegExp jsPlayerIdRe("-(.+)\\.js");
+                    jsPlayerIdRe.indexIn(jsPlayerUrl);
+                    QString jsPlayerId = jsPlayerRe.cap(1);
+                    */
         QObject *reply = The::http()->get(jsPlayerUrl);
         connect(reply, SIGNAL(data(QByteArray)), SLOT(parseJsPlayer(QByteArray)));
         connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
     }
 }
 
-void Video::gotHeadHeaders(QNetworkReply* reply) {
-    int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
-    // qDebug() << "gotHeaders" << statusCode;
-    if (statusCode == 200) {
-        foundVideoUrl(videoToken, definitionCode);
-    } else {
-
-        // try next (lower quality) definition
-        /*
-        QStringList definitionNames = VideoDefinition::getDefinitionNames();
-        int currentIndex = definitionNames.indexOf(currentDefinition);
-        int previousIndex = 0;
-        if (currentIndex > 0) {
-            previousIndex = currentIndex - 1;
-        }
-        if (previousIndex > 0) {
-            QString nextDefinitionName = definitionNames.at(previousIndex);
-            findVideoUrl(nextDefinitionName);
-        } else {
-            foundVideoUrl(videoToken, 18);
-        }*/
-
-
-        QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
-        int currentIndex = definitionCodes.indexOf(definitionCode);
-        int previousIndex = 0;
-        if (currentIndex > 0) {
-            previousIndex = currentIndex - 1;
-            int definitionCode = definitionCodes.at(previousIndex);
-            if (definitionCode == 18) {
-                // This is assumed always available
-                foundVideoUrl(videoToken, 18);
-            } else {
-                findVideoUrl(definitionCode);
-            }
-
-        } else {
-            foundVideoUrl(videoToken, 18);
-        }
-
-    }
-}
-
 void Video::parseJsPlayer(QByteArray bytes) {
     QString js = QString::fromUtf8(bytes);
     // qWarning() << "jsPlayer" << js;
-    QRegExp funcNameRe("signature=([" + jsNameChars + "]+)");
+
+    // QRegExp funcNameRe("\"signature\"\\w*,\\w*([" + jsNameChars + "]+)");
+    QRegExp funcNameRe(JsFunctions::instance()->signatureFunctionNameRE().arg(jsNameChars));
+
     if (funcNameRe.indexIn(js) == -1) {
-        qWarning() << "Cannot capture signature function name";
+        qWarning() << "Cannot capture signature function name" << js;
     } else {
         sigFuncName = funcNameRe.cap(1);
         captureFunction(sigFuncName, js);
@@ -449,16 +400,18 @@ void Video::parseJsPlayer(QByteArray bytes) {
             dashManifestUrl.replace(sigRe, "/signature/" + sig);
             qDebug() << dashManifestUrl;
 
-            m_streamUrl = dashManifestUrl;
-            this->definitionCode = 37;
-            emit gotStreamUrl(m_streamUrl);
-            loadingStreamUrl = false;
-
-            /*
-            QObject *reply = The::http()->get(QUrl::fromEncoded(dashManifestUrl.toUtf8()));
-            connect(reply, SIGNAL(data(QByteArray)), SLOT(parseDashManifest(QByteArray)));
-            connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
-            */
+            if (false) {
+                // let phonon play the manifest
+                m_streamUrl = dashManifestUrl;
+                this->definitionCode = 37;
+                emit gotStreamUrl(m_streamUrl);
+                loadingStreamUrl = false;
+            } else {
+                // download the manifest
+                QObject *reply = The::http()->get(QUrl::fromEncoded(dashManifestUrl.toUtf8()));
+                connect(reply, SIGNAL(data(QByteArray)), SLOT(parseDashManifest(QByteArray)));
+                connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
+            }
 
             return;
         }
@@ -469,7 +422,7 @@ void Video::parseJsPlayer(QByteArray bytes) {
 }
 
 void Video::parseDashManifest(QByteArray bytes) {
-    QFile file(Temporary::filename());
+    QFile file(Temporary::filename() + ".mpd");
     if (!file.open(QIODevice::WriteOnly))
         qWarning() << file.errorString() << file.fileName();
     QDataStream stream(&file);
@@ -548,20 +501,6 @@ QString Video::decryptSignature(const QString &s) {
     return value.toString();
 }
 
-void Video::findVideoUrl(int definitionCode) {
-    this->definitionCode = definitionCode;
-
-    QUrl videoUrl = QUrl(QString(
-                             "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
-                             ).arg(videoId, videoToken, QString::number(definitionCode)));
-
-    QObject *reply = The::http()->head(videoUrl);
-    connect(reply, SIGNAL(finished(QNetworkReply*)), SLOT(gotHeadHeaders(QNetworkReply*)));
-    // connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
-
-    // see you in gotHeadHeaders()
-}
-
 QString Video::formattedDuration() const {
     QString format = m_duration > 3600 ? "h:mm:ss" : "m:ss";
     return QTime().addSecs(m_duration).toString(format);