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

275 lines
6.7 KiB
C++
Raw Normal View History

2018-08-02 14:23:27 +02:00
#include "TwitchParseCheerEmotes.hpp"
#include <rapidjson/document.h>
#include <QString>
#include <vector>
namespace chatterino {
namespace {
template <typename Type>
2018-08-06 21:17:03 +02:00
inline bool ReadValue(const rapidjson::Value &object, const char *key,
Type &out)
2018-08-02 14:23:27 +02:00
{
if (!object.HasMember(key)) {
return false;
}
const auto &value = object[key];
if (!value.Is<Type>()) {
return false;
}
out = value.Get<Type>();
return true;
}
template <>
2018-08-06 21:17:03 +02:00
inline bool ReadValue<QString>(const rapidjson::Value &object, const char *key,
QString &out)
2018-08-02 14:23:27 +02:00
{
if (!object.HasMember(key)) {
return false;
}
const auto &value = object[key];
if (!value.IsString()) {
return false;
}
out = value.GetString();
return true;
}
template <>
2018-08-06 21:17:03 +02:00
inline bool ReadValue<std::vector<QString>>(const rapidjson::Value &object,
const char *key,
2018-08-02 14:23:27 +02:00
std::vector<QString> &out)
{
if (!object.HasMember(key)) {
return false;
}
const auto &value = object[key];
if (!value.IsArray()) {
return false;
}
for (const rapidjson::Value &innerValue : value.GetArray()) {
if (!innerValue.IsString()) {
return false;
}
out.emplace_back(innerValue.GetString());
}
return true;
}
// Parse a single cheermote set (or "action") from the twitch api
2018-08-06 21:17:03 +02:00
inline bool ParseSingleCheermoteSet(JSONCheermoteSet &set,
const rapidjson::Value &action)
2018-08-02 14:23:27 +02:00
{
if (!action.IsObject()) {
return false;
}
if (!ReadValue(action, "prefix", set.prefix)) {
return false;
}
if (!ReadValue(action, "scales", set.scales)) {
return false;
}
if (!ReadValue(action, "backgrounds", set.backgrounds)) {
return false;
}
if (!ReadValue(action, "states", set.states)) {
return false;
}
if (!ReadValue(action, "type", set.type)) {
return false;
}
if (!ReadValue(action, "updated_at", set.updatedAt)) {
return false;
}
if (!ReadValue(action, "priority", set.priority)) {
return false;
}
// Tiers
if (!action.HasMember("tiers")) {
return false;
}
const auto &tiersValue = action["tiers"];
if (!tiersValue.IsArray()) {
return false;
}
for (const rapidjson::Value &tierValue : tiersValue.GetArray()) {
JSONCheermoteSet::CheermoteTier tier;
if (!tierValue.IsObject()) {
return false;
}
if (!ReadValue(tierValue, "min_bits", tier.minBits)) {
return false;
}
if (!ReadValue(tierValue, "id", tier.id)) {
return false;
}
if (!ReadValue(tierValue, "color", tier.color)) {
return false;
}
// Images
if (!tierValue.HasMember("images")) {
return false;
}
const auto &imagesValue = tierValue["images"];
if (!imagesValue.IsObject()) {
return false;
}
// Read images object
for (const auto &imageBackgroundValue : imagesValue.GetObject()) {
QString background = imageBackgroundValue.name.GetString();
bool backgroundExists = false;
for (const auto &bg : set.backgrounds) {
if (background == bg) {
backgroundExists = true;
break;
}
}
if (!backgroundExists) {
continue;
}
2018-08-06 21:17:03 +02:00
const rapidjson::Value &imageBackgroundStates =
imageBackgroundValue.value;
2018-08-02 14:23:27 +02:00
if (!imageBackgroundStates.IsObject()) {
continue;
}
// Read each key which represents a background
2018-08-06 21:17:03 +02:00
for (const auto &imageBackgroundState :
imageBackgroundStates.GetObject()) {
2018-08-02 14:23:27 +02:00
QString state = imageBackgroundState.name.GetString();
bool stateExists = false;
for (const auto &_state : set.states) {
if (state == _state) {
stateExists = true;
break;
}
}
if (!stateExists) {
continue;
}
2018-08-06 21:17:03 +02:00
const rapidjson::Value &imageScalesValue =
imageBackgroundState.value;
2018-08-02 14:23:27 +02:00
if (!imageScalesValue.IsObject()) {
continue;
}
// Read each key which represents a scale
2018-08-06 21:17:03 +02:00
for (const auto &imageScaleValue :
imageScalesValue.GetObject()) {
2018-08-02 14:23:27 +02:00
QString scale = imageScaleValue.name.GetString();
bool scaleExists = false;
for (const auto &_scale : set.scales) {
if (scale == _scale) {
scaleExists = true;
break;
}
}
if (!scaleExists) {
continue;
}
2018-08-06 21:17:03 +02:00
const rapidjson::Value &imageScaleURLValue =
imageScaleValue.value;
2018-08-02 14:23:27 +02:00
if (!imageScaleURLValue.IsString()) {
continue;
}
QString url = imageScaleURLValue.GetString();
bool ok = false;
qreal scaleNumber = scale.toFloat(&ok);
if (!ok) {
continue;
}
qreal chatterinoScale = 1 / scaleNumber;
auto image = Image::fromUrl({url}, chatterinoScale);
// TODO(pajlada): Fill in name and tooltip
tier.images[background][state][scale] = image;
}
}
}
set.tiers.emplace_back(tier);
}
return true;
}
} // namespace
2018-08-06 21:17:03 +02:00
// Look through the results of
// https://api.twitch.tv/kraken/bits/actions?channel_id=11148817 for cheermote
// sets or "Actions" as they are called in the API
2018-08-02 14:23:27 +02:00
std::vector<JSONCheermoteSet> ParseCheermoteSets(const rapidjson::Document &d)
{
std::vector<JSONCheermoteSet> sets;
if (!d.IsObject()) {
return sets;
}
if (!d.HasMember("actions")) {
return sets;
}
const auto &actionsValue = d["actions"];
if (!actionsValue.IsArray()) {
return sets;
}
for (const auto &action : actionsValue.GetArray()) {
JSONCheermoteSet set;
bool res = ParseSingleCheermoteSet(set, action);
if (res) {
sets.emplace_back(set);
}
}
return sets;
}
} // namespace chatterino