From 9ee286f60f2714f2b48a1343e31a8227372a1e83 Mon Sep 17 00:00:00 2001 From: fourtf Date: Tue, 3 Sep 2019 23:32:22 +0200 Subject: [PATCH] Fixes #1279 bttv global V3 --- src/common/NetworkResult.cpp | 11 +++++++++++ src/common/NetworkResult.hpp | 6 ++++++ src/providers/bttv/BttvEmotes.cpp | 28 ++++++++++++---------------- src/providers/bttv/BttvEmotes.hpp | 2 +- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/common/NetworkResult.cpp b/src/common/NetworkResult.cpp index 4051b4f9e..25fa24831 100644 --- a/src/common/NetworkResult.cpp +++ b/src/common/NetworkResult.cpp @@ -24,6 +24,17 @@ QJsonObject NetworkResult::parseJson() const return jsonDoc.object(); } +QJsonArray NetworkResult::parseJsonArray() const +{ + QJsonDocument jsonDoc(QJsonDocument::fromJson(this->data_)); + if (jsonDoc.isNull()) + { + return QJsonArray{}; + } + + return jsonDoc.array(); +} + rapidjson::Document NetworkResult::parseRapidJson() const { rapidjson::Document ret(rapidjson::kObjectType); diff --git a/src/common/NetworkResult.hpp b/src/common/NetworkResult.hpp index 36a23de22..df8e28d06 100644 --- a/src/common/NetworkResult.hpp +++ b/src/common/NetworkResult.hpp @@ -10,7 +10,13 @@ class NetworkResult public: NetworkResult(const QByteArray &data); + /// Parses the result as json and returns the root as an object. + /// Returns empty object if parsing failed. QJsonObject parseJson() const; + /// Parses the result as json and returns the root as an array. + /// Returns empty object if parsing failed. + QJsonArray parseJsonArray() const; + /// Parses the result as json and returns the document. rapidjson::Document parseRapidJson() const; const QByteArray &getData() const; diff --git a/src/providers/bttv/BttvEmotes.cpp b/src/providers/bttv/BttvEmotes.cpp index 9737568d7..6b7c9ee2f 100644 --- a/src/providers/bttv/BttvEmotes.cpp +++ b/src/providers/bttv/BttvEmotes.cpp @@ -29,13 +29,17 @@ namespace { return {urlTemplate.arg(id.string, emoteScale)}; } + EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id) + { + static std::unordered_map> cache; + static std::mutex mutex; + + return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id); + } std::pair parseGlobalEmotes( - const QJsonObject &jsonRoot, const EmoteMap ¤tEmotes) + const QJsonArray &jsonEmotes, const EmoteMap ¤tEmotes) { auto emotes = EmoteMap(); - auto jsonEmotes = jsonRoot.value("emotes").toArray(); - auto urlTemplate = - qS("https:") + jsonRoot.value("urlTemplate").toString(); for (auto jsonEmote : jsonEmotes) { @@ -45,10 +49,9 @@ 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)}, + ImageSet{Image::fromUrl(getEmoteLinkV3(id, "1x"), 1), + Image::fromUrl(getEmoteLinkV3(id, "2x"), 0.5), + Image::fromUrl(getEmoteLinkV3(id, "3x"), 0.25)}, Tooltip{name.string + "
Global BetterTTV Emote"}, Url{"https://manage.betterttv.net/emotes/" + id.string}}); @@ -58,13 +61,6 @@ namespace { return {Success, std::move(emotes)}; } - EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id) - { - static std::unordered_map> cache; - static std::mutex mutex; - - return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id); - } std::pair parseChannelEmotes(const QJsonObject &jsonRoot) { auto emotes = EmoteMap(); @@ -129,7 +125,7 @@ void BttvEmotes::loadEmotes() .timeout(30000) .onSuccess([this](auto result) -> Outcome { auto emotes = this->global_.get(); - auto pair = parseGlobalEmotes(result.parseJson(), *emotes); + auto pair = parseGlobalEmotes(result.parseJsonArray(), *emotes); if (pair.first) this->global_.set( std::make_shared(std::move(pair.second))); diff --git a/src/providers/bttv/BttvEmotes.hpp b/src/providers/bttv/BttvEmotes.hpp index c5cd3a081..9c16edeff 100644 --- a/src/providers/bttv/BttvEmotes.hpp +++ b/src/providers/bttv/BttvEmotes.hpp @@ -14,7 +14,7 @@ class EmoteMap; class BttvEmotes final { static constexpr const char *globalEmoteApiUrl = - "https://api.betterttv.net/2/emotes"; + "https://api.betterttv.net/3/cached/emotes/global"; static constexpr const char *bttvChannelEmoteApiUrl = "https://api.betterttv.net/3/cached/users/twitch/";