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 QFile jsonFile("/Users/flavio/test.json");
45 jsonFile.open(QFile::WriteOnly);
46 jsonFile.write(doc.toJson());
48 const auto items = obj["related_videos"].toArray();
49 QVector<Video *> videos;
50 videos.reserve(items.size());
52 for (const auto &i : items) {
53 Video *video = new Video();
55 QString id = i["id"].toString();
58 QString title = i["title"].toString();
59 video->setTitle(title);
61 QString desc = i["description"].toString();
62 if (desc.isEmpty()) desc = i["desc"].toString();
63 video->setDescription(desc);
65 QString thumb = i["video_thumbnail"].toString();
66 video->setThumbnailUrl(thumb);
68 int views = i["view_count"].toInt();
69 video->setViewCount(views);
71 int duration = i["length_seconds"].toInt();
72 video->setViewCount(duration);
74 QString channelId = i["ucid"].toString();
75 video->setChannelId(channelId);
77 QString channelName = i["author"].toString();
78 video->setChannelTitle(channelName);
83 emit gotVideos(videos);
84 emit finished(videos.size());
86 QJSValue h = engine.newQObject(handler);
87 auto value = function.call({h, videoId});
88 ytjs.checkError(value);
91 void YTJSSingleVideoSource::setVideo(Video *video) {
93 videoId = video->getId();
96 void YTJSSingleVideoSource::abort() {
100 QString YTJSSingleVideoSource::getName() {