1 #include "ytjssinglevideosource.h"
6 YTJSSingleVideoSource::YTJSSingleVideoSource(QObject *parent)
7 : VideoSource(parent), video(nullptr) {}
9 void YTJSSingleVideoSource::loadVideos(int max, int startIndex) {
12 auto &ytjs = YTJS::instance();
13 if (!ytjs.isInitialized()) {
14 QTimer::singleShot(500, this, [this, max, startIndex] { loadVideos(max, startIndex); });
17 auto &engine = ytjs.getEngine();
19 auto function = engine.evaluate("videoInfo");
20 if (!function.isCallable()) {
21 qWarning() << function.toString() << " is not callable";
22 emit error(function.toString());
26 if (startIndex == 1) {
29 name = video->getTitle();
30 qDebug() << "Emitting name changed" << name;
31 emit nameChanged(name);
33 emit gotVideos({video->clone()});
37 auto handler = new ResultHandler;
38 connect(handler, &ResultHandler::error, this, &VideoSource::error);
39 connect(handler, &ResultHandler::data, this, [this](const QJsonDocument &doc) {
42 auto obj = doc.object();
44 const auto items = obj["related_videos"].toArray();
45 QVector<Video *> videos;
46 videos.reserve(items.size());
48 for (const auto &i : items) {
49 Video *video = new Video();
51 QString id = i["id"].toString();
54 QString title = i["title"].toString();
55 video->setTitle(title);
57 QString desc = i["description"].toString();
58 if (desc.isEmpty()) desc = i["desc"].toString();
59 video->setDescription(desc);
61 QString thumb = i["video_thumbnail"].toString();
62 video->setThumbnailUrl(thumb);
64 int views = i["view_count"].toInt();
65 video->setViewCount(views);
67 int duration = i["length_seconds"].toInt();
68 video->setViewCount(duration);
70 QString channelId = i["ucid"].toString();
71 video->setChannelId(channelId);
73 QString channelName = i["author"].toString();
74 video->setChannelTitle(channelName);
79 emit gotVideos(videos);
80 emit finished(videos.size());
82 QJSValue h = engine.newQObject(handler);
83 auto value = function.call({h, videoId});
84 ytjs.checkError(value);
87 void YTJSSingleVideoSource::setVideo(Video *video) {
89 videoId = video->getId();
92 void YTJSSingleVideoSource::abort() {
96 QString YTJSSingleVideoSource::getName() {