]> git.sur5r.net Git - minitube/blob - src/yt/ytjs/ytjssearch.cpp
New upstream version 3.8
[minitube] / src / yt / ytjs / ytjssearch.cpp
1 #include "ytjssearch.h"
2
3 #include "mainwindow.h"
4 #include "searchparams.h"
5 #include "video.h"
6 #include "ytsearch.h"
7
8 #include "js.h"
9
10 namespace {
11
12 int parseDuration(const QString &s) {
13     const auto parts = s.splitRef(':');
14     int secs = 0;
15     int p = 0;
16     for (auto i = parts.crbegin(); i != parts.crend(); ++i) {
17         if (p == 0) {
18             secs = i->toInt();
19         } else if (p == 1) {
20             secs += i->toInt() * 60;
21         } else if (p == 2) {
22             secs += i->toInt() * 60 * 60;
23         }
24         p++;
25     }
26     return secs;
27 }
28
29 QString parseChannelId(const QString &channelUrl) {
30     int pos = channelUrl.lastIndexOf('/');
31     if (pos >= 0) return channelUrl.mid(pos + 1);
32     return QString();
33 }
34
35 QDateTime parsePublishedText(const QString &s) {
36     int num = 0;
37     const auto parts = s.splitRef(' ');
38     for (const auto &part : parts) {
39         num = part.toInt();
40         if (num > 0) break;
41     }
42     if (num == 0) return QDateTime();
43
44     auto now = QDateTime::currentDateTimeUtc();
45     if (s.contains("hour")) {
46         return now.addSecs(-num * 3600);
47     } else if (s.contains("day")) {
48         return now.addDays(-num);
49     } else if (s.contains("week")) {
50         return now.addDays(-num * 7);
51     } else if (s.contains("month")) {
52         return now.addMonths(-num);
53     } else if (s.contains("year")) {
54         return now.addDays(-num * 365);
55     }
56     return QDateTime();
57 }
58
59 } // namespace
60
61 YTJSSearch::YTJSSearch(SearchParams *searchParams, QObject *parent)
62     : VideoSource(parent), searchParams(searchParams) {}
63
64 void YTJSSearch::loadVideos(int max, int startIndex) {
65     aborted = false;
66
67     auto &js = JS::instance();
68     auto &engine = js.getEngine();
69
70     QString q;
71     if (!searchParams->keywords().isEmpty()) {
72         if (searchParams->keywords().startsWith("http://") ||
73             searchParams->keywords().startsWith("https://")) {
74             q = YTSearch::videoIdFromUrl(searchParams->keywords());
75         } else
76             q = searchParams->keywords();
77     }
78
79     // Options
80
81     QJSValue options = engine.newObject();
82
83     if (startIndex > 1) {
84         if (!nextpageRef.isEmpty())
85             options.setProperty("nextpageRef", nextpageRef);
86         else {
87             // non-first page was requested but we have no continuation token
88             emit error("No pagination token");
89             return;
90         }
91     }
92     options.setProperty("limit", max);
93
94     switch (searchParams->safeSearch()) {
95     case SearchParams::None:
96         options.setProperty("safeSearch", false);
97         break;
98     case SearchParams::Strict:
99         options.setProperty("safeSearch", true);
100         break;
101     }
102
103     // Filters
104
105     auto filterMap = engine.evaluate("new Map()");
106     auto jsMapSet = filterMap.property("set");
107     auto addFilter = [&filterMap, &jsMapSet](QString name, QString value) {
108         jsMapSet.callWithInstance(filterMap, {name, value});
109     };
110
111     addFilter("Type", "Video");
112
113     switch (searchParams->sortBy()) {
114     case SearchParams::SortByNewest:
115         addFilter("Sort by", "Upload date");
116         break;
117     case SearchParams::SortByViewCount:
118         addFilter("Sort by", "View count");
119         break;
120     case SearchParams::SortByRating:
121         addFilter("Sort by", "Rating");
122         break;
123     }
124
125     switch (searchParams->duration()) {
126     case SearchParams::DurationShort:
127         addFilter("Duration", "Short");
128         break;
129     case SearchParams::DurationMedium:
130     case SearchParams::DurationLong:
131         addFilter("Duration", "Long");
132         break;
133     }
134
135     switch (searchParams->time()) {
136     case SearchParams::TimeToday:
137         addFilter("Upload date", "Today");
138         break;
139     case SearchParams::TimeWeek:
140         addFilter("Upload date", "This week");
141         break;
142     case SearchParams::TimeMonth:
143         addFilter("Upload date", "This month");
144         break;
145     }
146
147     switch (searchParams->quality()) {
148     case SearchParams::QualityHD:
149         addFilter("Features", "HD");
150         break;
151     case SearchParams::Quality4K:
152         addFilter("Features", "4K");
153         break;
154     case SearchParams::QualityHDR:
155         addFilter("Features", "HDR");
156         break;
157     }
158
159     js.callFunction(new JSResult(this), "search", {q, options, filterMap})
160             .onJson([this](auto &doc) {
161                 if (aborted) return;
162
163                 auto obj = doc.object();
164
165                 nextpageRef = obj["nextpageRef"].toString();
166
167                 const auto items = obj["items"].toArray();
168                 QVector<Video *> videos;
169                 videos.reserve(items.size());
170
171                 for (const auto &i : items) {
172                     QString type = i["type"].toString();
173                     if (type != "video") continue;
174
175                     Video *video = new Video();
176
177                     QString id = YTSearch::videoIdFromUrl(i["link"].toString());
178                     video->setId(id);
179
180                     QString title = i["title"].toString();
181                     video->setTitle(title);
182
183                     QString desc = i["description"].toString();
184                     video->setDescription(desc);
185
186                     QString thumb = i["thumbnail"].toString();
187                     video->setThumbnailUrl(thumb);
188
189                     int views = i["views"].toInt();
190                     video->setViewCount(views);
191
192                     int duration = parseDuration(i["duration"].toString());
193                     video->setDuration(duration);
194
195                     auto published = parsePublishedText(i["uploaded_at"].toString());
196                     if (published.isValid()) video->setPublished(published);
197
198                     auto authorObj = i["author"];
199                     QString channelName = authorObj["name"].toString();
200                     video->setChannelTitle(channelName);
201                     QString channelId = parseChannelId(authorObj["ref"].toString());
202                     video->setChannelId(channelId);
203
204                     videos << video;
205                 }
206
207                 if (videos.isEmpty()) {
208                     emit error("No results");
209                 } else {
210                     emit gotVideos(videos);
211                     emit finished(videos.size());
212                 }
213             })
214             .onError([this, &js, max, startIndex](auto &msg) {
215                 static int retries = 0;
216                 if (retries < 3) {
217                     qDebug() << "Retrying...";
218                     auto nam = js.getEngine().networkAccessManager();
219                     nam->clearAccessCache();
220                     nam->setCookieJar(new QNetworkCookieJar());
221                     QTimer::singleShot(0, this,
222                                        [this, max, startIndex] { loadVideos(max, startIndex); });
223                     retries++;
224                 } else {
225                     emit error(msg);
226                 }
227             });
228 }
229
230 QString YTJSSearch::getName() {
231     if (!name.isEmpty()) return name;
232     if (!searchParams->keywords().isEmpty()) return searchParams->keywords();
233     return QString();
234 }
235
236 const QList<QAction *> &YTJSSearch::getActions() {
237     static const QList<QAction *> channelActions = {
238             MainWindow::instance()->getAction("subscribeChannel")};
239     if (searchParams->channelId().isEmpty()) {
240         static const QList<QAction *> noActions;
241         return noActions;
242     }
243     return channelActions;
244 }