]> git.sur5r.net Git - minitube/blob - src/ytsinglevideosource.cpp
Imported Upstream version 2.4
[minitube] / src / ytsinglevideosource.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 "ytsinglevideosource.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 YTSingleVideoSource::YTSingleVideoSource(QObject *parent) : PaginatedVideoSource(parent),
37     video(0),
38     startIndex(0),
39     max(0) { }
40
41 #ifdef APP_YT3
42
43 void YTSingleVideoSource::loadVideos(int max, int startIndex) {
44     aborted = false;
45     this->startIndex = startIndex;
46     this->max = max;
47
48     QUrl url;
49
50     if (startIndex == 1) {
51
52         if (video) {
53             QList<Video*> videos;
54             videos << video->clone();
55             if (name.isEmpty()) {
56                 name = videos.first()->title();
57                 qDebug() << "Emitting name changed" << name;
58                 emit nameChanged(name);
59             }
60             emit gotVideos(videos);
61             loadVideos(max - 1, 2);
62             return;
63         }
64
65         url = YT3::instance().method("videos");
66 #if QT_VERSION >= 0x050000
67         {
68             QUrl &u = url;
69             QUrlQuery url;
70 #endif
71             url.addQueryItem("part", "snippet");
72             url.addQueryItem("id", videoId);
73 #if QT_VERSION >= 0x050000
74             u.setQuery(url);
75         }
76 #endif
77     } else {
78         url = YT3::instance().method("search");
79 #if QT_VERSION >= 0x050000
80         {
81             QUrl &u = url;
82             QUrlQuery url;
83 #endif
84             url.addQueryItem("part", "snippet");
85             url.addQueryItem("type", "video");
86             url.addQueryItem("relatedToVideoId", videoId);
87             url.addQueryItem("maxResults", QString::number(max));
88             if (startIndex > 2) {
89                 if (maybeReloadToken(max, startIndex)) return;
90                 url.addQueryItem("pageToken", nextPageToken);
91             }
92 #if QT_VERSION >= 0x050000
93             u.setQuery(url);
94         }
95 #endif
96     }
97
98     lastUrl = url;
99
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*)));
103 }
104
105 void YTSingleVideoSource::parseResults(QByteArray data) {
106     if (aborted) return;
107
108     YT3ListParser parser(data);
109     QList<Video*> videos = parser.getVideos();
110
111     bool tryingWithNewToken = setPageToken(parser.getNextPageToken());
112     if (tryingWithNewToken) return;
113
114     if (asyncDetails) {
115         emit gotVideos(videos);
116         if (startIndex == 2) emit finished(videos.size() + 1);
117         else emit finished(videos.size());
118     }
119     loadVideoDetails(videos);
120 }
121
122 #else
123
124 void YTSingleVideoSource::loadVideos(int max, int startIndex) {
125     aborted = false;
126     this->startIndex = startIndex;
127     this->max = max;
128
129     QString s;
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);
132     QUrl url(s);
133 #if QT_VERSION >= 0x050000
134     {
135         QUrl &u = url;
136         QUrlQuery url;
137 #endif
138         url.addQueryItem("v", "2");
139
140         if (startIndex != 1) {
141             url.addQueryItem("max-results", QString::number(max));
142             url.addQueryItem("start-index", QString::number(startIndex-1));
143         }
144
145 #if QT_VERSION >= 0x050000
146         u.setQuery(url);
147     }
148 #endif
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*)));
152 }
153
154 void YTSingleVideoSource::parse(QByteArray data) {
155     if (aborted) return;
156
157     YTFeedReader reader(data);
158     QList<Video*> videos = reader.getVideos();
159
160     if (name.isEmpty() && !videos.isEmpty() && startIndex == 1) {
161         name = videos.first()->title();
162         emit nameChanged(name);
163     }
164
165     emit gotVideos(videos);
166
167     if (startIndex == 1) loadVideos(max - 1, 2);
168     else if (startIndex == 2) emit finished(videos.size() + 1);
169     else emit finished(videos.size());
170 }
171
172 #endif
173
174 void YTSingleVideoSource::abort() {
175     aborted = true;
176 }
177
178 const QStringList & YTSingleVideoSource::getSuggestions() {
179     static const QStringList *l = new QStringList();
180     return *l;
181 }
182
183 QString YTSingleVideoSource::getName() {
184     return name;
185 }
186
187 void YTSingleVideoSource::setVideo(Video *video) {
188     this->video = video;
189     videoId = video->id();
190 }
191
192 void YTSingleVideoSource::requestError(QNetworkReply *reply) {
193     emit error(reply->errorString());
194 }