]> git.sur5r.net Git - minitube/blob - src/invidious/ivsinglevideosource.cpp
ff0c8047d730987161bb3df1d91763e78f0f6b22
[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     : VideoSource(parent), video(nullptr), startIndex(0), max(0) {}
12
13 void IVSingleVideoSource::loadVideos(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     QObject *reply = Invidious::cachedHttp().get(url);
43     connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
44     connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
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     IVListParser parser(items);
53     const QVector<Video *> &videos = parser.getVideos();
54
55     emit gotVideos(videos);
56     if (startIndex == 1)
57         loadVideos(max - 1, 2);
58     else if (startIndex == 2)
59         emit finished(videos.size() + 1);
60     else
61         emit finished(videos.size());
62 }
63
64 void IVSingleVideoSource::abort() {
65     aborted = true;
66 }
67
68 QString IVSingleVideoSource::getName() {
69     return name;
70 }
71
72 void IVSingleVideoSource::setVideo(Video *video) {
73     this->video = video;
74     videoId = video->getId();
75 }
76
77 void IVSingleVideoSource::requestError(const QString &message) {
78     emit error(message);
79 }