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

78 lines
2.3 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();
2018-10-21 13:43:02 +02:00
for (auto sIt = jsonSets.begin(); sIt != jsonSets.end(); ++sIt)
{
2018-08-14 17:45:17 +02:00
auto key = sIt.key();
auto versions = sIt.value().toObject().value("versions").toObject();
2018-10-21 13:43:02 +02:00
for (auto vIt = versions.begin(); vIt != versions.end(); ++vIt)
{
2018-08-14 17:45:17 +02:00
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{versionObj.value("description").toString()},
Url{versionObj.value("click_url").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);
2018-10-21 13:43:02 +02:00
if (it != badgeSets->end())
{
2018-08-14 17:45:17 +02:00
auto it2 = it->second.find(version);
2018-10-21 13:43:02 +02:00
if (it2 != it->second.end())
{
2018-08-14 17:45:17 +02:00
return it2->second;
}
}
return boost::none;
}
2018-08-02 14:23:27 +02:00
} // namespace chatterino