]> git.sur5r.net Git - minitube/blob - src/yt/invidious/ivchannelsource.cpp
New upstream version 3.8
[minitube] / src / yt / invidious / ivchannelsource.cpp
1 #include "ivchannelsource.h"
2
3 #include "http.h"
4 #include "httputils.h"
5 #include "invidious.h"
6 #include "ivlistparser.h"
7 #include "mainwindow.h"
8 #include "searchparams.h"
9 #include "video.h"
10
11 namespace {
12 int invidiousFixedMax = 20;
13 }
14
15 IVChannelSource::IVChannelSource(SearchParams *searchParams, QObject *parent)
16     : IVVideoSource(parent), searchParams(searchParams) {
17     searchParams->setParent(this);
18 }
19
20 void IVChannelSource::reallyLoadVideos(int max, int startIndex) {
21     QUrl url = Invidious::instance().method("channels/videos/");
22     if (url.isEmpty()) {
23         QTimer::singleShot(500, this, [this] { handleError("No baseUrl"); });
24         return;
25     }
26     url.setPath(url.path() + searchParams->channelId());
27
28     QUrlQuery q(url);
29
30     int page = ((startIndex - 1) / invidiousFixedMax) + 1;
31     q.addQueryItem("page", QString::number(page));
32
33     switch (searchParams->sortBy()) {
34     case SearchParams::SortByNewest:
35         q.addQueryItem("sort_by", "newest");
36         break;
37     case SearchParams::SortByViewCount:
38         q.addQueryItem("sort_by", "popular");
39         break;
40     }
41
42     url.setQuery(q);
43
44     auto *reply = Invidious::cachedHttp().get(url);
45     connect(reply, &HttpReply::data, this, [this](auto data) {
46         QJsonDocument doc = QJsonDocument::fromJson(data);
47         const QJsonArray items = doc.array();
48         if (items.isEmpty()) {
49             handleError("No videos");
50             return;
51         }
52
53         IVListParser parser(items);
54         const QVector<Video *> &videos = parser.getVideos();
55
56         if (items.size() > invidiousFixedMax) invidiousFixedMax = items.size();
57
58         if (name.isEmpty() && !searchParams->channelId().isEmpty()) {
59             if (!videos.isEmpty()) {
60                 name = videos.at(0)->getChannelTitle();
61                 if (!searchParams->keywords().isEmpty()) {
62                     name += QLatin1String(": ") + searchParams->keywords();
63                 }
64             }
65             emit nameChanged(name);
66         }
67
68         emit gotVideos(videos);
69         emit finished(videos.size());
70     });
71     connect(reply, &HttpReply::error, this, &IVChannelSource::handleError);
72 }
73
74 QString IVChannelSource::getName() {
75     return name;
76 }
77
78 const QList<QAction *> &IVChannelSource::getActions() {
79     static const QList<QAction *> channelActions = {
80             MainWindow::instance()->getAction("subscribeChannel")};
81     if (searchParams->channelId().isEmpty()) {
82         static const QList<QAction *> noActions;
83         return noActions;
84     }
85     return channelActions;
86 }
87
88 int IVChannelSource::maxResults() {
89     return invidiousFixedMax;
90 }