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