]> git.sur5r.net Git - minitube/blob - src/invidious/ivchannelsource.cpp
f4865172fe68266098e82ecbf0c2d313854607f3
[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     : VideoSource(parent), searchParams(searchParams) {
17     searchParams->setParent(this);
18 }
19
20 void IVChannelSource::loadVideos(int max, int startIndex) {
21     aborted = false;
22
23     QUrl url = Invidious::instance().method("channels/videos/");
24     url.setPath(url.path() + searchParams->channelId());
25
26     QUrlQuery q(url);
27
28     int page = ((startIndex - 1) / invidiousFixedMax) + 1;
29     q.addQueryItem("page", QString::number(page));
30
31     switch (searchParams->sortBy()) {
32     case SearchParams::SortByNewest:
33         q.addQueryItem("sort_by", "newest");
34         break;
35     case SearchParams::SortByViewCount:
36         q.addQueryItem("sort_by", "popular");
37         break;
38     }
39
40     url.setQuery(q);
41
42     auto *reply = Invidious::cachedHttp().get(url);
43     connect(reply, &HttpReply::data, this, [this](auto data) {
44         QJsonDocument doc = QJsonDocument::fromJson(data);
45         const QJsonArray items = doc.array();
46         IVListParser parser(items);
47         const QVector<Video *> &videos = parser.getVideos();
48
49         if (items.size() > invidiousFixedMax) invidiousFixedMax = items.size();
50
51         if (name.isEmpty() && !searchParams->channelId().isEmpty()) {
52             if (!videos.isEmpty()) {
53                 name = videos.at(0)->getChannelTitle();
54                 if (!searchParams->keywords().isEmpty()) {
55                     name += QLatin1String(": ") + searchParams->keywords();
56                 }
57             }
58             emit nameChanged(name);
59         }
60
61         emit gotVideos(videos);
62         emit finished(videos.size());
63     });
64     connect(reply, &HttpReply::error, this, [this](auto message) {
65         Invidious::instance().initServers();
66         emit error(message);
67     });
68 }
69
70 void IVChannelSource::abort() {
71     aborted = true;
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 }