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 auto parseVideoObject = [](QJsonObject i) {
32 Video *video = new Video();
34 QString id = i["id"].toString();
37 QString title = i["title"].toString();
38 video->setTitle(title);
40 QString desc = i["description"].toString();
41 if (desc.isEmpty()) desc = i["desc"].toString();
42 video->setDescription(desc);
44 const auto thumbs = i["thumbnails"].toArray();
45 for (const auto &thumb : thumbs) {
46 QString url = thumb["url"].toString();
47 int width = thumb["width"].toInt();
49 video->setLargeThumbnailUrl(url);
50 else if (width >= 246)
51 video->setMediumThumbnailUrl(url);
52 else if (width >= 168)
53 video->setThumbnailUrl(url);
56 int views = i["view_count"].toInt();
57 video->setViewCount(views);
59 int duration = i["length_seconds"].toInt();
60 video->setViewCount(duration);
62 QString channelId = i["ucid"].toString();
63 video->setChannelId(channelId);
65 QString channelName = i["author"].toString();
66 video->setChannelTitle(channelName);
71 QVector<Video *> videos;
74 // parse video details
75 videos << parseVideoObject(obj["videoDetails"].toObject());
78 const auto items = obj["related_videos"].toArray();
79 videos.reserve(items.size());
81 for (const auto &i : items) {
82 videos << parseVideoObject(i.toObject());
85 if (videos.isEmpty()) {
86 emit error("No results");
88 emit gotVideos(videos);
89 emit finished(videos.size());
92 // fake more videos by loading videos related to the last one
94 if (!videos.isEmpty()) videoId = videos.last()->getId();
96 .onError([this](auto &msg) { emit error(msg); });
99 void YTJSSingleVideoSource::setVideo(Video *video) {
101 videoId = video->getId();
104 void YTJSSingleVideoSource::abort() {
108 QString YTJSSingleVideoSource::getName() {