1 #include "ivsinglevideosource.h"
8 #include "ivlistparser.h"
10 IVSingleVideoSource::IVSingleVideoSource(QObject *parent)
11 : IVVideoSource(parent), video(nullptr), startIndex(0), max(0) {}
13 void IVSingleVideoSource::reallyLoadVideos(int max, int startIndex) {
15 this->startIndex = startIndex;
20 if (startIndex == 1) {
22 QVector<Video *> videos;
23 videos << video->clone();
25 name = videos.at(0)->getTitle();
26 qDebug() << "Emitting name changed" << name;
27 emit nameChanged(name);
29 emit gotVideos(videos);
30 loadVideos(max - 1, 2);
34 url = Invidious::instance().method("videos/");
36 QTimer::singleShot(500, this, [this] { handleError("No baseUrl"); });
39 url.setPath(url.path() + videoId);
42 url = Invidious::instance().method("videos");
44 QTimer::singleShot(500, this, [this] { handleError("No baseUrl"); });
47 url.setPath(url.path() + "/" + videoId);
50 auto reply = Invidious::cachedHttp().get(url);
51 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
52 connect(reply, &HttpReply::error, this, &IVSingleVideoSource::handleError);
55 void IVSingleVideoSource::parseResults(QByteArray data) {
58 QJsonDocument doc = QJsonDocument::fromJson(data);
59 const QJsonArray items = doc.object()["recommendedVideos"].toArray();
61 IVListParser parser(items);
62 auto videos = parser.getVideos();
64 emit gotVideos(videos);
66 loadVideos(max - 1, 2);
67 else if (startIndex == 2)
68 emit finished(videos.size() + 1);
70 emit finished(videos.size());
72 // fake more videos by loading videos related to the last one
74 if (!videos.isEmpty()) videoId = videos.last()->getId();
77 QString IVSingleVideoSource::getName() {
81 void IVSingleVideoSource::setVideo(Video *video) {
83 videoId = video->getId();