mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
use twitch get with rapidjson json in refreshLiveStatus
we now check that all values are legit, which can be an issue if we are sent invalid json data
This commit is contained in:
parent
4a405379d0
commit
307dc16d79
2 changed files with 79 additions and 16 deletions
|
@ -39,7 +39,7 @@ TwitchChannel::TwitchChannel(IrcManager &ircManager, const QString &channelName,
|
||||||
});
|
});
|
||||||
|
|
||||||
this->fetchMessages.connect([this] {
|
this->fetchMessages.connect([this] {
|
||||||
this->fetchRecentMessages();
|
this->fetchRecentMessages(); //
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,22 +109,49 @@ void TwitchChannel::refreshLiveStatus()
|
||||||
|
|
||||||
QString url("https://api.twitch.tv/kraken/streams/" + this->roomID);
|
QString url("https://api.twitch.tv/kraken/streams/" + this->roomID);
|
||||||
|
|
||||||
util::twitch::get(url, QThread::currentThread(), [this](QJsonObject obj) {
|
util::twitch::get2(url, QThread::currentThread(), [this](rapidjson::Document &d) {
|
||||||
if (obj.value("stream").isNull()) {
|
if (!d.IsObject()) {
|
||||||
|
debug::Log("[TwitchChannel:refreshLiveStatus] root is not an object");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!d.HasMember("stream")) {
|
||||||
|
debug::Log("[TwitchChannel:refreshLiveStatus] Missing stream in root");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto &stream = d["stream"];
|
||||||
|
|
||||||
|
if (!stream.IsObject()) {
|
||||||
|
// Stream is offline (stream is most likely null)
|
||||||
this->setLive(false);
|
this->setLive(false);
|
||||||
} else {
|
return;
|
||||||
auto stream = obj.value("stream").toObject();
|
}
|
||||||
this->streamViewerCount = QString::number(stream.value("viewers").toDouble());
|
|
||||||
this->streamGame = stream.value("game").toString();
|
if (!stream.HasMember("viewers") || !stream.HasMember("game") ||
|
||||||
this->streamStatus = stream.value("channel").toObject().value("status").toString();
|
!stream.HasMember("channel") || !stream.HasMember("created_at")) {
|
||||||
QDateTime since =
|
debug::Log("[TwitchChannel:refreshLiveStatus] Missing members in stream");
|
||||||
QDateTime::fromString(stream.value("created_at").toString(), Qt::ISODate);
|
this->setLive(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const rapidjson::Value &streamChannel = stream["channel"];
|
||||||
|
|
||||||
|
if (!streamChannel.IsObject() || !streamChannel.HasMember("status")) {
|
||||||
|
debug::Log("[TwitchChannel:refreshLiveStatus] Missing member \"status\" in channel");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stream is live
|
||||||
|
this->streamViewerCount = QString::number(stream["viewers"].GetInt());
|
||||||
|
this->streamGame = stream["game"].GetString();
|
||||||
|
this->streamStatus = streamChannel["status"].GetString();
|
||||||
|
QDateTime since = QDateTime::fromString(stream["created_at"].GetString(), Qt::ISODate);
|
||||||
auto diff = since.secsTo(QDateTime::currentDateTime());
|
auto diff = since.secsTo(QDateTime::currentDateTime());
|
||||||
this->streamUptime =
|
this->streamUptime =
|
||||||
QString::number(diff / 3600) + "h " + QString::number(diff % 3600 / 60) + "m";
|
QString::number(diff / 3600) + "h " + QString::number(diff % 3600 / 60) + "m";
|
||||||
|
|
||||||
this->setLive(true);
|
this->setLive(true);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,9 @@
|
||||||
#include "debug/log.hpp"
|
#include "debug/log.hpp"
|
||||||
#include "util/networkmanager.hpp"
|
#include "util/networkmanager.hpp"
|
||||||
|
|
||||||
|
#include <rapidjson/document.h>
|
||||||
|
#include <rapidjson/error/en.h>
|
||||||
|
#include <rapidjson/error/error.h>
|
||||||
#include <QEventLoop>
|
#include <QEventLoop>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
@ -35,6 +38,26 @@ static QJsonObject parseJSONFromReply(QNetworkReply *reply)
|
||||||
return jsonDoc.object();
|
return jsonDoc.object();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static rapidjson::Document parseJSONFromReply2(QNetworkReply *reply)
|
||||||
|
{
|
||||||
|
rapidjson::Document ret(rapidjson::kNullType);
|
||||||
|
|
||||||
|
if (reply->error() != QNetworkReply::NetworkError::NoError) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray data = reply->readAll();
|
||||||
|
rapidjson::ParseResult result = ret.Parse(data.data(), data.length());
|
||||||
|
|
||||||
|
if (result.Code() != rapidjson::kParseErrorNone) {
|
||||||
|
debug::Log("JSON parse error: {} ({})", rapidjson::GetParseError_En(result.Code()),
|
||||||
|
result.Offset());
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
namespace twitch {
|
namespace twitch {
|
||||||
|
|
||||||
static void get(QString url, const QObject *caller,
|
static void get(QString url, const QObject *caller,
|
||||||
|
@ -50,6 +73,19 @@ static void get(QString url, const QObject *caller,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void get2(QString url, const QObject *caller,
|
||||||
|
std::function<void(rapidjson::Document &)> successCallback)
|
||||||
|
{
|
||||||
|
util::NetworkRequest req(url);
|
||||||
|
req.setCaller(caller);
|
||||||
|
req.setRawHeader("Client-ID", getDefaultClientID());
|
||||||
|
req.setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
||||||
|
req.get([=](QNetworkReply *reply) {
|
||||||
|
auto document = parseJSONFromReply2(reply);
|
||||||
|
successCallback(document);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
static void getAuthorized(QString url, const QString &clientID, const QString &oauthToken,
|
static void getAuthorized(QString url, const QString &clientID, const QString &oauthToken,
|
||||||
const QObject *caller, std::function<void(QJsonObject &)> successCallback)
|
const QObject *caller, std::function<void(QJsonObject &)> successCallback)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue