]> git.sur5r.net Git - minitube/blob - src/yt/searchvideosource.cpp
New upstream version 3.8
[minitube] / src / yt / searchvideosource.cpp
1 #include "searchvideosource.h"
2
3 #include "searchparams.h"
4 #include "video.h"
5 #include "videoapi.h"
6
7 #include "ytsearch.h"
8
9 #include "ivchannelsource.h"
10 #include "ivsearch.h"
11 #include "ivsinglevideosource.h"
12
13 #include "ytjschannelsource.h"
14 #include "ytjssearch.h"
15 #include "ytjssinglevideosource.h"
16
17 SearchVideoSource::SearchVideoSource(SearchParams *searchParams, QObject *parent)
18     : VideoSource(parent), searchParams(searchParams) {}
19
20 void SearchVideoSource::loadVideos(int max, int startIndex) {
21     if (!source) {
22         aborted = false;
23         if (VideoAPI::impl() == VideoAPI::YT3) {
24             YTSearch *ytSearch = new YTSearch(searchParams);
25             ytSearch->setAsyncDetails(true);
26             // --- connect(ytSearch, SIGNAL(gotDetails()), playlistModel, SLOT(emitDataChanged()));
27             source = ytSearch;
28         } else if (VideoAPI::impl() == VideoAPI::IV) {
29             if (searchParams->channelId().isEmpty()) {
30                 source = new IVSearch(searchParams);
31             } else {
32                 source = new IVChannelSource(searchParams);
33             }
34         } else if (VideoAPI::impl() == VideoAPI::JS) {
35             if (searchParams->channelId().isEmpty()) {
36                 source = new YTJSSearch(searchParams);
37             } else {
38                 source = new YTJSChannelSource(searchParams);
39             }
40         }
41         connectSource(max, startIndex);
42     }
43     source->loadVideos(max, startIndex);
44 }
45
46 bool SearchVideoSource::hasMoreVideos() {
47     if (source) return source->hasMoreVideos();
48     return VideoSource::hasMoreVideos();
49 }
50
51 QString SearchVideoSource::getName() {
52     if (source) return source->getName();
53     return QString();
54 }
55
56 const QList<QAction *> &SearchVideoSource::getActions() {
57     if (source) return source->getActions();
58     return VideoSource::getActions();
59 }
60
61 int SearchVideoSource::maxResults() {
62     if (source) return source->maxResults();
63     return VideoSource::maxResults();
64 }
65
66 void SearchVideoSource::connectSource(int max, int startIndex) {
67     connect(source, &VideoSource::finished, this, &VideoSource::finished);
68     connect(source, &VideoSource::gotVideos, this, [this](auto &videos) {
69         if (aborted) return;
70         emit gotVideos(videos);
71     });
72     connect(source, &VideoSource::error, this, [this, max, startIndex](auto msg) {
73         qDebug() << source << msg;
74         if (aborted) return;
75         if (QLatin1String(source->metaObject()->className()).startsWith(QLatin1String("YTJS"))) {
76             qDebug() << "Falling back to IV";
77             source->deleteLater();
78             if (searchParams->channelId().isEmpty()) {
79                 source = new IVSearch(searchParams);
80             } else {
81                 source = new IVChannelSource(searchParams);
82             }
83             connectSource(max, startIndex);
84             source->loadVideos(max, startIndex);
85         } else {
86             emit error(msg);
87         }
88     });
89 }