2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/twitch/TwitchEmotes.hpp"
|
2018-06-05 17:13:29 +02:00
|
|
|
|
2018-07-15 14:11:46 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-06-26 17:06:17 +02:00
|
|
|
#include "debug/Benchmark.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "messages/Emote.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "messages/Image.hpp"
|
|
|
|
#include "util/RapidjsonHelpers.hpp"
|
2018-06-05 17:13:29 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
2018-06-24 14:42:40 +02:00
|
|
|
TwitchEmotes::TwitchEmotes()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-05 17:04:52 +01:00
|
|
|
QString TwitchEmotes::cleanUpEmoteCode(const EmoteName &dirtyEmoteCode)
|
|
|
|
{
|
|
|
|
auto cleanCode = dirtyEmoteCode.string;
|
|
|
|
cleanCode.detach();
|
|
|
|
|
|
|
|
static QMap<QString, QString> emoteNameReplacements{
|
|
|
|
{"[oO](_|\\.)[oO]", "O_o"}, {"\\>\\;\\(", ">("},
|
|
|
|
{"\\<\\;3", "<3"}, {"\\:-?(o|O)", ":O"},
|
|
|
|
{"\\:-?(p|P)", ":P"}, {"\\:-?[\\\\/]", ":/"},
|
|
|
|
{"\\:-?[z|Z|\\|]", ":Z"}, {"\\:-?\\(", ":("},
|
|
|
|
{"\\:-?\\)", ":)"}, {"\\:-?D", ":D"},
|
|
|
|
{"\\;-?(p|P)", ";P"}, {"\\;-?\\)", ";)"},
|
|
|
|
{"R-?\\)", "R)"}, {"B-?\\)", "B)"},
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it = emoteNameReplacements.find(dirtyEmoteCode.string);
|
|
|
|
if (it != emoteNameReplacements.end())
|
|
|
|
{
|
|
|
|
cleanCode = it.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanCode.replace("<", "<");
|
|
|
|
cleanCode.replace(">", ">");
|
|
|
|
|
|
|
|
return cleanCode;
|
|
|
|
}
|
|
|
|
|
2018-06-05 17:13:29 +02:00
|
|
|
// 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_)
|
2018-06-05 17:13:29 +02:00
|
|
|
{
|
2019-08-13 13:02:11 +02:00
|
|
|
static const QMap<QString, QString> replacements{
|
2018-08-06 21:17:03 +02:00
|
|
|
{"[oO](_|\\.)[oO]", "O_o"}, {"\\>\\;\\(", ">("},
|
|
|
|
{"\\<\\;3", "<3"}, {"\\:-?(o|O)", ":O"},
|
|
|
|
{"\\:-?(p|P)", ":P"}, {"\\:-?[\\\\/]", ":/"},
|
|
|
|
{"\\:-?[z|Z|\\|]", ":Z"}, {"\\:-?\\(", ":("},
|
|
|
|
{"\\:-?\\)", ":)"}, {"\\:-?D", ":D"},
|
|
|
|
{"\\;-?(p|P)", ";P"}, {"\\;-?\\)", ";)"},
|
2018-06-05 17:13:29 +02:00
|
|
|
{"R-?\\)", "R)"}, {"B-?\\)", "B)"},
|
|
|
|
};
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
auto name = name_.string;
|
|
|
|
name.detach();
|
2018-06-24 18:29:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
// replace < >
|
|
|
|
name.replace("<", "<");
|
|
|
|
name.replace(">", ">");
|
2018-06-24 18:29:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
// replace regexes
|
|
|
|
auto it = replacements.find(name);
|
2018-10-21 13:43:02 +02:00
|
|
|
if (it != replacements.end())
|
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
name = it.value();
|
|
|
|
}
|
2018-06-05 17:13:29 +02:00
|
|
|
|
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)
|
|
|
|
{
|
2020-11-22 15:54:58 +01:00
|
|
|
(*cache)[id] = shared = std::make_shared<Emote>(Emote{
|
2021-03-20 23:49:42 +01:00
|
|
|
EmoteName{TwitchEmotes::cleanUpEmoteCode(EmoteName{name})},
|
2020-11-22 15:54:58 +01:00
|
|
|
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 + "<br>Twitch Emote"},
|
|
|
|
Url{QString("https://twitchemotes.com/emotes/%1").arg(id.string)}});
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
2018-06-24 18:29:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return shared;
|
2018-06-05 17:13:29 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Url TwitchEmotes::getEmoteLink(const EmoteId &id, const QString &emoteScale)
|
2018-06-05 17:13:29 +02:00
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
return {QString(TWITCH_EMOTE_TEMPLATE)
|
|
|
|
.replace("{id}", id.string)
|
|
|
|
.replace("{scale}", emoteScale)};
|
2018-06-05 17:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|