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"
27 #include "yt3listparser.h"
29 #include "ytfeedreader.h"
33 NetworkAccess* http();
36 YTSingleVideoSource::YTSingleVideoSource(QObject *parent) : PaginatedVideoSource(parent),
43 void YTSingleVideoSource::loadVideos(int max, int startIndex) {
45 this->startIndex = startIndex;
50 if (startIndex == 1) {
54 videos << video->clone();
56 name = videos.first()->title();
57 qDebug() << "Emitting name changed" << name;
58 emit nameChanged(name);
60 emit gotVideos(videos);
61 loadVideos(max - 1, 2);
65 url = YT3::instance().method("videos");
66 #if QT_VERSION >= 0x050000
71 url.addQueryItem("part", "snippet");
72 url.addQueryItem("id", videoId);
73 #if QT_VERSION >= 0x050000
78 url = YT3::instance().method("search");
79 #if QT_VERSION >= 0x050000
84 url.addQueryItem("part", "snippet");
85 url.addQueryItem("type", "video");
86 url.addQueryItem("relatedToVideoId", videoId);
87 url.addQueryItem("maxResults", QString::number(max));
89 if (maybeReloadToken(max, startIndex)) return;
90 url.addQueryItem("pageToken", nextPageToken);
92 #if QT_VERSION >= 0x050000
100 QObject *reply = The::http()->get(url);
101 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
102 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
105 void YTSingleVideoSource::parseResults(QByteArray data) {
108 YT3ListParser parser(data);
109 QList<Video*> videos = parser.getVideos();
111 bool tryingWithNewToken = setPageToken(parser.getNextPageToken());
112 if (tryingWithNewToken) return;
115 emit gotVideos(videos);
116 if (startIndex == 2) emit finished(videos.size() + 1);
117 else emit finished(videos.size());
119 loadVideoDetails(videos);
124 void YTSingleVideoSource::loadVideos(int max, int startIndex) {
126 this->startIndex = startIndex;
130 if (startIndex == 1) s = "http://gdata.youtube.com/feeds/api/videos/" + videoId;
131 else s = QString("http://gdata.youtube.com/feeds/api/videos/%1/related").arg(videoId);
133 #if QT_VERSION >= 0x050000
138 url.addQueryItem("v", "2");
140 if (startIndex != 1) {
141 url.addQueryItem("max-results", QString::number(max));
142 url.addQueryItem("start-index", QString::number(startIndex-1));
145 #if QT_VERSION >= 0x050000
149 QObject *reply = The::http()->get(url);
150 connect(reply, SIGNAL(data(QByteArray)), SLOT(parse(QByteArray)));
151 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
154 void YTSingleVideoSource::parse(QByteArray data) {
157 YTFeedReader reader(data);
158 QList<Video*> videos = reader.getVideos();
160 if (name.isEmpty() && !videos.isEmpty() && startIndex == 1) {
161 name = videos.first()->title();
162 emit nameChanged(name);
165 emit gotVideos(videos);
167 if (startIndex == 1) loadVideos(max - 1, 2);
168 else if (startIndex == 2) emit finished(videos.size() + 1);
169 else emit finished(videos.size());
174 void YTSingleVideoSource::abort() {
178 const QStringList & YTSingleVideoSource::getSuggestions() {
179 static const QStringList *l = new QStringList();
183 QString YTSingleVideoSource::getName() {
187 void YTSingleVideoSource::setVideo(Video *video) {
189 videoId = video->id();
192 void YTSingleVideoSource::requestError(QNetworkReply *reply) {
193 emit error(reply->errorString());