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 "paginatedvideosource.h"
24 #include "yt3listparser.h"
25 #include "datautils.h"
29 #include "httputils.h"
31 PaginatedVideoSource::PaginatedVideoSource(QObject *parent) : VideoSource(parent)
33 , reloadingToken(false)
35 , currentStartIndex(0)
36 , asyncDetails(false) { }
38 bool PaginatedVideoSource::hasMoreVideos() {
39 qDebug() << __PRETTY_FUNCTION__ << nextPageToken;
40 return !nextPageToken.isEmpty();
43 bool PaginatedVideoSource::maybeReloadToken(int max, int startIndex) {
44 // kind of hackish. Thank the genius who came up with this stateful stuff
45 // in a supposedly RESTful (aka stateless) API.
47 if (nextPageToken.isEmpty()) {
48 // previous request did not return a page token. Game over.
49 // emit gotVideos(QVector<Video*>());
54 if (isPageTokenExpired()) {
55 reloadingToken = true;
57 currentStartIndex = startIndex;
64 bool PaginatedVideoSource::setPageToken(const QString &value) {
65 tokenTimestamp = QDateTime::currentDateTime().toTime_t();
66 nextPageToken = value;
69 reloadingToken = false;
70 loadVideos(currentMax, currentStartIndex);
71 currentMax = currentStartIndex = 0;
78 bool PaginatedVideoSource::isPageTokenExpired() {
79 uint now = QDateTime::currentDateTime().toTime_t();
80 return now - tokenTimestamp > 1800;
83 void PaginatedVideoSource::reloadToken() {
84 qDebug() << "Reloading pageToken";
85 QObject *reply = HttpUtils::yt().get(lastUrl);
86 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
87 connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
90 void PaginatedVideoSource::loadVideoDetails(const QVector<Video*> &videos) {
91 this->videos = videos;
93 videoIds.reserve(videos.size()*12);
94 videoMap.reserve(videos.size());
95 for (Video *video : videos) {
96 // TODO get video details from cache
97 if (!videoIds.isEmpty()) videoIds += QLatin1Char(',');
98 videoIds += video->getId();
99 videoMap.insert(video->getId(), video);
102 if (videoIds.isEmpty()) {
104 emit gotVideos(videos);
105 emit finished(videos.size());
110 QUrl url = YT3::instance().method("videos");
112 q.addQueryItem(QStringLiteral("part"), QStringLiteral("contentDetails,statistics"));
113 q.addQueryItem(QStringLiteral("id"), videoIds);
116 QObject *reply = HttpUtils::yt().get(url);
117 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseVideoDetails(QByteArray)));
118 connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
121 void PaginatedVideoSource::parseVideoDetails(const QByteArray &bytes) {
122 QJsonDocument doc = QJsonDocument::fromJson(bytes);
123 QJsonObject obj = doc.object();
125 QJsonValue items = obj["items"];
126 if (items.isArray()) {
127 const auto array = items.toArray();
128 for (const QJsonValue &v : array) {
129 if (!v.isObject()) continue;
131 QJsonObject item = v.toObject();
133 QString id = item["id"].toString();
134 Video *video = videoMap.value(id);
136 qWarning() << "No video for id" << id;
140 QString isoPeriod = item["contentDetails"].toObject()["duration"].toString();
141 int duration = DataUtils::parseIsoPeriod(isoPeriod);
142 video->setDuration(duration);
144 int viewCount = item["statistics"].toObject()["viewCount"].toString().toInt();
145 video->setViewCount(viewCount);
147 // TODO cache by etag?
151 emit gotVideos(videos);
152 emit finished(videos.size());