2017-09-16 00:05:06 +02:00
|
|
|
#include "twitchchannel.hpp"
|
2017-11-04 14:57:29 +01:00
|
|
|
#include "debug/log.hpp"
|
2017-09-16 00:05:06 +02:00
|
|
|
#include "emotemanager.hpp"
|
2017-11-04 14:57:29 +01:00
|
|
|
#include "util/urlfetch.hpp"
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
#include <QThread>
|
|
|
|
#include <QTimer>
|
2017-09-16 00:05:06 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace twitch {
|
|
|
|
|
|
|
|
TwitchChannel::TwitchChannel(EmoteManager &emoteManager, IrcManager &ircManager,
|
2017-11-04 14:57:29 +01:00
|
|
|
const QString &channelName, bool _isSpecial)
|
|
|
|
: Channel(channelName)
|
|
|
|
, emoteManager(emoteManager)
|
2017-09-16 00:05:06 +02:00
|
|
|
, ircManager(ircManager)
|
|
|
|
, bttvChannelEmotes(new EmoteMap)
|
|
|
|
, ffzChannelEmotes(new EmoteMap)
|
2017-11-04 14:57:29 +01:00
|
|
|
, subscriptionURL("https://www.twitch.tv/" + name + "/subscribe?ref=in_chat_subscriber_link")
|
|
|
|
, channelURL("https://twitch.tv/" + name)
|
|
|
|
, popoutPlayerURL("https://player.twitch.tv/?channel=" + name)
|
2017-09-16 00:05:06 +02:00
|
|
|
, isLive(false)
|
2017-11-04 14:57:29 +01:00
|
|
|
, isSpecial(_isSpecial)
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
debug::Log("[TwitchChannel:{}] Opened", this->name);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
if (!this->isSpecial) {
|
2017-09-16 00:05:06 +02:00
|
|
|
this->reloadChannelEmotes();
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
this->liveStatusTimer = new QTimer;
|
|
|
|
QObject::connect(this->liveStatusTimer, &QTimer::timeout, [this]() {
|
|
|
|
this->refreshLiveStatus(); //
|
|
|
|
});
|
|
|
|
this->liveStatusTimer->start(60000);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
this->roomIDchanged.connect([this]() {
|
|
|
|
this->refreshLiveStatus(); //
|
|
|
|
});
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
TwitchChannel::~TwitchChannel()
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
this->liveStatusTimer->stop();
|
|
|
|
this->liveStatusTimer->deleteLater();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
bool TwitchChannel::isEmpty() const
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
return this->name.isEmpty();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
bool TwitchChannel::canSendMessage() const
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
return !this->isEmpty() && !this->isSpecial;
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
void TwitchChannel::setRoomID(const QString &_roomID)
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
this->roomID = _roomID;
|
2017-09-16 00:05:06 +02:00
|
|
|
this->roomIDchanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::reloadChannelEmotes()
|
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
debug::Log("[TwitchChannel:{}] Reloading channel emotes", this->name);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
|
|
|
this->emoteManager.reloadBTTVChannelEmotes(this->name, this->bttvChannelEmotes);
|
|
|
|
this->emoteManager.reloadFFZChannelEmotes(this->name, this->ffzChannelEmotes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::sendMessage(const QString &message)
|
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
debug::Log("[TwitchChannel:{}] Send message: {}", this->name, message);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
|
|
|
// Do last message processing
|
|
|
|
QString parsedMessage = this->emoteManager.replaceShortCodes(message);
|
|
|
|
|
|
|
|
this->ircManager.sendMessage(this->name, parsedMessage);
|
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
|
|
|
|
void TwitchChannel::setLive(bool newLiveStatus)
|
|
|
|
{
|
|
|
|
if (this->isLive == newLiveStatus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->isLive = newLiveStatus;
|
|
|
|
this->onlineStatusChanged();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
|
|
|
|
void TwitchChannel::refreshLiveStatus()
|
|
|
|
{
|
|
|
|
if (this->roomID.isEmpty()) {
|
|
|
|
this->setLive(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug::Log("[TwitchChannel:{}] Refreshing live status", this->name);
|
|
|
|
|
|
|
|
QString url("https://api.twitch.tv/kraken/streams/" + this->roomID);
|
|
|
|
|
|
|
|
util::twitch::get(url, QThread::currentThread(), [this](QJsonObject obj) {
|
|
|
|
if (obj.value("stream").isNull()) {
|
|
|
|
this->setLive(false);
|
|
|
|
} else {
|
|
|
|
auto stream = obj.value("stream").toObject();
|
|
|
|
this->streamViewerCount = QString::number(stream.value("viewers").toDouble());
|
|
|
|
this->streamGame = stream.value("game").toString();
|
|
|
|
this->streamStatus = stream.value("channel").toObject().value("status").toString();
|
|
|
|
QDateTime since =
|
|
|
|
QDateTime::fromString(stream.value("created_at").toString(), Qt::ISODate);
|
|
|
|
auto diff = since.secsTo(QDateTime::currentDateTime());
|
|
|
|
this->streamUptime =
|
|
|
|
QString::number(diff / 3600) + "h " + QString::number(diff % 3600 / 60) + "m";
|
|
|
|
|
|
|
|
this->setLive(true);
|
|
|
|
}
|
|
|
|
});
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
|
|
|
|
} // namespace twitch
|
|
|
|
} // namespace chatterino
|