]> git.sur5r.net Git - minitube/blob - src/ytjs/ytjssinglevideosource.cpp
New upstream version 3.6.1
[minitube] / src / ytjs / ytjssinglevideosource.cpp
1 #include "ytjssinglevideosource.h"
2
3 #include "video.h"
4 #include "ytjs.h"
5
6 YTJSSingleVideoSource::YTJSSingleVideoSource(QObject *parent)
7     : VideoSource(parent), video(nullptr) {}
8
9 void YTJSSingleVideoSource::loadVideos(int max, int startIndex) {
10     aborted = false;
11
12     auto &ytjs = YTJS::instance();
13     if (!ytjs.isInitialized()) {
14         QTimer::singleShot(500, this, [this, max, startIndex] { loadVideos(max, startIndex); });
15         return;
16     }
17     auto &engine = ytjs.getEngine();
18
19     auto function = engine.evaluate("videoInfo");
20     if (!function.isCallable()) {
21         qWarning() << function.toString() << " is not callable";
22         emit error(function.toString());
23         return;
24     }
25
26     if (startIndex == 1) {
27         if (video) {
28             if (name.isEmpty()) {
29                 name = video->getTitle();
30                 qDebug() << "Emitting name changed" << name;
31                 emit nameChanged(name);
32             }
33             emit gotVideos({video->clone()});
34         }
35     }
36
37     auto handler = new ResultHandler;
38     connect(handler, &ResultHandler::error, this, &VideoSource::error);
39     connect(handler, &ResultHandler::data, this, [this](const QJsonDocument &doc) {
40         if (aborted) return;
41
42         auto obj = doc.object();
43
44         const auto items = obj["related_videos"].toArray();
45         QVector<Video *> videos;
46         videos.reserve(items.size());
47
48         for (const auto &i : items) {
49             Video *video = new Video();
50
51             QString id = i["id"].toString();
52             video->setId(id);
53
54             QString title = i["title"].toString();
55             video->setTitle(title);
56
57             QString desc = i["description"].toString();
58             if (desc.isEmpty()) desc = i["desc"].toString();
59             video->setDescription(desc);
60
61             QString thumb = i["video_thumbnail"].toString();
62             video->setThumbnailUrl(thumb);
63
64             int views = i["view_count"].toInt();
65             video->setViewCount(views);
66
67             int duration = i["length_seconds"].toInt();
68             video->setViewCount(duration);
69
70             QString channelId = i["ucid"].toString();
71             video->setChannelId(channelId);
72
73             QString channelName = i["author"].toString();
74             video->setChannelTitle(channelName);
75
76             videos << video;
77         }
78
79         emit gotVideos(videos);
80         emit finished(videos.size());
81     });
82     QJSValue h = engine.newQObject(handler);
83     auto value = function.call({h, videoId});
84     ytjs.checkError(value);
85 }
86
87 void YTJSSingleVideoSource::setVideo(Video *video) {
88     this->video = video;
89     videoId = video->getId();
90 }
91
92 void YTJSSingleVideoSource::abort() {
93     aborted = true;
94 }
95
96 QString YTJSSingleVideoSource::getName() {
97     return name;
98 }