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 "ytstandardfeed.h"
22 #include "networkaccess.h"
24 #include "compatibility/qurlqueryhelper.h"
28 #include "yt3listparser.h"
30 #include "ytfeedreader.h"
34 NetworkAccess* http();
37 YTStandardFeed::YTStandardFeed(QObject *parent)
38 : PaginatedVideoSource(parent),
43 void YTStandardFeed::loadVideos(int max, int startIndex) {
46 QUrl url = YT3::instance().method("videos");
49 QUrlQueryHelper urlHelper(url);
51 if (maybeReloadToken(max, startIndex)) return;
52 urlHelper.addQueryItem("pageToken", nextPageToken);
55 urlHelper.addQueryItem("part", "snippet,contentDetails,statistics");
56 urlHelper.addQueryItem("chart", "mostPopular");
58 if (!category.isEmpty())
59 urlHelper.addQueryItem("videoCategoryId", category);
61 if (!regionId.isEmpty())
62 urlHelper.addQueryItem("regionCode", regionId);
64 urlHelper.addQueryItem("maxResults", QString::number(max));
67 QObject *reply = The::http()->get(url);
68 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
69 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
72 void YTStandardFeed::parseResults(QByteArray data) {
75 YT3ListParser parser(data);
76 QList<Video*> videos = parser.getVideos();
78 bool tryingWithNewToken = setPageToken(parser.getNextPageToken());
79 if (tryingWithNewToken) return;
82 reloadingToken = false;
83 loadVideos(currentMax, currentStartIndex);
84 currentMax = currentStartIndex = 0;
88 emit gotVideos(videos);
89 emit finished(videos.size());
94 void YTStandardFeed::loadVideos(int max, int startIndex) {
97 QString s = "http://gdata.youtube.com/feeds/api/standardfeeds/";
98 if (!regionId.isEmpty()) s += regionId + "/";
100 if (!category.isEmpty()) s += "_" + category;
104 QUrlQueryHelper urlHelper(url);
105 urlHelper.addQueryItem("v", "2");
107 if (feedId != "most_shared" && feedId != "on_the_web") {
109 if (t.isEmpty()) t = "today";
110 urlHelper.addQueryItem("time", t);
113 urlHelper.addQueryItem("max-results", QString::number(max));
114 urlHelper.addQueryItem("start-index", QString::number(startIndex));
116 QObject *reply = The::http()->get(url);
117 connect(reply, SIGNAL(data(QByteArray)), SLOT(parse(QByteArray)));
118 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
121 void YTStandardFeed::parseResults(QByteArray data) {
124 YTFeedReader reader(data);
125 QList<Video*> videos = reader.getVideos();
127 emit gotVideos(videos);
128 emit finished(videos.size());
133 void YTStandardFeed::abort() {
137 const QStringList & YTStandardFeed::getSuggestions() {
138 static const QStringList l;
142 void YTStandardFeed::requestError(QNetworkReply *reply) {
143 emit error(reply->errorString());