Use BTTV V3 API

Fixes #1275
This commit is contained in:
Rasmus Karlsson 2019-09-03 11:27:30 +02:00 committed by fourtf
parent 1c0ed6b1aa
commit f94b44188d
3 changed files with 37 additions and 24 deletions

View file

@ -21,6 +21,14 @@ namespace {
return {urlTemplate.replace("{{id}}", id.string)
.replace("{{image}}", emoteScale)};
}
Url getEmoteLinkV3(const EmoteId &id, const QString &emoteScale)
{
static const QString urlTemplate(
"https://cdn.betterttv.net/emote/%1/%2");
return {urlTemplate.arg(id.string, emoteScale)};
}
std::pair<Outcome, EmoteMap> parseGlobalEmotes(
const QJsonObject &jsonRoot, const EmoteMap &currentEmotes)
{
@ -60,9 +68,9 @@ namespace {
std::pair<Outcome, EmoteMap> parseChannelEmotes(const QJsonObject &jsonRoot)
{
auto emotes = EmoteMap();
auto jsonEmotes = jsonRoot.value("emotes").toArray();
auto urlTemplate = "https:" + jsonRoot.value("urlTemplate").toString();
auto innerParse = [&jsonRoot, &emotes](const char *key) {
auto jsonEmotes = jsonRoot.value(key).toArray();
for (auto jsonEmote_ : jsonEmotes)
{
auto jsonEmote = jsonEmote_.toObject();
@ -74,14 +82,19 @@ namespace {
auto emote = Emote(
{name,
ImageSet{
Image::fromUrl(getEmoteLink(urlTemplate, id, "1x"), 1),
Image::fromUrl(getEmoteLink(urlTemplate, id, "2x"), 0.5),
Image::fromUrl(getEmoteLink(urlTemplate, id, "3x"), 0.25)},
Image::fromUrl(getEmoteLinkV3(id, "1x"), 1),
Image::fromUrl(getEmoteLinkV3(id, "2x"), 0.5),
Image::fromUrl(getEmoteLinkV3(id, "3x"), 0.25),
},
Tooltip{name.string + "<br />Channel BetterTTV Emote"},
Url{"https://manage.betterttv.net/emotes/" + id.string}});
emotes[name] = cachedOrMake(std::move(emote), id);
}
};
innerParse("channelEmotes");
innerParse("sharedEmotes");
return {Success, std::move(emotes)};
}
@ -125,10 +138,10 @@ void BttvEmotes::loadEmotes()
.execute();
}
void BttvEmotes::loadChannel(const QString &channelName,
void BttvEmotes::loadChannel(const QString &channelId,
std::function<void(EmoteMap &&)> callback)
{
NetworkRequest(QString(bttvChannelEmoteApiUrl) + channelName)
NetworkRequest(QString(bttvChannelEmoteApiUrl) + channelId)
.timeout(3000)
.onSuccess([callback = std::move(callback)](auto result) -> Outcome {
auto pair = parseChannelEmotes(result.parseJson());

View file

@ -16,7 +16,7 @@ class BttvEmotes final
static constexpr const char *globalEmoteApiUrl =
"https://api.betterttv.net/2/emotes";
static constexpr const char *bttvChannelEmoteApiUrl =
"https://api.betterttv.net/2/channels/";
"https://api.betterttv.net/3/cached/users/twitch/";
public:
BttvEmotes();
@ -24,7 +24,7 @@ public:
std::shared_ptr<const EmoteMap> emotes() const;
boost::optional<EmotePtr> emote(const EmoteName &name) const;
void loadEmotes();
static void loadChannel(const QString &channelName,
static void loadChannel(const QString &channelId,
std::function<void(EmoteMap &&)> callback);
private:

View file

@ -114,6 +114,7 @@ TwitchChannel::TwitchChannel(const QString &name,
this->refreshBadges();
this->refreshCheerEmotes();
this->refreshFFZChannelEmotes();
this->refreshBTTVChannelEmotes();
});
// timers
@ -136,7 +137,6 @@ TwitchChannel::TwitchChannel(const QString &name,
void TwitchChannel::initialize()
{
this->refreshChatters();
this->refreshBTTVChannelEmotes();
this->refreshBadges();
this->ffzCustomModBadge_.loadCustomModBadge();
}
@ -154,7 +154,7 @@ bool TwitchChannel::canSendMessage() const
void TwitchChannel::refreshBTTVChannelEmotes()
{
BttvEmotes::loadChannel(
this->getName(), [this, weak = weakOf<Channel>(this)](auto &&emoteMap) {
this->roomId(), [this, weak = weakOf<Channel>(this)](auto &&emoteMap) {
if (auto shared = weak.lock())
this->bttvEmotes_.set(
std::make_shared<EmoteMap>(std::move(emoteMap)));