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