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/>.
22 #include "ytfeedreader.h"
23 #include "constants.h"
24 #include "networkaccess.h"
25 #include "searchparams.h"
31 NetworkAccess* http();
32 QHash<QString, QAction*>* globalActions();
35 YTSearch::YTSearch(SearchParams *searchParams, QObject *parent) :
37 searchParams(searchParams) {
38 searchParams->setParent(this);
41 void YTSearch::loadVideos(int max, int skip) {
44 QUrl url("http://gdata.youtube.com/feeds/api/videos/");
45 #if QT_VERSION >= 0x050000
51 url.addQueryItem("v", "2");
52 url.addQueryItem("max-results", QString::number(max));
53 url.addQueryItem("start-index", QString::number(skip));
55 if (!searchParams->keywords().isEmpty()) {
56 if (searchParams->keywords().startsWith("http://") ||
57 searchParams->keywords().startsWith("https://")) {
58 url.addQueryItem("q", YTSearch::videoIdFromUrl(searchParams->keywords()));
59 } else url.addQueryItem("q", searchParams->keywords());
62 if (!searchParams->author().isEmpty())
63 url.addQueryItem("author", searchParams->author());
65 switch (searchParams->sortBy()) {
66 case SearchParams::SortByNewest:
67 url.addQueryItem("orderby", "published");
69 case SearchParams::SortByViewCount:
70 url.addQueryItem("orderby", "viewCount");
72 case SearchParams::SortByRating:
73 url.addQueryItem("orderby", "rating");
77 switch (searchParams->duration()) {
78 case SearchParams::DurationShort:
79 url.addQueryItem("duration", "short");
81 case SearchParams::DurationMedium:
82 url.addQueryItem("duration", "medium");
84 case SearchParams::DurationLong:
85 url.addQueryItem("duration", "long");
89 switch (searchParams->time()) {
90 case SearchParams::TimeToday:
91 url.addQueryItem("time", "today");
93 case SearchParams::TimeWeek:
94 url.addQueryItem("time", "this_week");
96 case SearchParams::TimeMonth:
97 url.addQueryItem("time", "this_month");
101 switch (searchParams->quality()) {
102 case SearchParams::QualityHD:
103 url.addQueryItem("hd", "true");
107 #if QT_VERSION >= 0x050000
111 QObject *reply = The::http()->get(url);
112 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
113 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
116 void YTSearch::abort() {
120 const QStringList & YTSearch::getSuggestions() {
124 QString YTSearch::getName() {
125 if (!name.isEmpty()) return name;
126 if (!searchParams->keywords().isEmpty()) return searchParams->keywords();
130 void YTSearch::parseResults(QByteArray data) {
133 YTFeedReader reader(data);
134 QList<Video*> videos = reader.getVideos();
135 suggestions = reader.getSuggestions();
137 if (name.isEmpty() && !searchParams->author().isEmpty()) {
138 if (videos.isEmpty()) name = searchParams->author();
140 name = videos.first()->author();
141 // also grab the userId
142 userId = videos.first()->userId();
144 emit nameChanged(name);
147 emit gotVideos(videos);
148 emit finished(videos.size());
151 void YTSearch::requestError(QNetworkReply *reply) {
152 emit error(reply->errorString());
155 QString YTSearch::videoIdFromUrl(QString url) {
156 QRegExp re = QRegExp("^.*[\\?&]v=([^&#]+).*$");
157 if (re.exactMatch(url)) return re.cap(1);
158 re = QRegExp("^.*://.*/([^&#\\?]+).*$");
159 if (re.exactMatch(url)) return re.cap(1);
163 QList<QAction*> YTSearch::getActions() {
164 QList<QAction*> channelActions;
165 if (searchParams->author().isEmpty())
166 return channelActions;
167 channelActions << The::globalActions()->value("subscribe-channel");
168 return channelActions;