From f94b44188d2ca862e09ebb558ba18affc561acbe Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Tue, 3 Sep 2019 11:27:30 +0200 Subject: [PATCH] Use BTTV V3 API Fixes #1275 --- src/providers/bttv/BttvEmotes.cpp | 53 ++++++++++++++++---------- src/providers/bttv/BttvEmotes.hpp | 4 +- src/providers/twitch/TwitchChannel.cpp | 4 +- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/providers/bttv/BttvEmotes.cpp b/src/providers/bttv/BttvEmotes.cpp index ef3ebfb06..9737568d7 100644 --- a/src/providers/bttv/BttvEmotes.cpp +++ b/src/providers/bttv/BttvEmotes.cpp @@ -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 parseGlobalEmotes( const QJsonObject &jsonRoot, const EmoteMap ¤tEmotes) { @@ -60,28 +68,33 @@ namespace { std::pair parseChannelEmotes(const QJsonObject &jsonRoot) { auto emotes = EmoteMap(); - auto jsonEmotes = jsonRoot.value("emotes").toArray(); - auto urlTemplate = "https:" + jsonRoot.value("urlTemplate").toString(); - for (auto jsonEmote_ : jsonEmotes) - { - auto jsonEmote = jsonEmote_.toObject(); + auto innerParse = [&jsonRoot, &emotes](const char *key) { + auto jsonEmotes = jsonRoot.value(key).toArray(); + for (auto jsonEmote_ : jsonEmotes) + { + auto jsonEmote = jsonEmote_.toObject(); - auto id = EmoteId{jsonEmote.value("id").toString()}; - auto name = EmoteName{jsonEmote.value("code").toString()}; - // emoteObject.value("imageType").toString(); + auto id = EmoteId{jsonEmote.value("id").toString()}; + auto name = EmoteName{jsonEmote.value("code").toString()}; + // emoteObject.value("imageType").toString(); - 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)}, - Tooltip{name.string + "
Channel BetterTTV Emote"}, - Url{"https://manage.betterttv.net/emotes/" + id.string}}); + auto emote = Emote( + {name, + ImageSet{ + Image::fromUrl(getEmoteLinkV3(id, "1x"), 1), + Image::fromUrl(getEmoteLinkV3(id, "2x"), 0.5), + Image::fromUrl(getEmoteLinkV3(id, "3x"), 0.25), + }, + Tooltip{name.string + "
Channel BetterTTV Emote"}, + Url{"https://manage.betterttv.net/emotes/" + id.string}}); - emotes[name] = cachedOrMake(std::move(emote), id); - } + 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 callback) { - NetworkRequest(QString(bttvChannelEmoteApiUrl) + channelName) + NetworkRequest(QString(bttvChannelEmoteApiUrl) + channelId) .timeout(3000) .onSuccess([callback = std::move(callback)](auto result) -> Outcome { auto pair = parseChannelEmotes(result.parseJson()); diff --git a/src/providers/bttv/BttvEmotes.hpp b/src/providers/bttv/BttvEmotes.hpp index c82cb67a4..c5cd3a081 100644 --- a/src/providers/bttv/BttvEmotes.hpp +++ b/src/providers/bttv/BttvEmotes.hpp @@ -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 emotes() const; boost::optional emote(const EmoteName &name) const; void loadEmotes(); - static void loadChannel(const QString &channelName, + static void loadChannel(const QString &channelId, std::function callback); private: diff --git a/src/providers/twitch/TwitchChannel.cpp b/src/providers/twitch/TwitchChannel.cpp index 1c6b5edaa..97f3b6e06 100644 --- a/src/providers/twitch/TwitchChannel.cpp +++ b/src/providers/twitch/TwitchChannel.cpp @@ -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(this)](auto &&emoteMap) { + this->roomId(), [this, weak = weakOf(this)](auto &&emoteMap) { if (auto shared = weak.lock()) this->bttvEmotes_.set( std::make_shared(std::move(emoteMap)));