3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "ytsinglevideosource.h"
22 #include "networkaccess.h"
24 #include "compatibility/qurlqueryhelper.h"
28 #include "yt3listparser.h"
30 #include "ytfeedreader.h"
34 NetworkAccess* http();
37 YTSingleVideoSource::YTSingleVideoSource(QObject *parent) : PaginatedVideoSource(parent),
44 void YTSingleVideoSource::loadVideos(int max, int startIndex) {
46 this->startIndex = startIndex;
51 if (startIndex == 1) {
55 videos << video->clone();
57 name = videos.first()->title();
58 qDebug() << "Emitting name changed" << name;
59 emit nameChanged(name);
61 emit gotVideos(videos);
62 loadVideos(max - 1, 2);
66 url = YT3::instance().method("videos");
68 QUrlQueryHelper urlHelper(url);
69 urlHelper.addQueryItem("part", "snippet");
70 urlHelper.addQueryItem("id", videoId);
73 url = YT3::instance().method("search");
75 QUrlQueryHelper urlHelper(url);
76 urlHelper.addQueryItem("part", "snippet");
77 urlHelper.addQueryItem("type", "video");
78 urlHelper.addQueryItem("relatedToVideoId", videoId);
79 urlHelper.addQueryItem("maxResults", QString::number(max));
81 if (maybeReloadToken(max, startIndex)) return;
82 urlHelper.addQueryItem("pageToken", nextPageToken);
89 QObject *reply = The::http()->get(url);
90 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
91 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
94 void YTSingleVideoSource::parseResults(QByteArray data) {
97 YT3ListParser parser(data);
98 QList<Video*> videos = parser.getVideos();
100 bool tryingWithNewToken = setPageToken(parser.getNextPageToken());
101 if (tryingWithNewToken) return;
104 emit gotVideos(videos);
105 if (startIndex == 2) emit finished(videos.size() + 1);
106 else emit finished(videos.size());
108 loadVideoDetails(videos);
113 void YTSingleVideoSource::loadVideos(int max, int startIndex) {
115 this->startIndex = startIndex;
119 if (startIndex == 1) s = "http://gdata.youtube.com/feeds/api/videos/" + videoId;
120 else s = QString("http://gdata.youtube.com/feeds/api/videos/%1/related").arg(videoId);
123 QUrlQueryHelper urlHelper(url);
124 urlHelper.addQueryItem("v", "2");
126 if (startIndex != 1) {
127 urlHelper.addQueryItem("max-results", QString::number(max));
128 urlHelper.addQueryItem("start-index", QString::number(startIndex-1));
131 QObject *reply = The::http()->get(url);
132 connect(reply, SIGNAL(data(QByteArray)), SLOT(parse(QByteArray)));
133 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
136 void YTSingleVideoSource::parse(QByteArray data) {
139 YTFeedReader reader(data);
140 QList<Video*> videos = reader.getVideos();
142 if (name.isEmpty() && !videos.isEmpty() && startIndex == 1) {
143 name = videos.first()->title();
144 emit nameChanged(name);
147 emit gotVideos(videos);
149 if (startIndex == 1) loadVideos(max - 1, 2);
150 else if (startIndex == 2) emit finished(videos.size() + 1);
151 else emit finished(videos.size());
156 void YTSingleVideoSource::abort() {
160 const QStringList & YTSingleVideoSource::getSuggestions() {
161 static const QStringList *l = new QStringList();
165 QString YTSingleVideoSource::getName() {
169 void YTSingleVideoSource::setVideo(Video *video) {
171 videoId = video->id();
174 void YTSingleVideoSource::requestError(QNetworkReply *reply) {
175 emit error(reply->errorString());