]> git.sur5r.net Git - minitube/blob - src/ytsinglevideosource.cpp
e7bf33e2168f9d6d09b426e873a5a1a50863d9fe
[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 "http.h"
23 #include "httputils.h"
24 #include "video.h"
25
26 #include "yt3.h"
27 #include "yt3listparser.h"
28
29 YTSingleVideoSource::YTSingleVideoSource(QObject *parent) : PaginatedVideoSource(parent),
30     video(0),
31     startIndex(0),
32     max(0) { }
33
34 void YTSingleVideoSource::loadVideos(int max, int startIndex) {
35     aborted = false;
36     this->startIndex = startIndex;
37     this->max = max;
38
39     QUrl url;
40
41     if (startIndex == 1) {
42
43         if (video) {
44             QVector<Video*> videos;
45             videos << video->clone();
46             if (name.isEmpty()) {
47                 name = videos.at(0)->getTitle();
48                 qDebug() << "Emitting name changed" << name;
49                 emit nameChanged(name);
50             }
51             emit gotVideos(videos);
52             loadVideos(max - 1, 2);
53             return;
54         }
55
56         url = YT3::instance().method("videos");
57         QUrlQuery q(url);
58         q.addQueryItem("part", "snippet");
59         q.addQueryItem("id", videoId);
60         url.setQuery(q);
61
62     } else {
63         url = YT3::instance().method("search");
64         QUrlQuery q(url);
65         q.addQueryItem("part", "snippet");
66         q.addQueryItem("type", "video");
67         q.addQueryItem("relatedToVideoId", videoId);
68         q.addQueryItem("maxResults", QString::number(max));
69         if (startIndex > 2) {
70             if (maybeReloadToken(max, startIndex)) return;
71             q.addQueryItem("pageToken", nextPageToken);
72         }
73         url.setQuery(q);
74     }
75
76     lastUrl = url;
77
78     QObject *reply = HttpUtils::yt().get(url);
79     connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
80     connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
81 }
82
83 void YTSingleVideoSource::parseResults(QByteArray data) {
84     if (aborted) return;
85
86     YT3ListParser parser(data);
87     const QVector<Video*> &videos = parser.getVideos();
88
89     bool tryingWithNewToken = setPageToken(parser.getNextPageToken());
90     if (tryingWithNewToken) return;
91
92     if (asyncDetails) {
93         emit gotVideos(videos);
94         if (startIndex == 2) emit finished(videos.size() + 1);
95         else emit finished(videos.size());
96     }
97     loadVideoDetails(videos);
98 }
99
100 void YTSingleVideoSource::abort() {
101     aborted = true;
102 }
103
104 QString YTSingleVideoSource::getName() {
105     return name;
106 }
107
108 void YTSingleVideoSource::setVideo(Video *video) {
109     this->video = video;
110     videoId = video->getId();
111 }
112
113 void YTSingleVideoSource::requestError(const QString &message) {
114     emit error(message);
115 }