- Setup C++/Qt6 desktop application structure - Added initial Go proxy backend for TMDB/IGDB integration
75 lines
2.7 KiB
C++
75 lines
2.7 KiB
C++
#include "providers/RawgProvider.h"
|
|
|
|
#include <QNetworkAccessManager>
|
|
#include <QNetworkReply>
|
|
#include <QNetworkRequest>
|
|
#include <QUrlQuery>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
|
|
namespace umt {
|
|
|
|
RawgProvider::RawgProvider(QObject *parent)
|
|
: MetadataProvider(parent)
|
|
, m_nam(new QNetworkAccessManager(this))
|
|
{
|
|
}
|
|
|
|
void RawgProvider::search(const QString &query, MediaType)
|
|
{
|
|
if (m_apiKey.trimmed().isEmpty()) {
|
|
emit failed(QStringLiteral(
|
|
"Für Spiele bitte einen kostenlosen RAWG API-Key in den "
|
|
"Einstellungen hinterlegen (rawg.io/apidocs)."));
|
|
return;
|
|
}
|
|
|
|
QUrl url(QStringLiteral("https://api.rawg.io/api/games"));
|
|
QUrlQuery q;
|
|
q.addQueryItem(QStringLiteral("key"), m_apiKey.trimmed());
|
|
q.addQueryItem(QStringLiteral("search"), query);
|
|
q.addQueryItem(QStringLiteral("page_size"), QStringLiteral("25"));
|
|
url.setQuery(q);
|
|
|
|
QNetworkRequest req(url);
|
|
req.setHeader(QNetworkRequest::UserAgentHeader,
|
|
QStringLiteral("UltimateMediaTracker/1.0"));
|
|
QNetworkReply *r = m_nam->get(req);
|
|
connect(r, &QNetworkReply::finished, this, [this, r]() {
|
|
r->deleteLater();
|
|
if (r->error() != QNetworkReply::NoError) {
|
|
emit failed(QStringLiteral("RAWG: ") + r->errorString());
|
|
return;
|
|
}
|
|
const QJsonDocument doc = QJsonDocument::fromJson(r->readAll());
|
|
QVector<SearchResult> out;
|
|
const QJsonArray games = doc.object().value(QStringLiteral("results")).toArray();
|
|
for (const QJsonValue &v : games) {
|
|
const QJsonObject o = v.toObject();
|
|
SearchResult res;
|
|
res.type = MediaType::Game;
|
|
res.source = name();
|
|
res.title = o.value(QStringLiteral("name")).toString();
|
|
res.externalId = QString::number(o.value(QStringLiteral("id")).toInt());
|
|
|
|
// "released" is an ISO date like "2017-03-03"; take the year prefix.
|
|
const QString released = o.value(QStringLiteral("released")).toString();
|
|
if (released.size() >= 4)
|
|
res.year = released.left(4).toInt();
|
|
|
|
res.coverUrl = o.value(QStringLiteral("background_image")).toString();
|
|
res.externalRating = o.value(QStringLiteral("rating")).toDouble();
|
|
|
|
const QJsonArray genres = o.value(QStringLiteral("genres")).toArray();
|
|
for (const QJsonValue &gv : genres) {
|
|
const QString g = gv.toObject().value(QStringLiteral("name")).toString();
|
|
if (!g.isEmpty()) res.genres << g;
|
|
}
|
|
out.push_back(res);
|
|
}
|
|
emit searchFinished(out);
|
|
});
|
|
}
|
|
|
|
} // namespace umt
|