2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/twitch/TwitchEmotes.hpp"
|
2018-06-05 17:13:29 +02:00
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
#include "common/UrlFetch.hpp"
|
|
|
|
#include "debug/Benchmark.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "debug/Log.hpp"
|
|
|
|
#include "messages/Image.hpp"
|
|
|
|
#include "util/RapidjsonHelpers.hpp"
|
2018-06-05 17:13:29 +02:00
|
|
|
|
|
|
|
#define TWITCH_EMOTE_TEMPLATE "https://static-cdn.jtvnw.net/emoticons/v1/{id}/{scale}"
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-06-07 13:01:06 +02:00
|
|
|
QString getEmoteLink(const QString &id, const QString &emoteScale)
|
2018-06-05 17:13:29 +02:00
|
|
|
{
|
|
|
|
QString value = TWITCH_EMOTE_TEMPLATE;
|
|
|
|
|
|
|
|
value.detach();
|
|
|
|
|
2018-06-07 13:01:06 +02:00
|
|
|
return value.replace("{id}", id).replace("{scale}", emoteScale);
|
2018-06-05 17:13:29 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 22:04:12 +02:00
|
|
|
QString cleanUpCode(const QString &dirtyEmoteCode)
|
|
|
|
{
|
2018-06-24 18:35:38 +02:00
|
|
|
QString cleanCode = dirtyEmoteCode;
|
2018-06-19 22:04:12 +02:00
|
|
|
// clang-format off
|
|
|
|
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)"},
|
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
auto it = emoteNameReplacements.find(dirtyEmoteCode);
|
|
|
|
if (it != emoteNameReplacements.end()) {
|
2018-06-24 18:35:38 +02:00
|
|
|
cleanCode = it.value();
|
2018-06-19 22:04:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-24 18:35:38 +02:00
|
|
|
cleanCode.replace("<", "<");
|
|
|
|
cleanCode.replace(">", ">");
|
|
|
|
|
|
|
|
return cleanCode;
|
2018-06-19 22:04:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-24 14:42:40 +02:00
|
|
|
void loadSetData(std::shared_ptr<TwitchEmotes::EmoteSet> emoteSet)
|
|
|
|
{
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Load twitch emote set data for {}", emoteSet->key);
|
|
|
|
NetworkRequest req("https://braize.pajlada.com/chatterino/twitchemotes/set/" + emoteSet->key +
|
|
|
|
"/");
|
2018-06-24 14:42:40 +02:00
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
req.setRequestType(NetworkRequest::GetRequest);
|
2018-06-24 14:42:40 +02:00
|
|
|
|
|
|
|
req.onError([](int errorCode) -> bool {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Emote sets on ERROR {}", errorCode);
|
2018-06-24 14:42:40 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.onSuccess([emoteSet](const rapidjson::Document &root) -> bool {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Emote sets on success");
|
2018-06-24 14:42:40 +02:00
|
|
|
if (!root.IsObject()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string emoteSetID;
|
|
|
|
QString channelName;
|
|
|
|
if (!rj::getSafe(root, "channel_name", channelName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
emoteSet->channelName = channelName;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.execute();
|
|
|
|
}
|
|
|
|
|
2018-06-05 17:13:29 +02:00
|
|
|
} // namespace
|
|
|
|
|
2018-06-24 14:42:40 +02:00
|
|
|
TwitchEmotes::TwitchEmotes()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
EmoteSet emoteSet;
|
|
|
|
emoteSet.key = "19194";
|
|
|
|
emoteSet.text = "Twitch Prime Emotes";
|
|
|
|
this->staticEmoteSets[emoteSet.key] = std::move(emoteSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
EmoteSet emoteSet;
|
|
|
|
emoteSet.key = "0";
|
|
|
|
emoteSet.text = "Twitch Global Emotes";
|
|
|
|
this->staticEmoteSets[emoteSet.key] = std::move(emoteSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-26 17:06:17 +02:00
|
|
|
EmoteData TwitchEmotes::getEmoteById(const QString &id, const QString &emoteName)
|
2018-06-05 17:13:29 +02:00
|
|
|
{
|
|
|
|
QString _emoteName = emoteName;
|
|
|
|
_emoteName.replace("<", "<");
|
|
|
|
_emoteName.replace(">", ">");
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
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)"},
|
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
auto it = emoteNameReplacements.find(_emoteName);
|
|
|
|
if (it != emoteNameReplacements.end()) {
|
|
|
|
_emoteName = it.value();
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
return twitchEmoteFromCache_.getOrAdd(id, [&emoteName, &_emoteName, &id] {
|
2018-06-26 17:06:17 +02:00
|
|
|
EmoteData newEmoteData;
|
2018-06-24 18:29:30 +02:00
|
|
|
auto cleanCode = cleanUpCode(emoteName);
|
2018-06-30 10:10:53 +02:00
|
|
|
newEmoteData.image1x =
|
|
|
|
new Image(getEmoteLink(id, "1.0"), 1, emoteName, _emoteName + "<br/>Twitch Emote");
|
2018-06-24 18:29:30 +02:00
|
|
|
newEmoteData.image1x->setCopyString(cleanCode);
|
|
|
|
|
2018-06-30 10:10:53 +02:00
|
|
|
newEmoteData.image2x =
|
|
|
|
new Image(getEmoteLink(id, "2.0"), .5, emoteName, _emoteName + "<br/>Twitch Emote");
|
2018-06-24 18:29:30 +02:00
|
|
|
newEmoteData.image2x->setCopyString(cleanCode);
|
|
|
|
|
2018-06-30 10:10:53 +02:00
|
|
|
newEmoteData.image3x =
|
|
|
|
new Image(getEmoteLink(id, "3.0"), .25, emoteName, _emoteName + "<br/>Twitch Emote");
|
2018-06-05 17:13:29 +02:00
|
|
|
|
2018-06-24 18:29:30 +02:00
|
|
|
newEmoteData.image3x->setCopyString(cleanCode);
|
|
|
|
|
2018-06-05 17:13:29 +02:00
|
|
|
return newEmoteData;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchEmotes::refresh(const std::shared_ptr<TwitchAccount> &user)
|
|
|
|
{
|
|
|
|
const auto &roomID = user->getUserId();
|
2018-06-07 12:36:06 +02:00
|
|
|
TwitchAccountEmoteData &emoteData = this->emotes[roomID];
|
2018-06-05 17:13:29 +02:00
|
|
|
|
|
|
|
if (emoteData.filled) {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Emotes are already loaded for room id {}", roomID);
|
2018-06-05 17:13:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-27 02:16:30 +02:00
|
|
|
auto loadEmotes = [=, &emoteData](const rapidjson::Document &root) {
|
2018-06-07 13:01:06 +02:00
|
|
|
emoteData.emoteSets.clear();
|
|
|
|
emoteData.emoteCodes.clear();
|
|
|
|
|
2018-06-27 02:16:30 +02:00
|
|
|
auto emoticonSets = root.FindMember("emoticon_sets");
|
|
|
|
if (emoticonSets == root.MemberEnd() || !emoticonSets->value.IsObject()) {
|
|
|
|
Log("No emoticon_sets in load emotes response");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &emoteSetJSON : emoticonSets->value.GetObject()) {
|
2018-06-24 14:42:40 +02:00
|
|
|
auto emoteSet = std::make_shared<EmoteSet>();
|
|
|
|
|
2018-06-27 02:16:30 +02:00
|
|
|
emoteSet->key = emoteSetJSON.name.GetString();
|
2018-06-07 13:01:06 +02:00
|
|
|
|
2018-06-24 14:42:40 +02:00
|
|
|
loadSetData(emoteSet);
|
2018-06-07 13:01:06 +02:00
|
|
|
|
2018-06-27 02:16:30 +02:00
|
|
|
for (const rapidjson::Value &emoteJSON : emoteSetJSON.value.GetArray()) {
|
|
|
|
if (!emoteJSON.IsObject()) {
|
|
|
|
Log("Emote value was invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString id, code;
|
|
|
|
|
|
|
|
uint64_t idNumber;
|
|
|
|
|
|
|
|
if (!rj::getSafe(emoteJSON, "id", idNumber)) {
|
|
|
|
Log("No ID key found in Emote value");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rj::getSafe(emoteJSON, "code", code)) {
|
|
|
|
Log("No code key found in Emote value");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = QString::number(idNumber);
|
2018-06-24 14:42:40 +02:00
|
|
|
|
2018-06-19 22:04:12 +02:00
|
|
|
auto cleanCode = cleanUpCode(code);
|
2018-06-24 14:42:40 +02:00
|
|
|
emoteSet->emotes.emplace_back(id, cleanCode);
|
2018-06-19 22:04:12 +02:00
|
|
|
emoteData.emoteCodes.push_back(cleanCode);
|
2018-06-07 13:01:06 +02:00
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
EmoteData emote = this->getEmoteById(id, code);
|
2018-06-07 13:01:06 +02:00
|
|
|
emoteData.emotes.insert(code, emote);
|
2018-06-05 17:13:29 +02:00
|
|
|
}
|
|
|
|
|
2018-06-24 14:42:40 +02:00
|
|
|
emoteData.emoteSets.emplace_back(emoteSet);
|
2018-06-07 13:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emoteData.filled = true;
|
|
|
|
};
|
|
|
|
|
2018-06-27 02:16:30 +02:00
|
|
|
user->loadEmotes(loadEmotes);
|
2018-06-05 17:13:29 +02:00
|
|
|
}
|
|
|
|
|
2018-06-24 14:42:40 +02:00
|
|
|
void TwitchEmotes::loadSetData(std::shared_ptr<TwitchEmotes::EmoteSet> emoteSet)
|
|
|
|
{
|
|
|
|
if (!emoteSet) {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("null emote set sent");
|
2018-06-24 14:42:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto staticSetIt = this->staticEmoteSets.find(emoteSet->key);
|
|
|
|
if (staticSetIt != this->staticEmoteSets.end()) {
|
|
|
|
const auto &staticSet = staticSetIt->second;
|
|
|
|
emoteSet->channelName = staticSet.channelName;
|
|
|
|
emoteSet->text = staticSet.text;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Load twitch emote set data for {}..", emoteSet->key);
|
|
|
|
NetworkRequest req("https://braize.pajlada.com/chatterino/twitchemotes/set/" + emoteSet->key +
|
|
|
|
"/");
|
2018-06-24 14:42:40 +02:00
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
req.setRequestType(NetworkRequest::GetRequest);
|
2018-06-24 14:42:40 +02:00
|
|
|
|
|
|
|
req.onError([](int errorCode) -> bool {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Emote sets on ERROR {}", errorCode);
|
2018-06-24 14:42:40 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.onSuccess([emoteSet](const rapidjson::Document &root) -> bool {
|
|
|
|
if (!root.IsObject()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string emoteSetID;
|
|
|
|
QString channelName;
|
|
|
|
QString type;
|
|
|
|
if (!rj::getSafe(root, "channel_name", channelName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rj::getSafe(root, "type", type)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Loaded twitch emote set data for {}!", emoteSet->key);
|
2018-06-24 14:42:40 +02:00
|
|
|
|
|
|
|
if (type == "sub") {
|
|
|
|
emoteSet->text = QString("Twitch Subscriber Emote (%1)").arg(channelName);
|
|
|
|
} else {
|
|
|
|
emoteSet->text = QString("Twitch Account Emote (%1)").arg(channelName);
|
|
|
|
}
|
|
|
|
|
|
|
|
emoteSet->channelName = channelName;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.execute();
|
|
|
|
}
|
|
|
|
|
2018-06-05 17:13:29 +02:00
|
|
|
} // namespace chatterino
|