forked from detain/skyscraper
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathabstractscraper.cpp
More file actions
822 lines (750 loc) · 26 KB
/
abstractscraper.cpp
File metadata and controls
822 lines (750 loc) · 26 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/***************************************************************************
* abstractscraper.cpp
*
* Wed Jun 18 12:00:00 CEST 2017
* Copyright 2017 Lars Muldjord
* muldjordlars@gmail.com
****************************************************************************/
/*
* 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 "abstractscraper.h"
#include "gameentry.h"
#include "nametools.h"
#include "platform.h"
#include "strtools.h"
#include <QDomDocument>
#include <QJsonDocument>
#include <QJsonObject>
#include <QRegularExpression>
#include <QStringBuilder>
static const QRegularExpression RE_THE = QRegularExpression(", [Tt]he");
static const QRegularExpression RE_VERSION = QRegularExpression(
" v[.]{0,1}([0-9]{1}[0-9]{0,2}[.]{0,1}[0-9]{1,4}|[IVX]{1,5})$");
static const QRegularExpression RE_REGIONS = QRegularExpression(
"\\((\\D+?)\\)", QRegularExpression::CaseInsensitiveOption);
AbstractScraper::AbstractScraper(Settings *config,
QSharedPointer<NetManager> manager,
MatchType type, int timeout)
: config(config), type(type) {
netComm = new NetComm(manager, timeout);
connect(netComm, &NetComm::dataReady, &q, &QEventLoop::quit);
}
AbstractScraper::~AbstractScraper() { netComm->deleteLater(); }
void AbstractScraper::getSearchResults(QList<GameEntry> &gameEntries,
QString searchName, QString platform) {
netComm->request(searchUrlPre + searchName + searchUrlPost);
q.exec();
data = netComm->getData();
GameEntry game;
while (data.indexOf(searchResultPre.toUtf8()) != -1) {
nomNom(searchResultPre);
// Digest until url
for (const auto &nom : urlPre) {
nomNom(nom);
}
game.url = baseUrl + "/" + data.left(data.indexOf(urlPost.toUtf8()));
// Digest until title
for (const auto &nom : titlePre) {
nomNom(nom);
}
game.title = data.left(data.indexOf(titlePost.toUtf8()));
// Digest until platform
for (const auto &nom : platformPre) {
nomNom(nom);
}
game.platform = data.left(data.indexOf(platformPost.toUtf8()));
if (platformMatch(game.platform, platform)) {
gameEntries.append(game);
}
}
}
void AbstractScraper::getGameData(GameEntry &game) {
netComm->request(game.url);
q.exec();
data = netComm->getData();
// ncprintf("URL IS: '%s'\n", game.url.toStdString().c_str());
// ncprintf("DATA IS:\n'%s'\n", data.data());
populateGameEntry(game);
}
void AbstractScraper::getTitle(GameEntry &) {}
void AbstractScraper::populateGameEntry(GameEntry &game) {
for (int t : fetchOrder) {
switch (t) {
case GameEntry::Elem::TITLE:
getTitle(game);
break;
case GameEntry::Elem::DESCRIPTION:
getDescription(game);
break;
case GameEntry::Elem::DEVELOPER:
getDeveloper(game);
break;
case GameEntry::Elem::PUBLISHER:
getPublisher(game);
break;
case GameEntry::Elem::PLAYERS:
getPlayers(game);
break;
case GameEntry::Elem::AGES:
getAges(game);
break;
case GameEntry::Elem::RATING:
getRating(game);
break;
case GameEntry::Elem::TAGS:
getTags(game);
break;
case GameEntry::Elem::RELEASEDATE:
getReleaseDate(game);
break;
case GameEntry::Elem::COVER:
if (config->cacheCovers) {
getCover(game);
}
break;
case GameEntry::Elem::SCREENSHOT:
if (config->cacheScreenshots) {
getScreenshot(game);
}
break;
case GameEntry::Elem::WHEEL:
if (config->cacheWheels) {
getWheel(game);
}
break;
case GameEntry::Elem::MARQUEE:
if (config->cacheMarquees) {
getMarquee(game);
}
break;
case GameEntry::Elem::TEXTURE:
if (config->cacheTextures) {
getTexture(game);
}
break;
case GameEntry::Elem::VIDEO:
if (config->videos) {
getVideo(game);
}
break;
case GameEntry::Elem::MANUAL:
if (config->manuals) {
getManual(game);
}
break;
case GameEntry::Elem::FANART:
if (config->fanart) {
getFanart(game);
}
break;
case GameEntry::Elem::BACKCOVER:
if (config->backcovers) {
getBackcover(game);
}
break;
default:;
}
}
}
// TODO: openretro
void AbstractScraper::getDescription(GameEntry &game) {
if (descriptionPre.isEmpty()) {
return;
}
for (const auto &nom : descriptionPre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : descriptionPre) {
nomNom(nom);
}
game.description = data.left(data.indexOf(descriptionPost.toUtf8()))
.replace("<", "<")
.replace(">", ">");
game.description = game.description.replace("\\n", "\n");
// Remove all html tags within description
game.description = StrTools::stripHtmlTags(game.description);
}
// TODO: openretro
void AbstractScraper::getDeveloper(GameEntry &game) {
for (const auto &nom : developerPre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : developerPre) {
nomNom(nom);
}
game.developer = data.left(data.indexOf(developerPost.toUtf8()));
}
// TODO: openretro
void AbstractScraper::getPublisher(GameEntry &game) {
if (publisherPre.isEmpty()) {
return;
}
for (const auto &nom : publisherPre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : publisherPre) {
nomNom(nom);
}
game.publisher = data.left(data.indexOf(publisherPost.toUtf8()));
}
// TODO: openretro
void AbstractScraper::getPlayers(GameEntry &game) {
if (playersPre.isEmpty()) {
return;
}
for (const auto &nom : playersPre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : playersPre) {
nomNom(nom);
}
game.players = data.left(data.indexOf(playersPost.toUtf8()));
}
// TODO: only for html scrape modules (currently none)
void AbstractScraper::getAges(GameEntry &game) {
if (agesPre.isEmpty()) {
return;
}
for (const auto &nom : agesPre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : agesPre) {
nomNom(nom);
}
game.ages = data.left(data.indexOf(agesPost.toUtf8()));
}
// TODO: openretro
void AbstractScraper::getTags(GameEntry &game) {
if (tagsPre.isEmpty()) {
return;
}
for (const auto &nom : tagsPre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : tagsPre) {
nomNom(nom);
}
game.tags = data.left(data.indexOf(tagsPost.toUtf8()));
}
// TODO: openretro
void AbstractScraper::getRating(GameEntry &game) {
if (ratingPre.isEmpty()) {
return;
}
for (const auto &nom : ratingPre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : ratingPre) {
nomNom(nom);
}
game.rating = data.left(data.indexOf(ratingPost.toUtf8()));
bool toDoubleOk = false;
double rating = game.rating.toDouble(&toDoubleOk);
if (toDoubleOk) {
game.rating = QString::number(rating / 5.0);
} else {
game.rating = "";
}
}
// TODO: openretro
void AbstractScraper::getReleaseDate(GameEntry &game) {
if (releaseDatePre.isEmpty()) {
return;
}
for (const auto &nom : releaseDatePre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : releaseDatePre) {
nomNom(nom);
}
game.releaseDate =
data.left(data.indexOf(releaseDatePost.toUtf8())).simplified();
}
// TODO: openretro
void AbstractScraper::getCover(GameEntry &game) {
if (coverPre.isEmpty()) {
return;
}
for (const auto &nom : coverPre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : coverPre) {
nomNom(nom);
}
QString coverUrl =
data.left(data.indexOf(coverPost.toUtf8())).replace("&", "&");
if (coverUrl.left(4) != "http") {
coverUrl.prepend(baseUrl + (coverUrl.left(1) == "/" ? "" : "/"));
}
game.coverData = downloadMedia(coverUrl);
}
// TODO: openretro only
void AbstractScraper::getScreenshot(GameEntry &game) {
if (screenshotPre.isEmpty()) {
return;
}
// Check that we have enough screenshots
int screens = data.count(screenshotCounter.toUtf8());
if (screens >= 1) {
for (int a = 0; a < screens - (screens / 2); a++) {
for (const auto &nom : screenshotPre) {
nomNom(nom);
}
}
QString screenshotUrl = data.left(data.indexOf(screenshotPost.toUtf8()))
.replace("&", "&");
if (screenshotUrl.left(4) != "http") {
screenshotUrl.prepend(baseUrl +
(screenshotUrl.left(1) == "/" ? "" : "/"));
}
game.screenshotData = downloadMedia(screenshotUrl);
}
}
// TODO: only for html scrape modules (currently none)
void AbstractScraper::getWheel(GameEntry &game) {
if (wheelPre.isEmpty()) {
return;
}
for (const auto &nom : wheelPre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : wheelPre) {
nomNom(nom);
}
QString wheelUrl =
data.left(data.indexOf(wheelPost.toUtf8())).replace("&", "&");
if (wheelUrl.left(4) != "http") {
wheelUrl.prepend(baseUrl + (wheelUrl.left(1) == "/" ? "" : "/"));
}
game.wheelData = downloadMedia(wheelUrl);
}
// TODO: openretro only
void AbstractScraper::getMarquee(GameEntry &game) {
if (marqueePre.isEmpty()) {
return;
}
for (const auto &nom : marqueePre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : marqueePre) {
nomNom(nom);
}
QString marqueeUrl =
data.left(data.indexOf(marqueePost.toUtf8())).replace("&", "&");
if (marqueeUrl.left(4) != "http") {
marqueeUrl.prepend(baseUrl + (marqueeUrl.left(1) == "/" ? "" : "/"));
}
game.marqueeData = downloadMedia(marqueeUrl);
}
// TODO: only for html scrape modules (currently none)
void AbstractScraper::getTexture(GameEntry &game) {
if (texturePre.isEmpty()) {
return;
}
for (const auto &nom : texturePre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : texturePre) {
nomNom(nom);
}
QString textureUrl =
data.left(data.indexOf(texturePost.toUtf8())).replace("&", "&");
if (textureUrl.left(4) != "http") {
textureUrl.prepend(baseUrl + (textureUrl.left(1) == "/" ? "" : "/"));
}
game.textureData = downloadMedia(textureUrl);
}
// TODO: only for html scrape modules (currently none)
void AbstractScraper::getVideo(GameEntry &game) {
if (videoPre.isEmpty()) {
return;
}
for (const auto &nom : videoPre) {
if (!checkNom(nom)) {
return;
}
}
for (const auto &nom : videoPre) {
nomNom(nom);
}
QString videoUrl =
data.left(data.indexOf(videoPost.toUtf8())).replace("&", "&");
if (videoUrl.left(4) != "http") {
videoUrl.prepend(baseUrl + (videoUrl.left(1) == "/" ? "" : "/"));
}
game.videoData = downloadMedia(videoUrl, false);
if (!game.videoData.isEmpty())
game.videoFormat = videoUrl.right(3);
}
QByteArray AbstractScraper::downloadMedia(const QString &url, bool isImage) {
netComm->request(url);
q.exec();
QByteArray d;
QImage img;
if (netComm->ok() && (!isImage || img.loadFromData(netComm->getData()))) {
d = netComm->getData();
}
return d;
}
void AbstractScraper::nomNom(const QString nom, bool including) {
data.remove(0, data.indexOf(nom.toUtf8()) + (including ? nom.length() : 0));
}
bool AbstractScraper::checkNom(const QString nom) {
if (data.indexOf(nom.toUtf8()) != -1) {
return true;
}
return false;
}
QString AbstractScraper::lookupArcadeTitle(const QString &baseName) {
if (config->arcadePlatform) {
return config->mameMap[baseName];
}
return "";
}
QString AbstractScraper::lookupAliasMap(const QString &baseName,
QString &debug) {
if (!config->aliasMap[baseName].isEmpty()) {
debug.append("'aliasMap.csv' entry found\n");
QString aliasTitle = config->aliasMap[baseName];
debug.append("Alias name: '" + aliasTitle + "'\n");
return aliasTitle;
}
return baseName;
}
QString AbstractScraper::lookupSearchName(const QFileInfo &info,
const QString &baseName,
QString &debug) {
QString searchName = baseName;
if (QString aliasTitle = lookupAliasMap(baseName, debug);
aliasTitle != searchName) {
searchName = aliasTitle;
} else if (info.suffix() == "lha") {
if (QString whdTitle = config->whdLoadMap[baseName].first;
!whdTitle.isEmpty()) {
debug.append("'whdload_db.xml' entry found\n");
searchName = whdTitle;
debug.append("Entry name: '" + searchName + "'\n");
} else {
searchName = NameTools::getNameWithSpaces(baseName);
}
} else if (config->platform == "scummvm") {
searchName = NameTools::getScummName(info, baseName, config->scummIni);
debug.append("ScummVM ROM '" + info.fileName() + "' resolved to '" +
searchName + "'\n");
} else if (QString romTitle = lookupArcadeTitle(baseName);
!romTitle.isEmpty()) {
debug.append("'mameMap.csv' entry found\n");
searchName = romTitle;
debug.append("Entry name: '" + searchName + "'\n");
}
return searchName;
}
QList<QString> AbstractScraper::getSearchNames(const QFileInfo &info,
QString &debug) {
const QString baseName = info.completeBaseName();
QList<QString> searchNames;
QString searchName = baseName;
debug.append("Base name: '" + baseName + "'\n");
if (config->scraper != "import") {
searchName = lookupSearchName(info, baseName, debug);
}
Q_ASSERT(!searchName.isEmpty());
searchName = removeStopwords(searchName);
searchNames.append(NameTools::getUrlQueryName(searchName));
// If search name has a subtitle, also search without subtitle
if (searchName.contains(":") || searchName.contains(" - ")) {
QString noSubtitle =
searchName.left(searchName.indexOf(":")).simplified();
noSubtitle = noSubtitle.left(noSubtitle.indexOf(" - ")).simplified();
// Only add if longer than 3. We don't want to search for "the" for
// instance
if (noSubtitle.length() > 3)
searchNames.append(NameTools::getUrlQueryName(noSubtitle));
}
// If the search name has a Roman numeral, also search for an integer
// numeral version, vice-versa
if (NameTools::hasRomanNumeral(searchName) ||
NameTools::hasArabicNumeral(searchName)) {
if (NameTools::hasRomanNumeral(searchName)) {
searchName = NameTools::convertToArabicNumeral(searchName);
} else {
searchName = NameTools::convertToRomanNumeral(searchName);
}
searchNames.append(NameTools::getUrlQueryName(searchName));
// If search name has a subtitle, also search without subtitle
if (searchName.contains(":") || searchName.contains(" - ")) {
QString noSubtitle =
searchName.left(searchName.indexOf(":")).simplified();
noSubtitle =
noSubtitle.left(noSubtitle.indexOf(" - ")).simplified();
if (noSubtitle.length() > 3) {
// Only add if longer than 3. We don't want to search for "the"
// for instance
searchNames.append(NameTools::getUrlQueryName(noSubtitle));
}
}
}
return searchNames;
}
QString AbstractScraper::getCompareTitle(const QFileInfo &info) {
const QString baseName = info.completeBaseName();
QString compareTitle;
if (!config->aliasMap[baseName].isEmpty()) {
compareTitle = config->aliasMap[baseName];
} else if (info.suffix() == "lha") {
if (QString whdTitle = config->whdLoadMap[baseName].first;
!whdTitle.isEmpty()) {
compareTitle = whdTitle;
} else {
compareTitle = NameTools::getNameWithSpaces(baseName);
}
} else if (config->platform == "scummvm") {
compareTitle =
NameTools::getScummName(info, baseName, config->scummIni);
} else if (QString romTitle = lookupArcadeTitle(baseName);
!romTitle.isEmpty()) {
compareTitle = romTitle;
} else {
compareTitle = baseName;
}
// Now create actual compareTitle
compareTitle = compareTitle.replace("_", " ");
compareTitle = StrTools::stripBrackets(compareTitle);
QRegularExpressionMatch match;
// Always move ", The" to the beginning of the name
match = RE_THE.match(compareTitle);
if (match.hasMatch()) {
compareTitle = compareTitle.replace(match.captured(0), "")
.prepend(match.captured(0).right(3) + " ");
}
// Remove "vX.XXX" versioning string if one is found
match = RE_VERSION.match(compareTitle);
if (match.hasMatch() && match.capturedStart(0) != -1) {
compareTitle = compareTitle.left(match.capturedStart(0)).simplified();
}
return compareTitle;
}
void AbstractScraper::detectRegionFromFilename(const QFileInfo &info) {
// the next statement redundant, but leave it here for the unit tests
regionPrios = config->regionPrios;
const QString fn = info.fileName();
QRegularExpressionMatchIterator matchIter = RE_REGIONS.globalMatch(fn);
QStringList addRegionPrios;
const bool regionsInline = config->regionFromFilename == "inline";
// loop over region infos from filename
while (matchIter.hasNext()) {
QString regionString = matchIter.next().captured().toLower();
if (regionString == "(jue)") {
regionString = "japan|usa|europe";
} else if (regionString == "(ue)") {
regionString = "usa|europe";
} else if (regionString != "(e)" && regionString != "(j)" &&
regionString != "(u)") {
// remove parenthesis
regionString = regionString.mid(1, regionString.length() - 2);
}
while (!regionString.isEmpty()) {
bool detectedRegion = false;
QListIterator<QPair<QString, QString>> iter(regionMap());
while (iter.hasNext()) {
QPair<QString, QString> e = iter.next();
QString fn_regio = e.first;
QString sky_regio_key = e.second;
if (regionString.startsWith(fn_regio)) {
qDebug() << "matched" << fn_regio;
// map to Skyscraper's short-names (sky_regio_key)
if (regionsInline) {
if (!regionPrios.contains(sky_regio_key) &&
!addRegionPrios.contains(sky_regio_key)) {
addRegionPrios.append(sky_regio_key);
}
} else {
// regionFromFilename == "first"
if (!addRegionPrios.contains(sky_regio_key)) {
addRegionPrios.append(sky_regio_key);
}
}
regionString = regionString.replace(fn_regio, "");
if (!regionString.isEmpty()) {
// remove possible separators (comma et al.) if
// regionString was "europe, japan" -> retain "japan"
regionString = regionString.replace(
QRegularExpression("^([^a-z]+)?"), "");
}
detectedRegion = true;
break;
}
}
if (!detectedRegion) {
// no match was found in regionMap()
break;
}
}
}
QStringList rankedRegionPrios;
QStringList retainedRegionPrios = regionPrios;
for (int i = regionPrios.size() - 1; i >= 0; i--) {
const QString prioRegion = regionPrios.at(i);
if (addRegionPrios.contains(prioRegion)) {
if (regionsInline) {
rankedRegionPrios.prepend(prioRegion);
addRegionPrios.removeAt(addRegionPrios.indexOf(prioRegion));
}
retainedRegionPrios.removeAt(
retainedRegionPrios.indexOf(prioRegion));
}
}
if (regionsInline)
regionPrios = rankedRegionPrios + retainedRegionPrios + addRegionPrios;
else
regionPrios = addRegionPrios + retainedRegionPrios;
}
void AbstractScraper::runPasses(QList<GameEntry> &gameEntries,
const QFileInfo &info, QString &output,
QString &debug) {
// Reset region priorities to original list from Settings
regionPrios = config->regionPrios;
if (config->region.isEmpty() && config->regionFromFilename != "off") {
detectRegionFromFilename(info);
}
QList<QString> searchNames;
if (!config->searchName.isEmpty()) {
// set the string provided by "--query"
searchNames.append(applyQuerySearchName(config->searchName));
} else {
searchNames = getSearchNames(info, debug);
}
if (searchNames.isEmpty()) {
return;
}
if (config->verbosity >= 3) {
int i = 0;
for (const auto &sn : searchNames) {
debug.append(QString("Search name #%1: '%2'\n")
.arg(QString::number(++i), sn));
}
}
int pass = 0;
for (const auto &sn : searchNames) {
output.append("\033[1;35mPass " % QString::number(++pass) % "\033[0m ");
getSearchResults(gameEntries, sn, config->platform);
if (config->verbosity >= 3) {
debug.append("Tried with: '" % sn % "'\n");
debug.append("Platform : " % config->platform % "\n");
QStringList candidates;
for (auto const &ge : gameEntries) {
QString c = ge.title;
QStringList extra;
if (!ge.releaseDate.isEmpty())
extra.append(ge.releaseDate);
if (!ge.publisher.isEmpty())
extra.append(ge.publisher);
if (!extra.isEmpty())
c = c % " (" % extra.join(", ") % ")";
candidates.append(c);
}
if (!candidates.empty()) {
candidates.sort(Qt::CaseInsensitive);
QString pad1 = candidates.length() > 3 ? "\n " : "";
QString pad2 = candidates.length() > 3 ? "\n " : ", ";
debug.append(QString("Candidate%1: ")
.arg(candidates.length() == 1 ? " " : "s") %
pad1 % candidates.join(pad2) % "\n");
}
}
if (!gameEntries.isEmpty()) {
break;
}
}
}
bool AbstractScraper::platformMatch(QString found, QString platform) {
for (const auto &p : Platform::get().getAliases(platform)) {
if (found.toLower() == p) {
return true;
}
}
return false;
}
QVector<int> AbstractScraper::getPlatformId(const QString) {
return QVector<int>();
}
/*
for ingesting of
mobygames_platforms.json (unused)
screenscraper_platforms.json (unused)
tgdb_developers.json
tgdb_genres.json
tgdb_platforms.json
tgdb_publishers.json
*/
QVariantMap AbstractScraper::readJson(const QString &filename) {
QVariantMap m;
QFile jsonFile(filename);
QJsonObject jsonObj;
bool canRead = jsonFile.open(QIODevice::ReadOnly);
if (canRead) {
jsonObj = QJsonDocument::fromJson(jsonFile.readAll()).object();
if (!jsonObj.isEmpty()) {
m = jsonObj.toVariantMap();
jsonFile.close();
}
}
if (!canRead) {
ncprintf("\033[1;31mFile '%s' not found or not readable. Please "
"fix.\nNot scraping...\n\033[0m",
filename.toUtf8().constData());
} else if (jsonObj.isEmpty()) {
ncprintf("\033[1;31mFile '%s' has insky_regio_keyid JSON format. "
"Please fix.\nNot "
"scraping...\n\033[0m",
filename.toUtf8().constData());
}
return m;
}
void AbstractScraper::bury(const int &returnCode, const QString &effect,
const QString &cause) {
if (config->stdErr) {
fprintf(stderr, "Skyscraper: %s: %s\n", effect.toStdString().c_str(),
cause.toStdString().c_str());
}
exit(returnCode);
}