mirror-chatterino2/emotes.cpp

185 lines
5.4 KiB
C++
Raw Normal View History

2017-01-04 15:12:31 +01:00
#include "emotes.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-01-18 21:30:23 +01:00
namespace chatterino {
2017-01-18 04:33:30 +01:00
QString Emotes::twitchEmoteTemplate(
2017-01-17 00:42:32 +01:00
"https://static-cdn.jtvnw.net/emoticons/v1/{id}/{scale}.0");
2017-01-04 15:12:31 +01:00
2017-01-18 04:33:30 +01:00
ConcurrentMap<QString, TwitchEmoteValue *> Emotes::twitchEmotes;
2017-01-18 21:30:23 +01:00
ConcurrentMap<QString, messages::LazyLoadedImage *> Emotes::bttvEmotes;
ConcurrentMap<QString, messages::LazyLoadedImage *> Emotes::ffzEmotes;
ConcurrentMap<QString, messages::LazyLoadedImage *> Emotes::chatterinoEmotes;
ConcurrentMap<QString, messages::LazyLoadedImage *>
Emotes::bttvChannelEmoteFromCaches;
2017-01-26 18:22:04 +01:00
ConcurrentMap<int, messages::LazyLoadedImage *>
2017-01-18 21:30:23 +01:00
Emotes::ffzChannelEmoteFromCaches;
ConcurrentMap<long, messages::LazyLoadedImage *> Emotes::twitchEmoteFromCache;
ConcurrentMap<QString, messages::LazyLoadedImage *> Emotes::miscImageFromCache;
2017-02-07 00:03:15 +01:00
boost::signals2::signal<void()> Emotes::gifUpdateTimerSignal;
2017-01-05 20:49:33 +01:00
2017-02-06 17:42:28 +01:00
QTimer Emotes::gifUpdateTimer;
bool Emotes::gifUpdateTimerInitiated(false);
2017-01-18 04:33:30 +01:00
int Emotes::generation = 0;
2017-01-15 16:38:30 +01:00
2017-01-05 16:07:20 +01:00
Emotes::Emotes()
{
}
2017-01-04 15:12:31 +01:00
2017-01-26 17:26:20 +01:00
void
Emotes::loadGlobalEmotes()
{
loadBttvEmotes();
loadFfzEmotes();
}
void
Emotes::loadBttvEmotes()
{
// 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();
QString _template = "https:" + root.value("urlTemplate").toString();
for (const QJsonValue &emote : emotes) {
QString id = emote.toObject().value("id").toString();
QString code = emote.toObject().value("code").toString();
// emote.value("imageType").toString();
QString tmp = _template;
tmp.detach();
QString url =
tmp.replace("{{id}}", id).replace("{{image}}", "1x");
Emotes::getBttvEmotes().insert(
code, new messages::LazyLoadedImage(
url, 1, code, code + "\nGlobal Bttv Emote"));
}
}
reply->deleteLater();
manager->deleteLater();
});
}
void
Emotes::loadFfzEmotes()
{
2017-02-08 23:58:28 +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
int id = object.value("id").toInt();
QString code = object.value("name").toString();
QJsonObject urls = object.value("urls").toObject();
QString url1 = "http:" + urls.value("1").toString();
Emotes::getBttvEmotes().insert(
code, new messages::LazyLoadedImage(
url1, 1, code, code + "\nGlobal Ffz Emote"));
}
}
}
reply->deleteLater();
manager->deleteLater();
});
}
2017-01-18 21:30:23 +01:00
messages::LazyLoadedImage *
2017-01-11 18:52:09 +01:00
Emotes::getTwitchEmoteById(const QString &name, long id)
2017-01-06 23:28:48 +01:00
{
2017-01-26 10:18:02 +01:00
qDebug() << "loading twitch emote: " << id;
return Emotes::twitchEmoteFromCache.getOrAdd(id, [&name, &id] {
qDebug() << "loading twitch emote: " << id;
2017-01-13 18:59:11 +01:00
qreal scale;
QString url = getTwitchEmoteLink(id, scale);
2017-01-18 21:30:23 +01:00
return new messages::LazyLoadedImage(url, scale, name,
name + "\nTwitch Emote");
2017-01-13 18:59:11 +01:00
});
}
QString
Emotes::getTwitchEmoteLink(long id, qreal &scale)
{
scale = .5;
2017-01-26 10:18:02 +01:00
QString value = Emotes::twitchEmoteTemplate;
value.detach();
return value.replace("{id}", QString::number(id)).replace("{scale}", "2");
2017-01-06 23:28:48 +01:00
}
2017-01-18 21:30:23 +01:00
messages::LazyLoadedImage *
2017-01-11 18:52:09 +01:00
Emotes::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-01-18 21:30:23 +01:00
messages::LazyLoadedImage *
2017-01-11 18:52:09 +01:00
Emotes::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-01-18 21:30:23 +01:00
}