1 #include "ytjschannel.h"
7 YTJSChannel::YTJSChannel(const QString &id, QObject *parent) : QObject(parent) {
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); });
17 auto &engine = ytjs.getEngine();
19 auto function = engine.evaluate("channelInfo");
20 if (!function.isCallable()) {
21 qWarning() << function.toString() << " is not callable";
22 emit error(function.toString());
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();
31 displayName = obj["author"].toString();
32 description = obj["description"].toString();
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;
47 QJSValue h = engine.newQObject(handler);
48 auto value = function.call({h, channelId});
49 ytjs.checkError(value);