]> git.sur5r.net Git - minitube/blob - src/ytsearch.cpp
Merge tag 'upstream/2.3.1'
[minitube] / src / ytsearch.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "ytsearch.h"
22 #include "ytfeedreader.h"
23 #include "constants.h"
24 #include "networkaccess.h"
25 #include "searchparams.h"
26 #include "video.h"
27 #include "ytuser.h"
28
29 namespace The {
30 NetworkAccess* http();
31 QHash<QString, QAction*>* globalActions();
32 }
33
34 YTSearch::YTSearch(SearchParams *searchParams, QObject *parent) :
35     VideoSource(parent),
36     searchParams(searchParams) {
37     searchParams->setParent(this);
38 }
39
40 void YTSearch::loadVideos(int max, int skip) {
41     aborted = false;
42
43     QUrl url("http://gdata.youtube.com/feeds/api/videos/");
44 #if QT_VERSION >= 0x050000
45 {
46     QUrl &u = url;
47     QUrlQuery url;
48 #endif
49
50     url.addQueryItem("v", "2");
51     url.addQueryItem("max-results", QString::number(max));
52     url.addQueryItem("start-index", QString::number(skip));
53
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());
59     }
60
61     if (!searchParams->author().isEmpty())
62         url.addQueryItem("author", searchParams->author());
63
64     switch (searchParams->sortBy()) {
65     case SearchParams::SortByNewest:
66         url.addQueryItem("orderby", "published");
67         break;
68     case SearchParams::SortByViewCount:
69         url.addQueryItem("orderby", "viewCount");
70         break;
71     case SearchParams::SortByRating:
72         url.addQueryItem("orderby", "rating");
73         break;
74     }
75
76     switch (searchParams->duration()) {
77     case SearchParams::DurationShort:
78         url.addQueryItem("duration", "short");
79         break;
80     case SearchParams::DurationMedium:
81         url.addQueryItem("duration", "medium");
82         break;
83     case SearchParams::DurationLong:
84         url.addQueryItem("duration", "long");
85         break;
86     }
87
88     switch (searchParams->time()) {
89     case SearchParams::TimeToday:
90         url.addQueryItem("time", "today");
91         break;
92     case SearchParams::TimeWeek:
93         url.addQueryItem("time", "this_week");
94         break;
95     case SearchParams::TimeMonth:
96         url.addQueryItem("time", "this_month");
97         break;
98     }
99
100     switch (searchParams->quality()) {
101     case SearchParams::QualityHD:
102         url.addQueryItem("hd", "true");
103         break;
104     }
105
106 #if QT_VERSION >= 0x050000
107         u.setQuery(url);
108     }
109 #endif
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*)));
113 }
114
115 void YTSearch::abort() {
116     aborted = true;
117 }
118
119 const QStringList & YTSearch::getSuggestions() {
120     return suggestions;
121 }
122
123 QString YTSearch::getName() {
124     if (!name.isEmpty()) return name;
125     if (!searchParams->keywords().isEmpty()) return searchParams->keywords();
126     return QString();
127 }
128
129 void YTSearch::parseResults(QByteArray data) {
130     if (aborted) return;
131
132     YTFeedReader reader(data);
133     QList<Video*> videos = reader.getVideos();
134     suggestions = reader.getSuggestions();
135
136     if (name.isEmpty() && !searchParams->author().isEmpty()) {
137         if (videos.isEmpty()) name = searchParams->author();
138         else {
139             name = videos.first()->author();
140             // also grab the userId
141             userId = videos.first()->userId();
142         }
143         emit nameChanged(name);
144     }
145
146     emit gotVideos(videos);
147     emit finished(videos.size());
148 }
149
150 void YTSearch::requestError(QNetworkReply *reply) {
151     emit error(reply->errorString());
152 }
153
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);
159     return QString();
160 }
161
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;
168 }