mirror-chatterino2/src/providers/twitch/TwitchBadges.cpp

59 lines
1.8 KiB
C++
Raw Normal View History

2018-08-02 14:23:27 +02:00
#include "TwitchBadges.hpp"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QThread>
#include "common/NetworkRequest.hpp"
namespace chatterino {
TwitchBadges::TwitchBadges()
{
}
void TwitchBadges::initialize(Settings &settings, Paths &paths)
{
this->loadTwitchBadges();
}
void TwitchBadges::loadTwitchBadges()
{
static QString url("https://badges.twitch.tv/v1/badges/global/display?language=en");
NetworkRequest req(url);
req.setCaller(QThread::currentThread());
req.onSuccess([this](auto result) -> Outcome {
auto root = result.parseJson();
QJsonObject sets = root.value("badge_sets").toObject();
for (QJsonObject::iterator it = sets.begin(); it != sets.end(); ++it) {
QJsonObject versions = it.value().toObject().value("versions").toObject();
for (auto versionIt = std::begin(versions); versionIt != std::end(versions);
++versionIt) {
auto emote =
Emote{{""},
ImageSet{
Image::fromUrl({root.value("image_url_1x").toString()}, 1),
Image::fromUrl({root.value("image_url_2x").toString()}, 0.5),
Image::fromUrl({root.value("image_url_4x").toString()}, 0.25),
},
Tooltip{root.value("description").toString()},
Url{root.value("clickURL").toString()}};
// "title"
// "clickAction"
QJsonObject versionObj = versionIt.value().toObject();
this->badges.emplace(versionIt.key(), std::make_shared<Emote>(emote));
}
}
return Success;
});
req.execute();
}
} // namespace chatterino