]> git.sur5r.net Git - minitube/blob - src/ytjs/ytjschannel.cpp
New upstream version 3.6.1
[minitube] / src / ytjs / ytjschannel.cpp
1 #include "ytjschannel.h"
2
3 #include "http.h"
4 #include "httputils.h"
5 #include "ytjs.h"
6
7 YTJSChannel::YTJSChannel(const QString &id, QObject *parent) : QObject(parent) {
8     load(id);
9 }
10
11 void YTJSChannel::load(const QString &channelId) {
12     auto &ytjs = YTJS::instance();
13     if (!ytjs.isInitialized()) {
14         QTimer::singleShot(500, this, [this, channelId] { load(channelId); });
15         return;
16     }
17     auto &engine = ytjs.getEngine();
18
19     auto function = engine.evaluate("channelInfo");
20     if (!function.isCallable()) {
21         qWarning() << function.toString() << " is not callable";
22         emit error(function.toString());
23         return;
24     }
25
26     auto handler = new ResultHandler;
27     connect(handler, &ResultHandler::error, this, &YTJSChannel::error);
28     connect(handler, &ResultHandler::data, this, [this](const QJsonDocument &doc) {
29         auto obj = doc.object();
30
31         displayName = obj["author"].toString();
32         description = obj["description"].toString();
33
34         const auto thumbs = obj["authorThumbnails"].toArray();
35         int maxFoundWidth = 0;
36         for (const auto &thumbObj : thumbs) {
37             QString url = thumbObj["url"].toString();
38             int width = thumbObj["width"].toInt();
39             if (width > maxFoundWidth) {
40                 maxFoundWidth = width;
41                 thumbnailUrl = url;
42             }
43         }
44
45         emit loaded();
46     });
47     QJSValue h = engine.newQObject(handler);
48     auto value = function.call({h, channelId});
49     ytjs.checkError(value);
50 }