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