Fixes #1279 bttv global V3

This commit is contained in:
fourtf 2019-09-03 23:32:22 +02:00
parent 69aa5db2be
commit 9ee286f60f
4 changed files with 30 additions and 17 deletions

View file

@ -24,6 +24,17 @@ QJsonObject NetworkResult::parseJson() const
return jsonDoc.object(); 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 NetworkResult::parseRapidJson() const
{ {
rapidjson::Document ret(rapidjson::kObjectType); rapidjson::Document ret(rapidjson::kObjectType);

View file

@ -10,7 +10,13 @@ class NetworkResult
public: public:
NetworkResult(const QByteArray &data); 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; 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; rapidjson::Document parseRapidJson() const;
const QByteArray &getData() const; const QByteArray &getData() const;

View file

@ -29,13 +29,17 @@ namespace {
return {urlTemplate.arg(id.string, emoteScale)}; return {urlTemplate.arg(id.string, emoteScale)};
} }
EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id)
{
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
static std::mutex mutex;
return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
}
std::pair<Outcome, EmoteMap> parseGlobalEmotes( std::pair<Outcome, EmoteMap> parseGlobalEmotes(
const QJsonObject &jsonRoot, const EmoteMap &currentEmotes) const QJsonArray &jsonEmotes, const EmoteMap &currentEmotes)
{ {
auto emotes = EmoteMap(); auto emotes = EmoteMap();
auto jsonEmotes = jsonRoot.value("emotes").toArray();
auto urlTemplate =
qS("https:") + jsonRoot.value("urlTemplate").toString();
for (auto jsonEmote : jsonEmotes) for (auto jsonEmote : jsonEmotes)
{ {
@ -45,10 +49,9 @@ namespace {
auto emote = Emote( auto emote = Emote(
{name, {name,
ImageSet{ ImageSet{Image::fromUrl(getEmoteLinkV3(id, "1x"), 1),
Image::fromUrl(getEmoteLink(urlTemplate, id, "1x"), 1), Image::fromUrl(getEmoteLinkV3(id, "2x"), 0.5),
Image::fromUrl(getEmoteLink(urlTemplate, id, "2x"), 0.5), Image::fromUrl(getEmoteLinkV3(id, "3x"), 0.25)},
Image::fromUrl(getEmoteLink(urlTemplate, id, "3x"), 0.25)},
Tooltip{name.string + "<br />Global BetterTTV Emote"}, Tooltip{name.string + "<br />Global BetterTTV Emote"},
Url{"https://manage.betterttv.net/emotes/" + id.string}}); Url{"https://manage.betterttv.net/emotes/" + id.string}});
@ -58,13 +61,6 @@ namespace {
return {Success, std::move(emotes)}; return {Success, std::move(emotes)};
} }
EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id)
{
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
static std::mutex mutex;
return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
}
std::pair<Outcome, EmoteMap> parseChannelEmotes(const QJsonObject &jsonRoot) std::pair<Outcome, EmoteMap> parseChannelEmotes(const QJsonObject &jsonRoot)
{ {
auto emotes = EmoteMap(); auto emotes = EmoteMap();
@ -129,7 +125,7 @@ void BttvEmotes::loadEmotes()
.timeout(30000) .timeout(30000)
.onSuccess([this](auto result) -> Outcome { .onSuccess([this](auto result) -> Outcome {
auto emotes = this->global_.get(); auto emotes = this->global_.get();
auto pair = parseGlobalEmotes(result.parseJson(), *emotes); auto pair = parseGlobalEmotes(result.parseJsonArray(), *emotes);
if (pair.first) if (pair.first)
this->global_.set( this->global_.set(
std::make_shared<EmoteMap>(std::move(pair.second))); std::make_shared<EmoteMap>(std::move(pair.second)));

View file

@ -14,7 +14,7 @@ class EmoteMap;
class BttvEmotes final class BttvEmotes final
{ {
static constexpr const char *globalEmoteApiUrl = static constexpr const char *globalEmoteApiUrl =
"https://api.betterttv.net/2/emotes"; "https://api.betterttv.net/3/cached/emotes/global";
static constexpr const char *bttvChannelEmoteApiUrl = static constexpr const char *bttvChannelEmoteApiUrl =
"https://api.betterttv.net/3/cached/users/twitch/"; "https://api.betterttv.net/3/cached/users/twitch/";