mirror-chatterino2/src/emotemanager.cpp

211 lines
5.8 KiB
C++
Raw Normal View History

2017-04-12 17:46:44 +02:00
#include "emotemanager.h"
2017-01-13 18:59:11 +01:00
#include "resources.h"
2017-01-04 15:12:31 +01:00
2017-01-26 10:18:02 +01:00
#include <QDebug>
2017-01-26 17:26:20 +01:00
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <memory>
2017-01-26 10:18:02 +01:00
2017-04-12 17:46:44 +02:00
#define TWITCH_EMOTE_TEMPLATE "https://static-cdn.jtvnw.net/emoticons/v1/{id}/{scale}.0"
2017-04-14 17:52:22 +02:00
using namespace chatterino::messages;
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-18 21:30:23 +01:00
2017-04-12 17:46:44 +02:00
EmoteManager EmoteManager::instance;
EmoteManager::EmoteManager()
: _twitchEmotes()
, _bttvEmotes()
, _ffzEmotes()
, _chatterinoEmotes()
, _bttvChannelEmoteFromCaches()
, _ffzChannelEmoteFromCaches()
, _twitchEmoteFromCache()
, _miscImageFromCache()
, _gifUpdateTimerSignal()
, _gifUpdateTimer()
, _gifUpdateTimerInitiated(false)
, _generation(0)
{
}
2017-01-04 15:12:31 +01:00
2017-04-12 17:46:44 +02:00
ConcurrentMap<QString, twitch::EmoteValue *> &EmoteManager::getTwitchEmotes()
{
return _twitchEmotes;
}
2017-01-05 20:49:33 +01:00
2017-04-12 17:46:44 +02:00
ConcurrentMap<QString, messages::LazyLoadedImage *> &EmoteManager::getBttvEmotes()
{
return _bttvEmotes;
}
2017-02-06 17:42:28 +01:00
2017-04-12 17:46:44 +02:00
ConcurrentMap<QString, messages::LazyLoadedImage *> &EmoteManager::getFfzEmotes()
{
return _ffzEmotes;
}
2017-01-15 16:38:30 +01:00
2017-04-12 17:46:44 +02:00
ConcurrentMap<QString, messages::LazyLoadedImage *> &EmoteManager::getChatterinoEmotes()
2017-01-05 16:07:20 +01:00
{
2017-04-12 17:46:44 +02:00
return _chatterinoEmotes;
2017-01-05 16:07:20 +01:00
}
2017-01-04 15:12:31 +01:00
2017-04-12 17:46:44 +02:00
ConcurrentMap<QString, messages::LazyLoadedImage *> &EmoteManager::getBttvChannelEmoteFromCaches()
{
return _bttvChannelEmoteFromCaches;
}
ConcurrentMap<int, messages::LazyLoadedImage *> &EmoteManager::getFfzChannelEmoteFromCaches()
{
return _ffzChannelEmoteFromCaches;
}
ConcurrentMap<long, messages::LazyLoadedImage *> &EmoteManager::getTwitchEmoteFromCache()
{
return _twitchEmoteFromCache;
}
ConcurrentMap<QString, messages::LazyLoadedImage *> &EmoteManager::getMiscImageFromCache()
{
return _miscImageFromCache;
}
void EmoteManager::loadGlobalEmotes()
2017-01-26 17:26:20 +01:00
{
loadBttvEmotes();
loadFfzEmotes();
}
2017-04-12 17:46:44 +02:00
void EmoteManager::loadBttvEmotes()
2017-01-26 17:26:20 +01:00
{
// bttv
QNetworkAccessManager *manager = new QNetworkAccessManager();
QUrl url("https://api.betterttv.net/2/emotes");
QNetworkRequest request(url);
QNetworkReply *reply = manager->get(request);
QObject::connect(reply, &QNetworkReply::finished, [=] {
if (reply->error() == QNetworkReply::NetworkError::NoError) {
QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject root = jsonDoc.object();
auto emotes = root.value("emotes").toArray();
2017-04-12 17:46:44 +02:00
QString linkTemplate = "https:" + root.value("urlTemplate").toString();
2017-01-26 17:26:20 +01:00
for (const QJsonValue &emote : emotes) {
QString id = emote.toObject().value("id").toString();
QString code = emote.toObject().value("code").toString();
// emote.value("imageType").toString();
2017-04-12 17:46:44 +02:00
QString tmp = linkTemplate;
2017-01-26 17:26:20 +01:00
tmp.detach();
2017-04-12 17:46:44 +02:00
QString url = tmp.replace("{{id}}", id).replace("{{image}}", "1x");
2017-01-26 17:26:20 +01:00
2017-04-12 17:46:44 +02:00
EmoteManager::getBttvEmotes().insert(
code, new LazyLoadedImage(url, 1, code, code + "\nGlobal Bttv Emote"));
2017-01-26 17:26:20 +01:00
}
}
reply->deleteLater();
manager->deleteLater();
});
}
2017-04-12 17:46:44 +02:00
void EmoteManager::loadFfzEmotes()
2017-01-26 17:26:20 +01:00
{
2017-02-09 00:03:46 +01:00
// ffz
2017-01-26 17:26:20 +01:00
QNetworkAccessManager *manager = new QNetworkAccessManager();
QUrl url("https://api.frankerfacez.com/v1/set/global");
QNetworkRequest request(url);
QNetworkReply *reply = manager->get(request);
QObject::connect(reply, &QNetworkReply::finished, [=] {
if (reply->error() == QNetworkReply::NetworkError::NoError) {
QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject root = jsonDoc.object();
auto sets = root.value("sets").toObject();
for (const QJsonValue &set : sets) {
auto emoticons = set.toObject().value("emoticons").toArray();
for (const QJsonValue &emote : emoticons) {
QJsonObject object = emote.toObject();
// margins
2017-04-12 17:46:44 +02:00
// int id = object.value("id").toInt();
2017-01-26 17:26:20 +01:00
QString code = object.value("name").toString();
QJsonObject urls = object.value("urls").toObject();
QString url1 = "http:" + urls.value("1").toString();
2017-04-12 17:46:44 +02:00
EmoteManager::getBttvEmotes().insert(
code, new LazyLoadedImage(url1, 1, code, code + "\nGlobal Ffz Emote"));
2017-01-26 17:26:20 +01:00
}
}
}
reply->deleteLater();
manager->deleteLater();
});
}
2017-04-12 17:46:44 +02:00
LazyLoadedImage *EmoteManager::getTwitchEmoteById(const QString &name, long id)
2017-01-06 23:28:48 +01:00
{
2017-04-12 17:46:44 +02:00
return EmoteManager::_twitchEmoteFromCache.getOrAdd(id, [&name, &id] {
qDebug() << "added twitch emote: " << id;
2017-01-13 18:59:11 +01:00
qreal scale;
QString url = getTwitchEmoteLink(id, scale);
2017-04-12 17:46:44 +02:00
return new LazyLoadedImage(url, scale, name, name + "\nTwitch Emote");
2017-01-13 18:59:11 +01:00
});
}
2017-04-12 17:46:44 +02:00
QString EmoteManager::getTwitchEmoteLink(long id, qreal &scale)
2017-01-13 18:59:11 +01:00
{
scale = .5;
2017-04-12 17:46:44 +02:00
QString value = TWITCH_EMOTE_TEMPLATE;
2017-01-26 10:18:02 +01:00
value.detach();
return value.replace("{id}", QString::number(id)).replace("{scale}", "2");
2017-01-06 23:28:48 +01:00
}
2017-04-12 17:46:44 +02:00
LazyLoadedImage *EmoteManager::getCheerImage(long long amount, bool animated)
2017-01-05 16:07:20 +01:00
{
// TODO: fix this xD
2017-01-05 20:49:33 +01:00
return getCheerBadge(amount);
}
2017-01-04 15:12:31 +01:00
2017-04-12 17:46:44 +02:00
LazyLoadedImage *EmoteManager::getCheerBadge(long long amount)
2017-01-05 20:49:33 +01:00
{
2017-01-11 18:52:09 +01:00
if (amount >= 100000) {
2017-01-18 04:33:30 +01:00
return Resources::getCheerBadge100000();
2017-01-11 18:52:09 +01:00
} else if (amount >= 10000) {
2017-01-18 04:33:30 +01:00
return Resources::getCheerBadge10000();
2017-01-11 18:52:09 +01:00
} else if (amount >= 5000) {
2017-01-18 04:33:30 +01:00
return Resources::getCheerBadge5000();
2017-01-11 18:52:09 +01:00
} else if (amount >= 1000) {
2017-01-18 04:33:30 +01:00
return Resources::getCheerBadge1000();
2017-01-11 18:52:09 +01:00
} else if (amount >= 100) {
2017-01-18 04:33:30 +01:00
return Resources::getCheerBadge100();
2017-01-11 18:52:09 +01:00
} else {
2017-01-18 04:33:30 +01:00
return Resources::getCheerBadge1();
2017-01-05 20:49:33 +01:00
}
2017-01-04 15:12:31 +01:00
}
2017-05-27 16:16:39 +02:00
} // namespace chatterino