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"
30 NetworkAccess* http();
31 QHash<QString, QAction*>* globalActions();
34 YTSearch::YTSearch(SearchParams *searchParams, QObject *parent) :
36 searchParams(searchParams) {
37 searchParams->setParent(this);
40 void YTSearch::loadVideos(int max, int skip) {
43 QUrl url("http://gdata.youtube.com/feeds/api/videos/");
44 #if QT_VERSION >= 0x050000
50 url.addQueryItem("v", "2");
51 url.addQueryItem("max-results", QString::number(max));
52 url.addQueryItem("start-index", QString::number(skip));
54 if (!searchParams->keywords().isEmpty()) {
55 if (searchParams->keywords().startsWith("http://") ||
56 searchParams->keywords().startsWith("https://")) {
57 url.addQueryItem("q", YTSearch::videoIdFromUrl(searchParams->keywords()));
58 } else url.addQueryItem("q", searchParams->keywords());
61 if (!searchParams->author().isEmpty())
62 url.addQueryItem("author", searchParams->author());
64 switch (searchParams->sortBy()) {
65 case SearchParams::SortByNewest:
66 url.addQueryItem("orderby", "published");
68 case SearchParams::SortByViewCount:
69 url.addQueryItem("orderby", "viewCount");
71 case SearchParams::SortByRating:
72 url.addQueryItem("orderby", "rating");
76 switch (searchParams->duration()) {
77 case SearchParams::DurationShort:
78 url.addQueryItem("duration", "short");
80 case SearchParams::DurationMedium:
81 url.addQueryItem("duration", "medium");
83 case SearchParams::DurationLong:
84 url.addQueryItem("duration", "long");
88 switch (searchParams->time()) {
89 case SearchParams::TimeToday:
90 url.addQueryItem("time", "today");
92 case SearchParams::TimeWeek:
93 url.addQueryItem("time", "this_week");
95 case SearchParams::TimeMonth:
96 url.addQueryItem("time", "this_month");
100 switch (searchParams->quality()) {
101 case SearchParams::QualityHD:
102 url.addQueryItem("hd", "true");
106 #if QT_VERSION >= 0x050000
110 QObject *reply = The::http()->get(url);
111 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
112 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
115 void YTSearch::abort() {
119 const QStringList & YTSearch::getSuggestions() {
123 QString YTSearch::getName() {
124 if (!name.isEmpty()) return name;
125 if (!searchParams->keywords().isEmpty()) return searchParams->keywords();
129 void YTSearch::parseResults(QByteArray data) {
132 YTFeedReader reader(data);
133 QList<Video*> videos = reader.getVideos();
134 suggestions = reader.getSuggestions();
136 if (name.isEmpty() && !searchParams->author().isEmpty()) {
137 if (videos.isEmpty()) name = searchParams->author();
139 name = videos.first()->author();
140 // also grab the userId
141 userId = videos.first()->userId();
143 emit nameChanged(name);
146 emit gotVideos(videos);
147 emit finished(videos.size());
150 void YTSearch::requestError(QNetworkReply *reply) {
151 emit error(reply->errorString());
154 QString YTSearch::videoIdFromUrl(QString url) {
155 QRegExp re = QRegExp("^.*[\\?&]v=([^&#]+).*$");
156 if (re.exactMatch(url)) return re.cap(1);
157 re = QRegExp("^.*://.*/([^&#\\?]+).*$");
158 if (re.exactMatch(url)) return re.cap(1);
162 QList<QAction*> YTSearch::getActions() {
163 QList<QAction*> channelActions;
164 if (searchParams->author().isEmpty())
165 return channelActions;
166 channelActions << The::globalActions()->value("subscribe-channel");
167 return channelActions;