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