]> git.sur5r.net Git - minitube/blob - src/ytstandardfeed.cpp
Imported Upstream version 2.5.1
[minitube] / src / ytstandardfeed.cpp
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 "networkaccess.h"
23 #include "video.h"
24 #include "compatibility/qurlqueryhelper.h"
25
26 #ifdef APP_YT3
27 #include "yt3.h"
28 #include "yt3listparser.h"
29 #else
30 #include "ytfeedreader.h"
31 #endif
32
33 namespace The {
34 NetworkAccess* http();
35 }
36
37 YTStandardFeed::YTStandardFeed(QObject *parent)
38     : PaginatedVideoSource(parent),
39       aborted(false) { }
40
41 #ifdef APP_YT3
42
43 void YTStandardFeed::loadVideos(int max, int startIndex) {
44     aborted = false;
45
46     QUrl url = YT3::instance().method("videos");
47
48     {
49         QUrlQueryHelper urlHelper(url);
50         if (startIndex > 1) {
51             if (maybeReloadToken(max, startIndex)) return;
52             urlHelper.addQueryItem("pageToken", nextPageToken);
53         }
54
55         urlHelper.addQueryItem("part", "snippet,contentDetails,statistics");
56         urlHelper.addQueryItem("chart", "mostPopular");
57
58         if (!category.isEmpty())
59             urlHelper.addQueryItem("videoCategoryId", category);
60
61         if (!regionId.isEmpty())
62             urlHelper.addQueryItem("regionCode", regionId);
63
64         urlHelper.addQueryItem("maxResults", QString::number(max));
65
66     }
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*)));
70 }
71
72 void YTStandardFeed::parseResults(QByteArray data) {
73     if (aborted) return;
74
75     YT3ListParser parser(data);
76     QList<Video*> videos = parser.getVideos();
77
78     bool tryingWithNewToken = setPageToken(parser.getNextPageToken());
79     if (tryingWithNewToken) return;
80
81     if (reloadingToken) {
82         reloadingToken = false;
83         loadVideos(currentMax, currentStartIndex);
84         currentMax = currentStartIndex = 0;
85         return;
86     }
87
88     emit gotVideos(videos);
89     emit finished(videos.size());
90 }
91
92 #else
93
94 void YTStandardFeed::loadVideos(int max, int startIndex) {
95     aborted = false;
96
97     QString s = "http://gdata.youtube.com/feeds/api/standardfeeds/";
98     if (!regionId.isEmpty()) s += regionId + "/";
99     s += feedId;
100     if (!category.isEmpty()) s += "_" + category;
101
102     QUrl url(s);
103     {
104         QUrlQueryHelper urlHelper(url);
105         urlHelper.addQueryItem("v", "2");
106
107         if (feedId != "most_shared" && feedId != "on_the_web") {
108             QString t = time;
109             if (t.isEmpty()) t = "today";
110             urlHelper.addQueryItem("time", t);
111         }
112
113         urlHelper.addQueryItem("max-results", QString::number(max));
114         urlHelper.addQueryItem("start-index", QString::number(startIndex));
115     }
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*)));
119 }
120
121 void YTStandardFeed::parseResults(QByteArray data) {
122     if (aborted) return;
123
124     YTFeedReader reader(data);
125     QList<Video*> videos = reader.getVideos();
126
127     emit gotVideos(videos);
128     emit finished(videos.size());
129 }
130
131 #endif
132
133 void YTStandardFeed::abort() {
134     aborted = true;
135 }
136
137 const QStringList & YTStandardFeed::getSuggestions() {
138     static const QStringList l;
139     return l;
140 }
141
142 void YTStandardFeed::requestError(QNetworkReply *reply) {
143     emit error(reply->errorString());
144 }