]> git.sur5r.net Git - minitube/blob - src/ytjs/ytjssinglevideosource.cpp
b63a133c5df86b54b4c444872c917386f9013d21
[minitube] / src / ytjs / ytjssinglevideosource.cpp
1 #include "ytjssinglevideosource.h"
2
3 #include "video.h"
4 #include "ytjs.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     auto &ytjs = YTJS::instance();
13     if (!ytjs.isInitialized()) {
14         QTimer::singleShot(500, this, [this, max, startIndex] { loadVideos(max, startIndex); });
15         return;
16     }
17     auto &engine = ytjs.getEngine();
18
19     auto function = engine.evaluate("videoInfo");
20     if (!function.isCallable()) {
21         qWarning() << function.toString() << " is not callable";
22         emit error(function.toString());
23         return;
24     }
25
26     if (startIndex == 1) {
27         if (video) {
28             if (name.isEmpty()) {
29                 name = video->getTitle();
30                 qDebug() << "Emitting name changed" << name;
31                 emit nameChanged(name);
32             }
33             emit gotVideos({video->clone()});
34         }
35     }
36
37     auto handler = new ResultHandler;
38     connect(handler, &ResultHandler::error, this, &VideoSource::error);
39     connect(handler, &ResultHandler::data, this, [this](const QJsonDocument &doc) {
40         if (aborted) return;
41
42         auto obj = doc.object();
43
44         QFile jsonFile("/Users/flavio/test.json");
45         jsonFile.open(QFile::WriteOnly);
46         jsonFile.write(doc.toJson());
47
48         const auto items = obj["related_videos"].toArray();
49         QVector<Video *> videos;
50         videos.reserve(items.size());
51
52         for (const auto &i : items) {
53             Video *video = new Video();
54
55             QString id = i["id"].toString();
56             video->setId(id);
57
58             QString title = i["title"].toString();
59             video->setTitle(title);
60
61             QString desc = i["description"].toString();
62             if (desc.isEmpty()) desc = i["desc"].toString();
63             video->setDescription(desc);
64
65             QString thumb = i["video_thumbnail"].toString();
66             video->setThumbnailUrl(thumb);
67
68             int views = i["view_count"].toInt();
69             video->setViewCount(views);
70
71             int duration = i["length_seconds"].toInt();
72             video->setViewCount(duration);
73
74             QString channelId = i["ucid"].toString();
75             video->setChannelId(channelId);
76
77             QString channelName = i["author"].toString();
78             video->setChannelTitle(channelName);
79
80             videos << video;
81         }
82
83         emit gotVideos(videos);
84         emit finished(videos.size());
85     });
86     QJSValue h = engine.newQObject(handler);
87     auto value = function.call({h, videoId});
88     ytjs.checkError(value);
89 }
90
91 void YTJSSingleVideoSource::setVideo(Video *video) {
92     this->video = video;
93     videoId = video->getId();
94 }
95
96 void YTJSSingleVideoSource::abort() {
97     aborted = true;
98 }
99
100 QString YTJSSingleVideoSource::getName() {
101     return name;
102 }