Skip to content

Commit

Permalink
fix: properly display shipped badges
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Sep 20, 2024
1 parent 61acba5 commit e571073
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 37 deletions.
2 changes: 1 addition & 1 deletion resources/twitch-badges.json

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions scripts/update-badges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys
import json
from pathlib import Path


def trim_version(version):
base_url = version["image_url_1x"].removesuffix("1")
assert version["image_url_2x"] == base_url + "2"
assert version["image_url_4x"] == base_url + "3"
v = {
"id": version["id"],
"title": version["title"],
"image": base_url,
}
if version["click_url"]:
v["url"] = version["click_url"]
return v


raw = sys.stdin.read()
assert len(raw) > 0, "Response from Helix' chat/badges/global needs to be piped"
base = json.loads(raw)["data"]
out = {set["set_id"]: [trim_version(v) for v in set["versions"]] for set in base}

with open(
Path(__file__).parent.parent / "resources" / "twitch-badges.json", mode="w"
) as f:
f.write(json.dumps(out, separators=(",", ":")))
71 changes: 36 additions & 35 deletions src/providers/twitch/TwitchBadges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,57 +79,58 @@ void TwitchBadges::loadTwitchBadges()
break;
}
qCWarning(chatterinoTwitch) << errorMessage;
QFile file(":/twitch-badges.json");
if (!file.open(QFile::ReadOnly))
{
// Despite erroring out, we still want to reach the same point
// Loaded should still be set to true to not build up an endless queue, and the quuee should still be flushed.
qCWarning(chatterinoTwitch)
<< "Error loading Twitch Badges from the local backup file";
this->loaded();
return;
}
auto bytes = file.readAll();
auto doc = QJsonDocument::fromJson(bytes);
this->loadLocalBadges();
});
}

void TwitchBadges::loadLocalBadges()
{
QFile file(":/twitch-badges.json");
if (!file.open(QFile::ReadOnly))
{
// Despite erroring out, we still want to reach the same point
// Loaded should still be set to true to not build up an endless queue, and the quuee should still be flushed.
qCWarning(chatterinoTwitch)
<< "Error loading Twitch Badges from the local backup file";
this->loaded();
return;
}
auto bytes = file.readAll();
auto doc = QJsonDocument::fromJson(bytes);

this->parseTwitchBadges(doc.object());
this->parseTwitchBadges(doc.object());

this->loaded();
});
this->loaded();
}

void TwitchBadges::parseTwitchBadges(QJsonObject root)
void TwitchBadges::parseTwitchBadges(const QJsonObject &root)
{
auto badgeSets = this->badgeSets_.access();

auto jsonSets = root.value("badge_sets").toObject();
for (auto sIt = jsonSets.begin(); sIt != jsonSets.end(); ++sIt)
for (auto setIt = root.begin(); setIt != root.end(); setIt++)
{
auto key = sIt.key();
auto versions = sIt.value().toObject().value("versions").toObject();
auto key = setIt.key();

for (auto vIt = versions.begin(); vIt != versions.end(); ++vIt)
for (auto versionValue : setIt.value().toArray())
{
auto versionObj = vIt.value().toObject();
const auto versionObj = versionValue.toObject();
auto id = versionObj["id"].toString();
auto baseImage = versionObj["image"].toString();
auto emote = Emote{
.name = {""},
.name = {},
.images =
ImageSet{
Image::fromUrl(
{versionObj.value("image_url_1x").toString()}, 1,
BADGE_BASE_SIZE),
Image::fromUrl(
{versionObj.value("image_url_2x").toString()}, .5,
BADGE_BASE_SIZE * 2),
Image::fromUrl(
{versionObj.value("image_url_4x").toString()}, .25,
BADGE_BASE_SIZE * 4),
Image::fromUrl({baseImage + '1'}, 1, BADGE_BASE_SIZE),
Image::fromUrl({baseImage + '2'}, .5,
BADGE_BASE_SIZE * 2),
Image::fromUrl({baseImage + '3'}, .25,
BADGE_BASE_SIZE * 4),
},
.tooltip = Tooltip{versionObj.value("title").toString()},
.homePage = Url{versionObj.value("click_url").toString()},
.tooltip = Tooltip{versionObj["title"].toString()},
.homePage = Url{versionObj["url"].toString()},
};

(*badgeSets)[key][vIt.key()] = std::make_shared<Emote>(emote);
(*badgeSets)[key][id] = std::make_shared<Emote>(emote);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/providers/twitch/TwitchBadges.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ class TwitchBadges

void loadTwitchBadges();

/// Loads the badges shipped with Chatterino (twitch-badges.json)
void loadLocalBadges();

private:
void parseTwitchBadges(QJsonObject root);
void parseTwitchBadges(const QJsonObject &root);
void loaded();
void loadEmoteImage(const QString &name, const ImagePtr &image,
BadgeIconCallback &&callback);
Expand Down

0 comments on commit e571073

Please sign in to comment.