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

74 lines
2.2 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>
2018-08-14 17:45:17 +02:00
2018-08-02 14:23:27 +02:00
#include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
2018-08-14 17:45:17 +02:00
#include "debug/Log.hpp"
#include "messages/Emote.hpp"
2018-08-02 14:23:27 +02:00
namespace chatterino {
void TwitchBadges::loadTwitchBadges()
{
2018-08-06 21:17:03 +02:00
static QString url(
"https://badges.twitch.tv/v1/badges/global/display?language=en");
2018-08-02 14:23:27 +02:00
NetworkRequest req(url);
req.setCaller(QThread::currentThread());
req.onSuccess([this](auto result) -> Outcome {
auto root = result.parseJson();
2018-08-14 17:45:17 +02:00
auto badgeSets = this->badgeSets_.access();
2018-08-02 14:23:27 +02:00
2018-08-14 17:45:17 +02:00
auto jsonSets = root.value("badge_sets").toObject();
for (auto sIt = jsonSets.begin(); sIt != jsonSets.end(); ++sIt) {
auto key = sIt.key();
auto versions = sIt.value().toObject().value("versions").toObject();
for (auto vIt = versions.begin(); vIt != versions.end(); ++vIt) {
auto versionObj = vIt.value().toObject();
2018-08-06 21:17:03 +02:00
auto emote = Emote{
{""},
ImageSet{
2018-08-14 17:45:17 +02:00
Image::fromUrl(
{versionObj.value("image_url_1x").toString()}, 1),
Image::fromUrl(
{versionObj.value("image_url_2x").toString()}, .5),
Image::fromUrl(
{versionObj.value("image_url_4x").toString()}, .25),
2018-08-06 21:17:03 +02:00
},
Tooltip{root.value("description").toString()},
Url{root.value("clickURL").toString()}};
2018-08-02 14:23:27 +02:00
// "title"
// "clickAction"
2018-08-14 17:45:17 +02:00
(*badgeSets)[key][vIt.key()] = std::make_shared<Emote>(emote);
2018-08-02 14:23:27 +02:00
}
}
return Success;
});
req.execute();
}
2018-08-14 17:45:17 +02:00
boost::optional<EmotePtr> TwitchBadges::badge(const QString &set,
const QString &version) const
{
auto badgeSets = this->badgeSets_.access();
auto it = badgeSets->find(set);
if (it != badgeSets->end()) {
auto it2 = it->second.find(version);
if (it2 != it->second.end()) {
return it2->second;
}
}
return boost::none;
}
2018-08-02 14:23:27 +02:00
} // namespace chatterino