3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "ytsinglevideosource.h"
23 #include "httputils.h"
27 #include "yt3listparser.h"
29 YTSingleVideoSource::YTSingleVideoSource(QObject *parent)
30 : PaginatedVideoSource(parent), video(nullptr), startIndex(0), max(0) {}
32 void YTSingleVideoSource::loadVideos(int max, int startIndex) {
34 this->startIndex = startIndex;
39 if (startIndex == 1) {
41 QVector<Video *> videos;
42 videos << video->clone();
44 name = videos.at(0)->getTitle();
45 qDebug() << "Emitting name changed" << name;
46 emit nameChanged(name);
48 emit gotVideos(videos);
49 loadVideos(max - 1, 2);
53 url = YT3::instance().method("videos");
55 q.addQueryItem("part", "snippet");
56 q.addQueryItem("id", videoId);
60 url = YT3::instance().method("search");
62 q.addQueryItem("part", "snippet");
63 q.addQueryItem("type", "video");
64 q.addQueryItem("relatedToVideoId", videoId);
65 q.addQueryItem("maxResults", QString::number(max));
67 if (maybeReloadToken(max, startIndex)) return;
68 q.addQueryItem("pageToken", nextPageToken);
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)));
80 void YTSingleVideoSource::parseResults(QByteArray data) {
83 YT3ListParser parser(data);
84 const QVector<Video *> &videos = parser.getVideos();
86 bool tryingWithNewToken = setPageToken(parser.getNextPageToken());
87 if (tryingWithNewToken) return;
90 emit gotVideos(videos);
92 emit finished(videos.size() + 1);
94 emit finished(videos.size());
96 loadVideoDetails(videos);
99 void YTSingleVideoSource::abort() {
103 QString YTSingleVideoSource::getName() {
107 void YTSingleVideoSource::setVideo(Video *video) {
109 videoId = video->getId();
112 void YTSingleVideoSource::requestError(const QString &message) {