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 url.addQueryItem("v", "2");
47 url.addQueryItem("max-results", QString::number(max));
48 url.addQueryItem("start-index", QString::number(skip));
50 if (!searchParams->keywords().isEmpty()) {
51 if (searchParams->keywords().startsWith("http://") ||
52 searchParams->keywords().startsWith("https://")) {
53 url.addQueryItem("q", YTSearch::videoIdFromUrl(searchParams->keywords()));
54 } else url.addQueryItem("q", searchParams->keywords());
57 if (!searchParams->author().isEmpty())
58 url.addQueryItem("author", searchParams->author());
60 switch (searchParams->sortBy()) {
61 case SearchParams::SortByNewest:
62 url.addQueryItem("orderby", "published");
64 case SearchParams::SortByViewCount:
65 url.addQueryItem("orderby", "viewCount");
67 case SearchParams::SortByRating:
68 url.addQueryItem("orderby", "rating");
72 switch (searchParams->duration()) {
73 case SearchParams::DurationShort:
74 url.addQueryItem("duration", "short");
76 case SearchParams::DurationMedium:
77 url.addQueryItem("duration", "medium");
79 case SearchParams::DurationLong:
80 url.addQueryItem("duration", "long");
84 switch (searchParams->time()) {
85 case SearchParams::TimeToday:
86 url.addQueryItem("time", "today");
88 case SearchParams::TimeWeek:
89 url.addQueryItem("time", "this_week");
91 case SearchParams::TimeMonth:
92 url.addQueryItem("time", "this_month");
96 switch (searchParams->quality()) {
97 case SearchParams::QualityHD:
98 url.addQueryItem("hd", "true");
102 QObject *reply = The::http()->get(url);
103 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
104 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
107 void YTSearch::abort() {
111 const QStringList & YTSearch::getSuggestions() {
115 QString YTSearch::getName() {
116 if (!name.isEmpty()) return name;
117 if (!searchParams->keywords().isEmpty()) return searchParams->keywords();
121 void YTSearch::parseResults(QByteArray data) {
124 YTFeedReader reader(data);
125 QList<Video*> videos = reader.getVideos();
126 suggestions = reader.getSuggestions();
128 if (name.isEmpty() && !searchParams->author().isEmpty()) {
129 if (videos.isEmpty()) name = searchParams->author();
131 name = videos.first()->author();
132 // also grab the userId
133 userId = videos.first()->userId();
135 emit nameChanged(name);
138 emit gotVideos(videos);
139 emit finished(videos.size());
142 void YTSearch::requestError(QNetworkReply *reply) {
143 emit error(reply->errorString());
146 QString YTSearch::videoIdFromUrl(QString url) {
147 QRegExp re = QRegExp("^.*[\\?&]v=([^&#]+).*$");
148 if (re.exactMatch(url)) return re.cap(1);
149 re = QRegExp("^.*://.*/([^&#\\?]+).*$");
150 if (re.exactMatch(url)) return re.cap(1);
154 QList<QAction*> YTSearch::getActions() {
155 QList<QAction*> channelActions;
156 if (searchParams->author().isEmpty())
157 return channelActions;
158 channelActions << The::globalActions()->value("subscribe-channel");
159 return channelActions;