]> git.sur5r.net Git - minitube/blob - src/invidious/ivchannelsource.cpp
New upstream version 3.6.1
[minitube] / src / 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     url.setPath(url.path() + searchParams->channelId());
23
24     QUrlQuery q(url);
25
26     int page = ((startIndex - 1) / invidiousFixedMax) + 1;
27     q.addQueryItem("page", QString::number(page));
28
29     switch (searchParams->sortBy()) {
30     case SearchParams::SortByNewest:
31         q.addQueryItem("sort_by", "newest");
32         break;
33     case SearchParams::SortByViewCount:
34         q.addQueryItem("sort_by", "popular");
35         break;
36     }
37
38     url.setQuery(q);
39
40     auto *reply = Invidious::cachedHttp().get(url);
41     connect(reply, &HttpReply::data, this, [this](auto data) {
42         QJsonDocument doc = QJsonDocument::fromJson(data);
43         const QJsonArray items = doc.array();
44         if (items.isEmpty()) {
45             handleError("No videos");
46             return;
47         }
48
49         IVListParser parser(items);
50         const QVector<Video *> &videos = parser.getVideos();
51
52         if (items.size() > invidiousFixedMax) invidiousFixedMax = items.size();
53
54         if (name.isEmpty() && !searchParams->channelId().isEmpty()) {
55             if (!videos.isEmpty()) {
56                 name = videos.at(0)->getChannelTitle();
57                 if (!searchParams->keywords().isEmpty()) {
58                     name += QLatin1String(": ") + searchParams->keywords();
59                 }
60             }
61             emit nameChanged(name);
62         }
63
64         emit gotVideos(videos);
65         emit finished(videos.size());
66     });
67     connect(reply, &HttpReply::error, this, &IVChannelSource::handleError);
68 }
69
70 QString IVChannelSource::getName() {
71     return name;
72 }
73
74 const QList<QAction *> &IVChannelSource::getActions() {
75     static const QList<QAction *> channelActions = {
76             MainWindow::instance()->getAction("subscribeChannel")};
77     if (searchParams->channelId().isEmpty()) {
78         static const QList<QAction *> noActions;
79         return noActions;
80     }
81     return channelActions;
82 }
83
84 int IVChannelSource::maxResults() {
85     return invidiousFixedMax;
86 }