2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/bttv/BttvEmotes.hpp"
|
2018-06-05 17:39:49 +02:00
|
|
|
|
2018-06-26 17:20:03 +02:00
|
|
|
#include "common/UrlFetch.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "debug/Log.hpp"
|
|
|
|
#include "messages/Image.hpp"
|
2018-06-05 17:39:49 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
QString getEmoteLink(QString urlTemplate, const QString &id, const QString &emoteScale)
|
|
|
|
{
|
|
|
|
urlTemplate.detach();
|
|
|
|
|
|
|
|
return urlTemplate.replace("{{id}}", id).replace("{{image}}", emoteScale);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void BTTVEmotes::loadGlobalEmotes()
|
|
|
|
{
|
|
|
|
QString url("https://api.betterttv.net/2/emotes");
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
NetworkRequest req(url);
|
2018-06-05 17:39:49 +02:00
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.setTimeout(30000);
|
|
|
|
req.setUseQuickLoadCache(true);
|
|
|
|
req.getJSON([this](QJsonObject &root) {
|
|
|
|
auto emotes = root.value("emotes").toArray();
|
|
|
|
|
|
|
|
QString urlTemplate = "https:" + root.value("urlTemplate").toString();
|
|
|
|
|
2018-06-07 13:09:08 +02:00
|
|
|
std::vector<QString> codes;
|
2018-06-05 17:39:49 +02:00
|
|
|
for (const QJsonValue &emote : emotes) {
|
|
|
|
QString id = emote.toObject().value("id").toString();
|
|
|
|
QString code = emote.toObject().value("code").toString();
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
EmoteData emoteData;
|
2018-06-28 19:38:57 +02:00
|
|
|
emoteData.image1x = new Image(getEmoteLink(urlTemplate, id, "1x"), 1, code,
|
2018-06-26 17:20:03 +02:00
|
|
|
code + "<br />Global BTTV Emote");
|
2018-06-28 19:38:57 +02:00
|
|
|
emoteData.image2x = new Image(getEmoteLink(urlTemplate, id, "2x"), 0.5,
|
2018-06-26 17:20:03 +02:00
|
|
|
code, code + "<br />Global BTTV Emote");
|
2018-06-28 19:38:57 +02:00
|
|
|
emoteData.image3x = new Image(getEmoteLink(urlTemplate, id, "3x"), 0.25,
|
2018-06-26 17:20:03 +02:00
|
|
|
code, code + "<br />Global BTTV Emote");
|
2018-06-05 17:39:49 +02:00
|
|
|
emoteData.pageLink = "https://manage.betterttv.net/emotes/" + id;
|
|
|
|
|
|
|
|
this->globalEmotes.insert(code, emoteData);
|
2018-06-07 13:09:08 +02:00
|
|
|
codes.push_back(code);
|
2018-06-05 17:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->globalEmoteCodes = codes;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
void BTTVEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map)
|
2018-06-05 17:39:49 +02:00
|
|
|
{
|
|
|
|
printf("[BTTVEmotes] Reload BTTV Channel Emotes for channel %s\n", qPrintable(channelName));
|
|
|
|
|
|
|
|
QString url("https://api.betterttv.net/2/channels/" + channelName);
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Request bttv channel emotes for {}", channelName);
|
2018-06-05 17:39:49 +02:00
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
NetworkRequest req(url);
|
2018-06-05 17:39:49 +02:00
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.setTimeout(3000);
|
2018-06-20 09:02:16 +02:00
|
|
|
req.setUseQuickLoadCache(true);
|
2018-06-05 17:39:49 +02:00
|
|
|
req.getJSON([this, channelName, _map](QJsonObject &rootNode) {
|
|
|
|
auto map = _map.lock();
|
|
|
|
|
|
|
|
if (_map.expired()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
map->clear();
|
|
|
|
|
|
|
|
auto emotesNode = rootNode.value("emotes").toArray();
|
|
|
|
|
|
|
|
QString linkTemplate = "https:" + rootNode.value("urlTemplate").toString();
|
|
|
|
|
2018-06-07 13:09:08 +02:00
|
|
|
std::vector<QString> codes;
|
2018-06-05 17:39:49 +02:00
|
|
|
for (const QJsonValue &emoteNode : emotesNode) {
|
|
|
|
QJsonObject emoteObject = emoteNode.toObject();
|
|
|
|
|
|
|
|
QString id = emoteObject.value("id").toString();
|
|
|
|
QString code = emoteObject.value("code").toString();
|
|
|
|
// emoteObject.value("imageType").toString();
|
|
|
|
|
2018-06-07 12:22:28 +02:00
|
|
|
auto emote = this->channelEmoteCache.getOrAdd(id, [&] {
|
2018-06-26 17:06:17 +02:00
|
|
|
EmoteData emoteData;
|
2018-06-05 17:39:49 +02:00
|
|
|
QString link = linkTemplate;
|
|
|
|
link.detach();
|
|
|
|
emoteData.image1x =
|
2018-06-28 19:38:57 +02:00
|
|
|
new Image(link.replace("{{id}}", id).replace("{{image}}", "1x"), 1,
|
2018-06-26 17:20:03 +02:00
|
|
|
code, code + "<br />Channel BTTV Emote");
|
2018-06-05 17:39:49 +02:00
|
|
|
link = linkTemplate;
|
|
|
|
link.detach();
|
|
|
|
emoteData.image2x =
|
2018-06-28 19:38:57 +02:00
|
|
|
new Image(link.replace("{{id}}", id).replace("{{image}}", "2x"),
|
2018-06-26 17:20:03 +02:00
|
|
|
0.5, code, code + "<br />Channel BTTV Emote");
|
2018-06-05 17:39:49 +02:00
|
|
|
link = linkTemplate;
|
|
|
|
link.detach();
|
|
|
|
emoteData.image3x =
|
2018-06-28 19:38:57 +02:00
|
|
|
new Image(link.replace("{{id}}", id).replace("{{image}}", "3x"),
|
2018-06-26 17:20:03 +02:00
|
|
|
0.25, code, code + "<br />Channel BTTV Emote");
|
2018-06-05 17:39:49 +02:00
|
|
|
emoteData.pageLink = "https://manage.betterttv.net/emotes/" + id;
|
|
|
|
|
|
|
|
return emoteData;
|
|
|
|
});
|
|
|
|
|
|
|
|
this->channelEmotes.insert(code, emote);
|
|
|
|
map->insert(code, emote);
|
2018-06-07 13:09:08 +02:00
|
|
|
codes.push_back(code);
|
2018-06-05 17:39:49 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:09:08 +02:00
|
|
|
this->channelEmoteCodes[channelName] = codes;
|
2018-06-05 17:39:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|