]> git.sur5r.net Git - minitube/blobdiff - src/ytsinglevideosource.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / ytsinglevideosource.cpp
index 6a22de58b22a55ebe9256871124c365d56991395..09936cad8f1a541deb831f31331c4e45f351219c 100644 (file)
+/* $BEGIN_LICENSE
+
+This file is part of Minitube.
+Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
+
+Minitube is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Minitube is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
+
+$END_LICENSE */
+
 #include "ytsinglevideosource.h"
-#include <QtXml>
-#include "networkaccess.h"
+#include "http.h"
+#include "httputils.h"
 #include "video.h"
-#include "ytfeedreader.h"
 
-namespace The {
-NetworkAccess* http();
-}
+#include "yt3.h"
+#include "yt3listparser.h"
 
-YTSingleVideoSource::YTSingleVideoSource(QObject *parent) : VideoSource(parent) {
-    skip = 0;
-    max = 0;
-}
+YTSingleVideoSource::YTSingleVideoSource(QObject *parent)
+    : PaginatedVideoSource(parent), video(nullptr), startIndex(0), max(0) {}
 
-void YTSingleVideoSource::loadVideos(int max, int skip) {
+void YTSingleVideoSource::loadVideos(int max, int startIndex) {
     aborted = false;
-    this->skip = skip;
+    this->startIndex = startIndex;
     this->max = max;
 
-    QString s;
-    if (skip == 1) s = "http://gdata.youtube.com/feeds/api/videos/" + videoId;
-    else s = QString("http://gdata.youtube.com/feeds/api/videos/%1/related").arg(videoId);
-    QUrl url(s);
-    url.addQueryItem("v", "2");
+    QUrl url;
 
-    if (skip != 1) {
-        url.addQueryItem("max-results", QString::number(max));
-        url.addQueryItem("start-index", QString::number(skip-1));
-    }
+    if (startIndex == 1) {
+        if (video) {
+            QVector<Video *> videos;
+            videos << video->clone();
+            if (name.isEmpty()) {
+                name = videos.at(0)->getTitle();
+                qDebug() << "Emitting name changed" << name;
+                emit nameChanged(name);
+            }
+            emit gotVideos(videos);
+            loadVideos(max - 1, 2);
+            return;
+        }
 
-    QObject *reply = The::http()->get(url);
-    connect(reply, SIGNAL(data(QByteArray)), SLOT(parse(QByteArray)));
-    connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
-}
+        url = YT3::instance().method("videos");
+        QUrlQuery q(url);
+        q.addQueryItem("part", "snippet");
+        q.addQueryItem("id", videoId);
+        url.setQuery(q);
 
-void YTSingleVideoSource::abort() {
-    aborted = true;
-}
+    } else {
+        url = YT3::instance().method("search");
+        QUrlQuery q(url);
+        q.addQueryItem("part", "snippet");
+        q.addQueryItem("type", "video");
+        q.addQueryItem("relatedToVideoId", videoId);
+        q.addQueryItem("maxResults", QString::number(max));
+        if (startIndex > 2) {
+            if (maybeReloadToken(max, startIndex)) return;
+            q.addQueryItem("pageToken", nextPageToken);
+        }
+        url.setQuery(q);
+    }
 
-const QStringList & YTSingleVideoSource::getSuggestions() {
-    QStringList *l = new QStringList();
-    return *l;
-}
+    lastUrl = url;
 
-QString YTSingleVideoSource::getName() {
-    return name;
+    QObject *reply = HttpUtils::yt().get(url);
+    connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
+    connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
 }
 
-void YTSingleVideoSource::parse(QByteArray data) {
+void YTSingleVideoSource::parseResults(QByteArray data) {
     if (aborted) return;
 
-    YTFeedReader reader(data);
-    QList<Video*> videos = reader.getVideos();
+    YT3ListParser parser(data);
+    const QVector<Video *> &videos = parser.getVideos();
 
-    if (name.isEmpty() && !videos.isEmpty() && skip == 1) {
-        name = videos.first()->title();
-        emit nameChanged(name);
+    bool tryingWithNewToken = setPageToken(parser.getNextPageToken());
+    if (tryingWithNewToken) return;
+
+    if (asyncDetails) {
+        emit gotVideos(videos);
+        if (startIndex == 2)
+            emit finished(videos.size() + 1);
+        else
+            emit finished(videos.size());
     }
+    loadVideoDetails(videos);
+}
 
-    emit gotVideos(videos);
+void YTSingleVideoSource::abort() {
+    aborted = true;
+}
+
+QString YTSingleVideoSource::getName() {
+    return name;
+}
 
-    if (skip == 1) loadVideos(max - 1, 2);
-    else if (skip == 2) emit finished(videos.size() + 1);
-    else emit finished(videos.size());
+void YTSingleVideoSource::setVideo(Video *video) {
+    this->video = video;
+    videoId = video->getId();
 }
 
-void YTSingleVideoSource::requestError(QNetworkReply *reply) {
-    emit error(reply->errorString());
+void YTSingleVideoSource::requestError(const QString &message) {
+    emit error(message);
 }