1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "ytstandardfeed.h"
22 #include "http.h"
23 #include "httputils.h"
24 #include "video.h"
25
26 #include "yt3.h"
27 #include "yt3listparser.h"
28
YTStandardFeed(QObject * parent)29 YTStandardFeed::YTStandardFeed(QObject *parent)
30 : PaginatedVideoSource(parent),
31 aborted(false) { }
32
loadVideos(int max,int startIndex)33 void YTStandardFeed::loadVideos(int max, int startIndex) {
34 aborted = false;
35
36 QUrl url = YT3::instance().method("videos");
37
38 QUrlQuery q(url);
39 if (startIndex > 1) {
40 if (maybeReloadToken(max, startIndex)) return;
41 q.addQueryItem("pageToken", nextPageToken);
42 }
43
44 q.addQueryItem("part", "snippet,contentDetails,statistics");
45 q.addQueryItem("chart", "mostPopular");
46
47 if (!category.isEmpty())
48 q.addQueryItem("videoCategoryId", category);
49
50 if (!regionId.isEmpty())
51 q.addQueryItem("regionCode", regionId);
52
53 q.addQueryItem("maxResults", QString::number(max));
54
55 url.setQuery(q);
56
57 QObject *reply = HttpUtils::yt().get(url);
58 qDebug() << url;
59 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
60 connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
61 }
62
parseResults(QByteArray data)63 void YTStandardFeed::parseResults(QByteArray data) {
64 if (aborted) return;
65
66 YT3ListParser parser(data);
67 const QVector<Video*> &videos = parser.getVideos();
68
69 bool tryingWithNewToken = setPageToken(parser.getNextPageToken());
70 if (tryingWithNewToken) return;
71
72 if (reloadingToken) {
73 reloadingToken = false;
74 loadVideos(currentMax, currentStartIndex);
75 currentMax = currentStartIndex = 0;
76 return;
77 }
78
79 emit gotVideos(videos);
80 emit finished(videos.size());
81 }
82
abort()83 void YTStandardFeed::abort() {
84 aborted = true;
85 }
86
requestError(const QString & message)87 void YTStandardFeed::requestError(const QString &message) {
88 emit error(message);
89 }
90