forked from detain/skyscraper
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathigdb.cpp
More file actions
484 lines (444 loc) · 15.4 KB
/
igdb.cpp
File metadata and controls
484 lines (444 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/***************************************************************************
* igdb.cpp
*
* Sun Aug 26 12:00:00 CEST 2018
* Copyright 2018 Lars Muldjord
* Copyright 2025 Gemba @ GitHub
****************************************************************************/
/*
* This file is part of skyscraper.
*
* skyscraper is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* skyscraper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with skyscraper; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "igdb.h"
#include "gameentry.h"
#include "nametools.h"
#include "strtools.h"
#include <QJsonArray>
#include <QRandomGenerator>
#include <QRegularExpression>
#include <QStringBuilder>
Igdb::Igdb(Settings *config, QSharedPointer<NetManager> manager)
: AbstractScraper(config, manager, MatchType::MATCH_MANY) {
QPair<QString, QString> clientIdHeader;
clientIdHeader.first = "Client-ID";
clientIdHeader.second = config->user;
QPair<QString, QString> tokenHeader;
tokenHeader.first = "Authorization";
tokenHeader.second = "Bearer " + config->igdbToken;
headers.append(clientIdHeader);
headers.append(tokenHeader);
connect(&limitTimer, &QTimer::timeout, &limiter, &QEventLoop::quit);
/* 1.1 second request limit set a bit above 1.0 as requested by the good
* folks at IGDB. Don't change! It will break the module stability. */
limitTimer.setInterval(1100);
limitTimer.setSingleShot(false);
limitTimer.start();
baseUrl = "https://api.igdb.com/v4";
searchUrlPre = baseUrl;
fetchOrder.append(GameEntry::Elem::RELEASEDATE);
fetchOrder.append(GameEntry::Elem::RATING);
fetchOrder.append(GameEntry::Elem::PUBLISHER);
fetchOrder.append(GameEntry::Elem::DEVELOPER);
fetchOrder.append(GameEntry::Elem::DESCRIPTION);
fetchOrder.append(GameEntry::Elem::PLAYERS);
fetchOrder.append(GameEntry::Elem::TAGS);
fetchOrder.append(GameEntry::Elem::AGES);
fetchOrder.append(GameEntry::Elem::SCREENSHOT);
fetchOrder.append(GameEntry::Elem::COVER);
fetchOrder.append(GameEntry::Elem::FANART); /* IGDB: artworks */
}
void Igdb::getSearchResults(QList<GameEntry> &gameEntries, QString searchName,
QString platform) {
limiter.exec();
const QStringList fields = {
// clang-format off
"game.name",
"game.platforms.name",
"game.release_dates.date",
"game.release_dates.platform"
// clang-format on
};
// Request list of games but don't allow re-releases ("game.version_parent =
// null")
QString clause =
QString("search \"%1\"; where game != null").arg(searchName);
if (searchName.startsWith("id=")) {
// query by game id
bool ok;
int gameId = (searchName.split("=").at(1)).toInt(&ok);
if (!ok) {
gameId = -1;
}
clause = QString("where game = %1").arg(gameId);
}
const QString postData =
QString("fields %1; %2 & game.version_parent = null;")
.arg(fields.join(","))
.arg(clause);
qDebug() << headers;
qDebug() << baseUrl + "/search/";
qDebug() << postData;
netComm->request(baseUrl + "/search/", postData, headers);
q.exec();
data = netComm->getData();
if (!netComm->ok()) {
const QString httpStatusStr =
" (HTTP status " % QString::number(netComm->getHttpStatus()) % ")";
ncprintf(
"\033[1;31mThe IGDB request failed%s, cannot proceed!\033[0m\n",
config->verbosity > 1 ? httpStatusStr.toStdString().c_str() : "");
reqRemaining = 0;
return;
}
jsonDoc = QJsonDocument::fromJson(data);
if (jsonDoc.isEmpty()) {
return;
}
if (jsonDoc.object()["message"].toString() == "Too Many Requests") {
ncprintf("\033[1;31mThe IGDB requests per second limit has been "
"exceeded, cannot continue!\033[0m\n");
reqRemaining = 0;
return;
}
QJsonArray jsonGames = jsonDoc.array();
for (const auto &jsonGame : jsonGames) {
GameEntry game;
const QJsonObject gameObj = jsonGame.toObject()["game"].toObject();
game.title = gameObj["name"].toString();
game.id = QString::number(gameObj["id"].toInt());
QJsonArray jsonPlatforms = gameObj["platforms"].toArray();
QJsonArray jsonReleaseDates = gameObj["release_dates"].toArray();
for (const auto &jsonPlatform : jsonPlatforms) {
int platformId = jsonPlatform.toObject()["id"].toInt();
game.id.append(";" + QString::number(platformId));
game.platform = jsonPlatform.toObject()["name"].toString();
if (platformMatch(game.platform, platform)) {
for (const auto &releaseDate : jsonReleaseDates) {
if (releaseDate.toObject()["platform"].toInt() ==
platformId) {
game.releaseDate =
QDateTime::fromSecsSinceEpoch(
(qint64)releaseDate.toObject()["date"].toInt())
.date()
.toString(Qt::ISODate);
}
}
gameEntries.append(game);
}
}
}
}
void Igdb::getGameData(GameEntry &game) {
limiter.exec();
const QStringList fields = {
// clang-format off
"age_ratings.organization",
"age_ratings.rating_category",
"cover.url",
"game_modes.slug",
"genres.name",
"involved_companies.company.name",
"involved_companies.developer",
"involved_companies.publisher",
"release_dates.date",
"release_dates.platform",
"release_dates.region",
"screenshots.url",
"summary",
"total_rating",
/* fanart */
"artworks.artwork_type",
"artworks.url",
"artworks.width",
"artworks.height",
// clang-format on
};
/* artwork_type, resolve order:
* 2: "Key art without logo"
* 1: "Artwork"
* 3: "Key art with logo"
*/
const QString postData = QString("fields %1; where id = %2;")
.arg(fields.join(","))
.arg(game.id.split(";").first());
netComm->request(baseUrl + "/games/", postData, headers);
qDebug() << headers;
qDebug() << baseUrl + "/games/";
qDebug() << postData;
q.exec();
data = netComm->getData();
jsonDoc = QJsonDocument::fromJson(data);
if (jsonDoc.isEmpty()) {
return;
}
jsonObj = jsonDoc.array().first().toObject();
populateGameEntry(game);
}
void Igdb::getReleaseDate(GameEntry &game) {
QJsonArray jsonDates = jsonObj["release_dates"].toArray();
bool regionMatch = false;
/* http --print bH https://api.igdb.com/v4/release_date_regions
* "Client-ID:<id>" "Authorization: Bearer <auth>" --raw='fields region;'
*/
// regions as Skyscraper keywords
QStringList igdbRegions = {"eu", "us", "au", "nz", "jp",
"cn", "asi", "wor", "kr", "br"};
for (const auto ®ion : regionPrios) {
for (const auto &jsonDate : jsonDates) {
int regionEnum = jsonDate.toObject()["region"].toInt();
QString curRegion = "";
if (regionEnum > 0 && regionEnum < igdbRegions.length()) {
// resolve to skyscraper region identifier
curRegion = igdbRegions.at(regionEnum);
}
if (QString::number(jsonDate.toObject()["platform"].toInt()) ==
game.id.split(";").last() &&
region == curRegion) {
game.releaseDate =
QDateTime::fromSecsSinceEpoch(
(qint64)jsonDate.toObject()["date"].toInt())
.toString("yyyy-MM-dd");
regionMatch = true;
break;
}
}
if (regionMatch)
break;
}
}
void Igdb::getPlayers(GameEntry &game) {
// This is a bit of a hack. The unique identifiers are as follows:
// 1 = Single Player
// 2 = Multiplayer
// 3 = Cooperative
// 4 = Split screen
// 5 = MMO
// So basically if != 1 it's at least 2 players. That's all we can gather
// from this
game.players = "1";
QJsonArray jsonPlayers = jsonObj["game_modes"].toArray();
for (const auto &jsonPlayer : jsonPlayers) {
if (jsonPlayer.toObject()["id"].toInt() != 1) {
game.players = "2";
break;
}
}
}
void Igdb::getTags(GameEntry &game) {
QJsonArray jsonGenres = jsonObj["genres"].toArray();
for (const auto &jsonGenre : jsonGenres) {
game.tags.append(jsonGenre.toObject()["name"].toString() + ", ");
}
game.tags.chop(2);
}
void Igdb::getAges(GameEntry &game) {
// https://api.igdb.com/v4/age_ratings
int agesEnum = jsonObj["age_ratings"]
.toArray()
.first()
.toObject()["rating_category"]
.toInt();
switch (agesEnum) {
case 1: // Three
game.ages = "3";
break;
case 2: // Seven
case 28: // IND L
case 34: // ACB G
game.ages = "7";
break;
case 3: // Twelve
case 14: // CERO B
case 20: // USK 12
case 24: // GRAC Twelve
case 30: // IND Twelve
game.ages = "12";
break;
case 4: // Sixteen
case 21: // USK 16
case 32: // IND Sixteen
case 36: // ACB M
case 37: // ACB MA15
game.ages = "16";
break;
case 7: // EC
game.ages = "EC";
break;
case 8: // E
case 13: // CERO A
case 18: // USK 0
case 23: // GRAC All
game.ages = "E";
break;
case 9: // E10
case 29: // IND Ten
game.ages = "E10";
break;
case 10: // T
game.ages = "T";
break;
case 11: // M
game.ages = "M";
break;
case 5: // Eighteen
case 12: // AO
case 17: // CERO Z
case 22: // USK 18
case 33: // IND Eighteen
case 38: // ACB R18
case 39: // ACB RC
game.ages = "AO";
break;
case 15: // CERO C
case 25: // GRAC Fifteen
case 35: // ACB PG
game.ages = "15";
break;
case 16: // CERO D
game.ages = "17";
break;
case 19: // USK 6
game.ages = "6";
break;
case 26: // GRAC Eighteen
// https://en.wikipedia.org/wiki/Game_Rating_and_Administration_Committee#Former_rating
game.ages = "19";
break;
case 31: // IND Fourteen
game.ages = "14";
break;
case 6: // RP
case 27: // GRAC Testing
default:
break;
}
}
void Igdb::getPublisher(GameEntry &game) {
QJsonArray jsonCompanies = jsonObj["involved_companies"].toArray();
for (const auto &jsonCompany : jsonCompanies) {
if (jsonCompany.toObject()["publisher"].toBool() == true) {
game.publisher =
jsonCompany.toObject()["company"].toObject()["name"].toString();
return;
}
}
}
void Igdb::getDeveloper(GameEntry &game) {
QJsonArray jsonCompanies = jsonObj["involved_companies"].toArray();
for (const auto &jsonCompany : jsonCompanies) {
if (jsonCompany.toObject()["developer"].toBool() == true) {
game.developer =
jsonCompany.toObject()["company"].toObject()["name"].toString();
return;
}
}
}
void Igdb::getDescription(GameEntry &game) {
QJsonValue jsonValue = jsonObj["summary"];
if (jsonValue != QJsonValue::Undefined) {
game.description = StrTools::stripHtmlTags(jsonValue.toString());
}
}
void Igdb::getRating(GameEntry &game) {
QJsonValue jsonValue = jsonObj["total_rating"];
if (jsonValue != QJsonValue::Undefined) {
double rating = jsonValue.toDouble();
if (rating > 0.0) {
rating = rating / 100.0;
if (rating < 1.0) {
rating += 0.005;
}
game.rating = QString::number(rating, 'g', 2);
}
}
}
void Igdb::getScreenshot(GameEntry &game) {
QJsonArray mediaFiles = jsonObj["screenshots"].toArray();
int chosen = 1;
if (mediaFiles.count() < 1) {
return;
}
const int offset = 2;
if (mediaFiles.count() > offset) {
// Claim: First 2 are almost always not ingame, so skip those if we have
// 3 or more
chosen = offset + QRandomGenerator::system()->bounded(
mediaFiles.count() - offset);
}
QString mediaUrl = mediaFiles.at(chosen).toObject()["url"].toString();
QByteArray img = mediaFromJsonRef("screenshots", mediaUrl);
if (!img.isEmpty()) {
game.screenshotData = img;
}
}
void Igdb::getCover(GameEntry &game) {
QString mediaUrl = jsonObj["cover"].toObject()["url"].toString();
QByteArray img = mediaFromJsonRef("cover", mediaUrl);
if (!img.isEmpty()) {
game.coverData = img;
}
}
void Igdb::getFanart(GameEntry &game) {
QJsonArray artworks = jsonObj["artworks"].toArray();
QStringList awkUrls = {"", "", ""};
for (int k = 0; k < artworks.count(); k++) {
QJsonObject awk = artworks.at(k).toObject();
int type = awk["artwork_type"].toInt();
if (type < 1 && type > 3) {
continue;
}
double aspect =
(double)awk["width"].toInt() / (double)awk["height"].toInt();
if (aspect < 0.85) {
qDebug() << "Skipping portrait mode artwork"
<< "https://" % awk["url"].toString();
/* skip portrait artwork */
continue;
}
// Only keep first of each artwork type
if (type == 1 && awkUrls[1].isEmpty()) {
// "Artwork"
awkUrls[1] = awk["url"].toString();
} else if (type == 2 && awkUrls[0].isEmpty()) {
// "Key art without logo"
awkUrls[0] = awk["url"].toString();
} else if (type == 3 && awkUrls[2].isEmpty()) {
// "Key art with logo"
awkUrls[2] = awk["url"].toString();
}
}
qDebug() << "Got Artwork" << awkUrls;
for (auto const &url : awkUrls) {
if (!url.isEmpty()) {
game.fanartData = mediaFromJsonRef("artwork", url);
break;
}
}
}
QList<QString> Igdb::getSearchNames(const QFileInfo &info, QString &debug) {
const QString baseName = info.completeBaseName();
QString searchName = baseName;
debug.append("Base name: '" + baseName + "'\n");
searchName = lookupSearchName(info, baseName, debug);
searchName = StrTools::stripBrackets(searchName);
return QList<QString>{searchName};
}
QByteArray Igdb::mediaFromJsonRef(QString gameMedia, QString mediaUrl) {
mediaUrl = mediaUrl.replace(QRegularExpression("^//"), "https://");
mediaUrl = mediaUrl.replace("/t_thumb/", "/t_1080p/");
qDebug() << gameMedia << mediaUrl;
return downloadMedia(mediaUrl);
}