X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fytstandardfeed.cpp;h=4f08211bd5516175da7adce3a4828a97fcb8d393;hb=HEAD;hp=111ec291a5600cb50236585b76b00480f583aa59;hpb=a8e005af0aa72f809f823bbd741bb3d0def00ced;p=minitube diff --git a/src/ytstandardfeed.cpp b/src/ytstandardfeed.cpp index 111ec29..4f08211 100644 --- a/src/ytstandardfeed.cpp +++ b/src/ytstandardfeed.cpp @@ -1,58 +1,89 @@ +/* $BEGIN_LICENSE + +This file is part of Minitube. +Copyright 2009, Flavio Tordini + +Minitube is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Minitube is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Minitube. If not, see . + +$END_LICENSE */ + #include "ytstandardfeed.h" -#include -#include "networkaccess.h" +#include "http.h" +#include "httputils.h" #include "video.h" -#include "ytfeedreader.h" -namespace The { -NetworkAccess* http(); -} +#include "yt3.h" +#include "yt3listparser.h" YTStandardFeed::YTStandardFeed(QObject *parent) - : VideoSource(parent), + : PaginatedVideoSource(parent), aborted(false) { } -void YTStandardFeed::loadVideos(int max, int skip) { +void YTStandardFeed::loadVideos(int max, int startIndex) { aborted = false; - QString s = "http://gdata.youtube.com/feeds/api/standardfeeds/"; - if (!regionId.isEmpty()) s += regionId + "/"; - s += feedId; - if (!category.isEmpty()) s += "_" + category; + QUrl url = YT3::instance().method("videos"); - QUrl url(s); - url.addQueryItem("v", "2"); + QUrlQuery q(url); + if (startIndex > 1) { + if (maybeReloadToken(max, startIndex)) return; + q.addQueryItem("pageToken", nextPageToken); + } - if (feedId != "most_shared") - url.addQueryItem("time", "today"); + q.addQueryItem("part", "snippet,contentDetails,statistics"); + q.addQueryItem("chart", "mostPopular"); - url.addQueryItem("max-results", QString::number(max)); - url.addQueryItem("start-index", QString::number(skip)); + if (!category.isEmpty()) + q.addQueryItem("videoCategoryId", category); - QObject *reply = The::http()->get(url); - connect(reply, SIGNAL(data(QByteArray)), SLOT(parse(QByteArray))); - connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*))); -} + if (!regionId.isEmpty()) + q.addQueryItem("regionCode", regionId); -void YTStandardFeed::abort() { - aborted = true; -} + q.addQueryItem("maxResults", QString::number(max)); + + url.setQuery(q); -const QStringList & YTStandardFeed::getSuggestions() { - QStringList *l = new QStringList(); - return *l; + QObject *reply = HttpUtils::yt().get(url); + qDebug() << url; + connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray))); + connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString))); } -void YTStandardFeed::parse(QByteArray data) { +void YTStandardFeed::parseResults(QByteArray data) { if (aborted) return; - YTFeedReader reader(data); - QList videos = reader.getVideos(); + YT3ListParser parser(data); + const QVector &videos = parser.getVideos(); + + bool tryingWithNewToken = setPageToken(parser.getNextPageToken()); + if (tryingWithNewToken) return; + + if (reloadingToken) { + reloadingToken = false; + loadVideos(currentMax, currentStartIndex); + currentMax = currentStartIndex = 0; + return; + } emit gotVideos(videos); emit finished(videos.size()); } -void YTStandardFeed::requestError(QNetworkReply *reply) { - emit error(reply->errorString()); +void YTStandardFeed::abort() { + aborted = true; +} + +void YTStandardFeed::requestError(const QString &message) { + emit error(message); }