2018-08-02 14:23:27 +02:00
|
|
|
#include "TwitchBadges.hpp"
|
|
|
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QThread>
|
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/Outcome.hpp"
|
|
|
|
#include "messages/Emote.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
TwitchBadges::TwitchBadges()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchBadges::initialize(Settings &settings, Paths &paths)
|
|
|
|
{
|
|
|
|
this->loadTwitchBadges();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
QJsonObject sets = root.value("badge_sets").toObject();
|
|
|
|
|
|
|
|
for (QJsonObject::iterator it = sets.begin(); it != sets.end(); ++it) {
|
2018-08-06 21:17:03 +02:00
|
|
|
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()}};
|
2018-08-02 14:23:27 +02:00
|
|
|
// "title"
|
|
|
|
// "clickAction"
|
|
|
|
|
|
|
|
QJsonObject versionObj = versionIt.value().toObject();
|
2018-08-06 21:17:03 +02:00
|
|
|
this->badges.emplace(versionIt.key(),
|
|
|
|
std::make_shared<Emote>(emote));
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|