1 #include "ytjssinglevideosource.h"
6 YTJSSingleVideoSource::YTJSSingleVideoSource(QObject *parent)
7 : VideoSource(parent), video(nullptr) {}
9 void YTJSSingleVideoSource::loadVideos(int max, int startIndex) {
12 if (startIndex == 1) {
15 name = video->getTitle();
16 qDebug() << "Emitting name changed" << name;
17 emit nameChanged(name);
19 emit gotVideos({video->clone()});
24 .callFunction(new JSResult(this), "videoInfo", {videoId})
25 .onJson([this](auto &doc) {
28 auto obj = doc.object();
29 // qDebug() << doc.toJson();
31 const auto items = obj["related_videos"].toArray();
32 QVector<Video *> videos;
33 videos.reserve(items.size());
35 for (const auto &i : items) {
36 Video *video = new Video();
38 QString id = i["id"].toString();
41 QString title = i["title"].toString();
42 video->setTitle(title);
44 QString desc = i["description"].toString();
45 if (desc.isEmpty()) desc = i["desc"].toString();
46 video->setDescription(desc);
48 const auto thumbs = i["thumbnails"].toArray();
49 for (const auto &thumb : thumbs) {
50 QString url = thumb["url"].toString();
51 int width = thumb["width"].toInt();
53 video->setLargeThumbnailUrl(url);
54 else if (width >= 246)
55 video->setMediumThumbnailUrl(url);
56 else if (width >= 168)
57 video->setThumbnailUrl(url);
60 int views = i["view_count"].toInt();
61 video->setViewCount(views);
63 int duration = i["length_seconds"].toInt();
64 video->setViewCount(duration);
66 QString channelId = i["ucid"].toString();
67 video->setChannelId(channelId);
69 QString channelName = i["author"].toString();
70 video->setChannelTitle(channelName);
75 if (videos.isEmpty()) {
76 emit error("No results");
78 emit gotVideos(videos);
79 emit finished(videos.size());
82 // fake more videos by loading videos related to the last one
84 if (!videos.isEmpty()) videoId = videos.last()->getId();
86 .onError([this](auto &msg) { emit error(msg); });
89 void YTJSSingleVideoSource::setVideo(Video *video) {
91 videoId = video->getId();
94 void YTJSSingleVideoSource::abort() {
98 QString YTJSSingleVideoSource::getName() {