]> git.sur5r.net Git - minitube/blob - src/invidious/ivsinglevideosource.cpp
New upstream version 3.6.1
[minitube] / src / invidious / ivsinglevideosource.cpp
1 #include "ivsinglevideosource.h"
2
3 #include "http.h"
4 #include "httputils.h"
5 #include "video.h"
6
7 #include "invidious.h"
8 #include "ivlistparser.h"
9
10 IVSingleVideoSource::IVSingleVideoSource(QObject *parent)
11     : IVVideoSource(parent), video(nullptr), startIndex(0), max(0) {}
12
13 void IVSingleVideoSource::reallyLoadVideos(int max, int startIndex) {
14     aborted = false;
15     this->startIndex = startIndex;
16     this->max = max;
17
18     QUrl url;
19
20     if (startIndex == 1) {
21         if (video) {
22             QVector<Video *> videos;
23             videos << video->clone();
24             if (name.isEmpty()) {
25                 name = videos.at(0)->getTitle();
26                 qDebug() << "Emitting name changed" << name;
27                 emit nameChanged(name);
28             }
29             emit gotVideos(videos);
30             loadVideos(max - 1, 2);
31             return;
32         }
33
34         url = Invidious::instance().method("videos/");
35         url.setPath(url.path() + videoId);
36
37     } else {
38         url = Invidious::instance().method("videos");
39         url.setPath(url.path() + "/" + videoId);
40     }
41
42     auto reply = Invidious::cachedHttp().get(url);
43     connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
44     connect(reply, &HttpReply::error, this, &IVSingleVideoSource::handleError);
45 }
46
47 void IVSingleVideoSource::parseResults(QByteArray data) {
48     if (aborted) return;
49
50     QJsonDocument doc = QJsonDocument::fromJson(data);
51     const QJsonArray items = doc.object()["recommendedVideos"].toArray();
52
53     IVListParser parser(items);
54     const QVector<Video *> &videos = parser.getVideos();
55
56     emit gotVideos(videos);
57     if (startIndex == 1)
58         loadVideos(max - 1, 2);
59     else if (startIndex == 2)
60         emit finished(videos.size() + 1);
61     else
62         emit finished(videos.size());
63 }
64
65 QString IVSingleVideoSource::getName() {
66     return name;
67 }
68
69 void IVSingleVideoSource::setVideo(Video *video) {
70     this->video = video;
71     videoId = video->getId();
72 }