]> git.sur5r.net Git - minitube/blob - src/yt/ytjs/ytjssinglevideosource.cpp
New upstream version 3.8
[minitube] / src / yt / ytjs / ytjssinglevideosource.cpp
1 #include "ytjssinglevideosource.h"
2
3 #include "js.h"
4 #include "video.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     if (startIndex == 1) {
13         if (video) {
14             if (name.isEmpty()) {
15                 name = video->getTitle();
16                 qDebug() << "Emitting name changed" << name;
17                 emit nameChanged(name);
18             }
19             emit gotVideos({video->clone()});
20         }
21     }
22
23     JS::instance()
24             .callFunction(new JSResult(this), "videoInfo", {videoId})
25             .onJson([this](auto &doc) {
26                 if (aborted) return;
27
28                 auto obj = doc.object();
29                 // qDebug() << doc.toJson();
30
31                 const auto items = obj["related_videos"].toArray();
32                 QVector<Video *> videos;
33                 videos.reserve(items.size());
34
35                 for (const auto &i : items) {
36                     Video *video = new Video();
37
38                     QString id = i["id"].toString();
39                     video->setId(id);
40
41                     QString title = i["title"].toString();
42                     video->setTitle(title);
43
44                     QString desc = i["description"].toString();
45                     if (desc.isEmpty()) desc = i["desc"].toString();
46                     video->setDescription(desc);
47
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();
52                         if (width >= 336)
53                             video->setLargeThumbnailUrl(url);
54                         else if (width >= 246)
55                             video->setMediumThumbnailUrl(url);
56                         else if (width >= 168)
57                             video->setThumbnailUrl(url);
58                     }
59
60                     int views = i["view_count"].toInt();
61                     video->setViewCount(views);
62
63                     int duration = i["length_seconds"].toInt();
64                     video->setViewCount(duration);
65
66                     QString channelId = i["ucid"].toString();
67                     video->setChannelId(channelId);
68
69                     QString channelName = i["author"].toString();
70                     video->setChannelTitle(channelName);
71
72                     videos << video;
73                 }
74
75                 if (videos.isEmpty()) {
76                     emit error("No results");
77                 } else {
78                     emit gotVideos(videos);
79                     emit finished(videos.size());
80                 }
81
82                 // fake more videos by loading videos related to the last one
83                 video = nullptr;
84                 if (!videos.isEmpty()) videoId = videos.last()->getId();
85             })
86             .onError([this](auto &msg) { emit error(msg); });
87 }
88
89 void YTJSSingleVideoSource::setVideo(Video *video) {
90     this->video = video;
91     videoId = video->getId();
92 }
93
94 void YTJSSingleVideoSource::abort() {
95     aborted = true;
96 }
97
98 QString YTJSSingleVideoSource::getName() {
99     return name;
100 }