mirror-chatterino2/src/providers/twitch/TwitchEmotes.cpp

74 lines
2.2 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchEmotes.hpp"
2018-07-15 14:11:46 +02:00
#include "common/NetworkRequest.hpp"
2018-06-26 17:06:17 +02:00
#include "debug/Benchmark.hpp"
#include "messages/Emote.hpp"
2018-06-26 14:09:39 +02:00
#include "messages/Image.hpp"
#include "util/RapidjsonHelpers.hpp"
namespace chatterino {
TwitchEmotes::TwitchEmotes()
{
}
QString TwitchEmotes::cleanUpEmoteCode(const QString &dirtyEmoteCode)
{
auto cleanCode = dirtyEmoteCode;
cleanCode.detach();
static QMap<QString, QString> emoteNameReplacements{
{"[oO](_|\\.)[oO]", "O_o"}, {"\\&gt\\;\\(", "&gt;("},
{"\\&lt\\;3", "&lt;3"}, {"\\:-?(o|O)", ":O"},
{"\\:-?(p|P)", ":P"}, {"\\:-?[\\\\/]", ":/"},
{"\\:-?[z|Z|\\|]", ":Z"}, {"\\:-?\\(", ":("},
{"\\:-?\\)", ":)"}, {"\\:-?D", ":D"},
{"\\;-?(p|P)", ";P"}, {"\\;-?\\)", ";)"},
{"R-?\\)", "R)"}, {"B-?\\)", "B)"},
};
auto it = emoteNameReplacements.find(dirtyEmoteCode);
if (it != emoteNameReplacements.end())
{
cleanCode = it.value();
}
return cleanCode;
}
// id is used for lookup
// emoteName is used for giving a name to the emote in case it doesn't exist
2018-08-06 21:17:03 +02:00
EmotePtr TwitchEmotes::getOrCreateEmote(const EmoteId &id,
const EmoteName &name_)
{
auto name = TwitchEmotes::cleanUpEmoteCode(name_.string);
2018-08-02 14:23:27 +02:00
// search in cache or create new emote
auto cache = this->twitchEmotesCache_.access();
auto shared = (*cache)[id].lock();
2018-10-21 13:43:02 +02:00
if (!shared)
{
(*cache)[id] = shared = std::make_shared<Emote>(Emote{
EmoteName{name},
ImageSet{
Image::fromUrl(getEmoteLink(id, "1.0"), 1),
Image::fromUrl(getEmoteLink(id, "2.0"), 0.5),
Image::fromUrl(getEmoteLink(id, "3.0"), 0.25),
},
Tooltip{name.toHtmlEscaped() + "<br>Twitch Emote"},
Url{QString("https://twitchemotes.com/emotes/%1").arg(id.string)}});
2018-08-02 14:23:27 +02:00
}
2018-08-02 14:23:27 +02:00
return shared;
}
2018-08-02 14:23:27 +02:00
Url TwitchEmotes::getEmoteLink(const EmoteId &id, const QString &emoteScale)
{
2018-08-06 21:17:03 +02:00
return {QString(TWITCH_EMOTE_TEMPLATE)
.replace("{id}", id.string)
.replace("{scale}", emoteScale)};
}
} // namespace chatterino