]> git.sur5r.net Git - minitube/blob - src/ytsearch.cpp
Imported Upstream version 2.1.3
[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     url.addQueryItem("v", "2");
46
47     url.addQueryItem("max-results", QString::number(max));
48     url.addQueryItem("start-index", QString::number(skip));
49
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());
55     }
56
57     if (!searchParams->author().isEmpty())
58         url.addQueryItem("author", searchParams->author());
59
60     switch (searchParams->sortBy()) {
61     case SearchParams::SortByNewest:
62         url.addQueryItem("orderby", "published");
63         break;
64     case SearchParams::SortByViewCount:
65         url.addQueryItem("orderby", "viewCount");
66         break;
67     case SearchParams::SortByRating:
68         url.addQueryItem("orderby", "rating");
69         break;
70     }
71
72     switch (searchParams->duration()) {
73     case SearchParams::DurationShort:
74         url.addQueryItem("duration", "short");
75         break;
76     case SearchParams::DurationMedium:
77         url.addQueryItem("duration", "medium");
78         break;
79     case SearchParams::DurationLong:
80         url.addQueryItem("duration", "long");
81         break;
82     }
83
84     switch (searchParams->time()) {
85     case SearchParams::TimeToday:
86         url.addQueryItem("time", "today");
87         break;
88     case SearchParams::TimeWeek:
89         url.addQueryItem("time", "this_week");
90         break;
91     case SearchParams::TimeMonth:
92         url.addQueryItem("time", "this_month");
93         break;
94     }
95
96     switch (searchParams->quality()) {
97     case SearchParams::QualityHD:
98         url.addQueryItem("hd", "true");
99         break;
100     }
101
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*)));
105 }
106
107 void YTSearch::abort() {
108     aborted = true;
109 }
110
111 const QStringList & YTSearch::getSuggestions() {
112     return suggestions;
113 }
114
115 QString YTSearch::getName() {
116     if (!name.isEmpty()) return name;
117     if (!searchParams->keywords().isEmpty()) return searchParams->keywords();
118     return QString();
119 }
120
121 void YTSearch::parseResults(QByteArray data) {
122     if (aborted) return;
123
124     YTFeedReader reader(data);
125     QList<Video*> videos = reader.getVideos();
126     suggestions = reader.getSuggestions();
127
128     if (name.isEmpty() && !searchParams->author().isEmpty()) {
129         if (videos.isEmpty()) name = searchParams->author();
130         else {
131             name = videos.first()->author();
132             // also grab the userId
133             userId = videos.first()->userId();
134         }
135         emit nameChanged(name);
136     }
137
138     emit gotVideos(videos);
139     emit finished(videos.size());
140 }
141
142 void YTSearch::requestError(QNetworkReply *reply) {
143     emit error(reply->errorString());
144 }
145
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);
151     return QString();
152 }
153
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;
160 }