-
Notifications
You must be signed in to change notification settings - Fork 0
/
xboxcalculator.php
747 lines (665 loc) · 30.4 KB
/
xboxcalculator.php
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
<?php
/**
* This CLI script will scrape your TrueAchievements game collection, and all the current prices for it.
* It will export a .json file which can then be imported into the main mygamecollection script.
*
* If you get outdated info, delete the prices json cache file and try again.
*
* TODO:
* . sales?
* . write everything to database instead of exporting as .json
* - error reporting at the end
* - html interface?
* - urls are www.ta.com/www.ta.com/game
*/
//define('DB_HOST', '');
//define('DB_USER', '');
//define('DB_NAME', '');
//define('DB_PASS', '');
(new XboxCalculator)->run();
class XboxCalculator
{
private $baseurl = 'https://www.trueachievements.com/';
private $gamecollection = 'https://www.trueachievements.com/gamer/%s/gamecollection';
private $gamepage = 'https://www.trueachievements.com/game/%s/achievements';
private $pricepage = 'https://www.trueachievements.com/ajaxfunctions.aspx/ContentRecord_Regional';
private $gamecollectionFullList = 'https://www.trueachievements.com/gamer/%1$s/gamecollection?executeformfunction&function=AjaxList¶ms=oGameCollection%%7C%%26ddlSortBy%%3DTitlename%%26ddlDLCInclusionSetting%%3DNoDLC%%26ddlPlatformIDs%%3D%%26sddOwnerShipStatusIDs%%3D%%26sddPlayStatusIDs%%3D%%26ddlContestStatus%%3DAny%%20status%%26ddlGenreIDs%%3D%%26sddGameMediaID%%3D%%20%%26ddlStartedStatus%%3D0%%26asdGamePropertyID%%3D-1%%26GameView%%3DoptListView%%26MultiEditMode%%3DoptSingleEdit%%26chkColTitleimage%%3DTrue%%26chkColTitlename%%3DTrue%%26chkColPlatform%%3DTrue%%26chkColSiteScore%%3DTrue%%26chkColOfficialScore%%3DTrue%%26chkColItems%%3DTrue%%26chkColCompletionpercentage%%3DTrue%%26chkColMyrating%%3DTrue%%26chkColTimeplayed%%3DTrue%%26chkColLastunlock%%3DTrue%%26chkColOwnershipstatus%%3DTrue%%26chkColPlaystatus%%3DTrue%%26chkColCompletionestimate%%3DTrue%%26chkColGamenotes%%3DTrue%%26txtBaseComparisonGamerID%%3D%3$s%%26oGameCollection_Order%%3DTitlename%%26oGameCollection_Page%%3D%2$s%%26oGameCollection_ItemsPerPage%%3D100%%26oGameCollection_TimeZone%%3DW.%%20Europe%%20Standard%%20Time%%26oGameCollection_ShowAll%%3DFalse%%26txtGamerID%%3D%3$s%%26txtGameRegionID%%3D%4$s%%26txtUseAchievementsForProgress%%3DFalse%%26txtContestID%%3D0';
//regions for ajax price request
private $regions = [
'US' => 1,
'Canada' => 7,
'Australia' => 6,
'UK' => 2,
'Europe' => 4,
'Brazil' => 11,
];
//some games have no boxart, meaning we can't find the game id
private $missinggameids = [
'Trucking 3D (Win 8)' => 5752,
];
//input
private $gamertag;
private $region;
//internal global vars
private $gamerid;
private $games; //array
private $basexpaths; //array
private $skipcache = true;
public function __construct()
{
libxml_use_internal_errors(true);
if (php_sapi_name() == 'cli') {
global $argv;
if (empty($argv[1])) {
printf('Usage: %s gamertag [region]' . PHP_EOL, $argv[0]);
printf('- Regions: %s (default is US)' . PHP_EOL, implode(', ', array_keys($this->regions)));
exit();
}
$this->gamertag = !empty($argv[1]) ? $argv[1] : 'mrbellek';
$this->region = !empty($argv[2]) ? $argv[2] : 'US';
} else {
$gamertag = filter_input(INPUT_GET, 'gamertag');
$this->gamertag = $gamertag ? $gamertag : 'mrbellek';
$region = filter_input(INPUT_GET, 'region');
$this->region = $region ? $region : 'US';
}
if (!isset($this->regions[$this->region])) {
printf('Invalid region "%s", falling back to US' . PHP_EOL, $this->region);
$this->region = 'US';
}
}
public function run()
{
/*try {
$this->oPDO = new PDO(sprintf('mysql:host=%s;dbname=%s', DB_HOST, DB_NAME), DB_USER, DB_PASS);
} catch (Exception $e) {
die('Database connection failed. ' . $e->getMessage());
}*/
printf('Fetching %s\'s game collection..' . PHP_EOL, $this->gamertag);
$this->fetchGamerId();
$this->fetchGameCollection();
//$this->importCsvToDatabase();
//$this->importJsonToDatabase();
//$this->updateDatabase();
//die('stop');
$this->fetchPrices();
$this->printSummary();
//$this->printCsvImport();
}
private function printCsvImport()
{
if (!$this->games || !$this->prices) {
die('Games/prices missing. Import went wrong?' . PHP_EOL);
} elseif ($this->games->length != count($this->prices)) {
die(sprintf('Games count mismatch between list view (%s) and prices (%s)!' . PHP_EOL, $this->games->length, count($this->prices)));
}
die(var_dump($this->prices));
}
private function importJsonToDatabase()
{
$prices = json_decode(file_get_contents('C:\Users\Merijn\Documents\Twitterbot\xboxcalculator-prices-europe.json'));
$i = 0;
foreach ($prices as $game) {
printf('UPDATE mygamecollection SET current_price = %s, status = "%s", last_modified = "%s" WHERE id = %d LIMIT 1;' . PHP_EOL,
$game->price,
$game->status,
$game->timestamp,
$game->id
);
$i++;
}
printf('processed %d games' . PHP_EOL, $i);
}
private function importCsvToDatabase()
{
$csv = array_map('str_getcsv', file('C:\Users\Merijn\Downloads\My Xbox Game Collection - MyGameCollection_ 150218_100728.csv'));
$csv[0][0] = trim($csv[0][0]);
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
array_shift($csv);
foreach ($csv as $game) {
$completiondate = 'NULL';
if (!empty($game['Completion Date'])) {
$a = DateTime::createFromFormat('d/m/Y H:i:s', $game['Completion Date']);
if ($a) {
$completiondate = '"' . $a->format('Y-m-d H:i:s') . '"';
}
}
//printf('UPDATE mygamecollection SET platform = "%s", backcompat = %s, kinect_required = %s, peripheral_required = %s, online_multiplayer = %s, completion_perc = %d, completion_estimate = "%s", hours_played = %d, achievements_won = %d, achievements_total = %d, gamerscore_won = %d, gamerscore_total = %d, ta_score = %d, completion_date = %s, site_rating = %s, format = "%s", walkthrough_url = "%s" WHERE name = "%s" LIMIT 1;' . PHP_EOL,
printf('UPDATE mygamecollection SET backcompat = %s, kinect_required = %s, peripheral_required = %s, online_multiplayer = %s WHERE name = "%s" LIMIT 1;' . PHP_EOL,
($game['BC'] == 'yes' ? 1 : ($game['BC'] == 'no' ? 0 : 'NULL')),
($game['Kinect?'] == 'yes' ? 1 : ($game['Kinect?'] == 'no' ? 0 : 'NULL')),
($game['Periph?'] == 'yes' ? 1 : ($game['Periph?'] == 'no' ? 0 : 'NULL')),
($game['Online?'] == 'yes' ? 1 : ($game['Online?'] == 'no' ? 0 : 'NULL')),
/*$game['My Completion Percentage'],
$game['Completion Estimate'],
$game['Hours Played'],
$game['Achievements Won (incl. DLC)'],
$game['Max Achievements (incl. DLC)'],
$game['GamerScore Won (incl. DLC)'],
$game['Max Gamerscore (incl. DLC)'],
$game['Max TrueAchievement (incl. DLC)'],
$completiondate,
!empty($game['Site Rating']) ? $game['Site Rating'] : 'NULL',
$game['Format'],
$game['Walkthrough'],*/
$game['Game name']
);
}
printf('done processing %d games' . PHP_EOL, count($csv));
}
private function updateDatabase()
{
//update database from current game collection (or cached version)
print('INSERT INTO mygamecollection (id, name, completion_perc, achievements_won, achievements_total, gamerscore_won, gamerscore_total, ta_score, game_url, last_modified) VALUES' . PHP_EOL);
foreach ($this->games as $gamegroupname) {
list($gameid, $title, $url) = $this->parseBasicInfo($gamegroupname);
$game = [
'id' => $gameid,
'name' => $title,
'game_url' => $url,
];
//NB: some games have challenges, which adds the class 'haschallenge' to the div#statistics element
$els = $this->basexpath->query('div[contains(@class,"statistics")]/div[@class="wrapper"]/div[@class="statistic"]', $gamegroupname);
$p = $this->basexpath->query('div/p[@class="bottomborder"]', $els[0]);
if ($p->length) {
//$game['ta_score_unlocked'] = $p[0]->textContent;
$p = $this->basexpath->query('div/p[@class="small"]', $els[0]);
$game['ta_score'] = str_replace(',', '', $p[0]->textContent);
} else {
$p = $this->basexpath->query('div/p', $els[0]);
$game['ta_score'] = str_replace(',', '', $p[0]->textContent);
}
$p = $this->basexpath->query('div/p[@class="bottomborder"]', $els[1]);
if ($p->length) {
$game['gamerscore_won'] = str_replace(',', '', $p[0]->textContent);
$p = $this->basexpath->query('div/p[@class="small"]', $els[1]);
$game['gamerscore_total'] = str_replace(',', '', $p[0]->textContent);
} else {
$p = $this->basexpath->query('div/p', $els[1]);
$game['gamerscore_won'] = 0;
$game['gamerscore_total'] = str_replace(',', '', $p[0]->textContent);
}
$p = $this->basexpath->query('div/p[@class="bottomborder"]', $els[2]);
if ($p->length) {
$game['achievements_won'] = $p[0]->textContent;
$p = $this->basexpath->query('div/p[@class="small"]', $els[2]);
$game['achievements_total'] = $p[0]->textContent;
} else {
$p = $this->basexpath->query('div/p', $els[2]);
$game['achievements_won'] = 0;
$game['achievements_total'] = $p[0]->textContent;
}
$el = $this->basexpath->query('div[@class="statistics"]/div[@class="wrapper"]/div[@class="statistic narrow"]/p[@class="percentage"]', $gamegroupname);
if ($el->length) {
$game['completion_perc'] = rtrim($el[0]->textContent, '%');
} else {
$game['completion_perc'] = 0;
}
printf('(%d, "%s", %d, %d, %d, %d, %d, %d, "%s", NOW()),' . PHP_EOL,
$game['id'],
$game['name'],
$game['completion_perc'],
$game['achievements_won'],
$game['achievements_total'],
$game['gamerscore_won'],
$game['gamerscore_total'],
$game['ta_score'],
$this->baseurl . $game['game_url']
);
}
}
private function fetchGamerId()
{
//$this->gamerid = 71675;
//return;
//this may seem skippable but we need the request header cookies for subsequent requests
$dom = new DOMDocument();
$dom->loadHTML($this->getUrl(sprintf($this->gamecollection, $this->gamertag)));
$xpath = new DOMXPath($dom);
$rsslink = $xpath->query('//link[@type="application/rss+xml"][2]');
$linktarget = $rsslink[0]->getAttribute('href');
if (preg_match('/gamerid=(.+)/', $linktarget, $matches)) {
$this->gamerid = $matches[1];
} else {
die(sprintf('Unable to find gamerid for gamertag %s.' . PHP_EOL, $this->gamertag));
}
}
private function fetchGameCollection()
{
if ($this->skipcache) {
@unlink('./xboxcalculator-cache-page*.html');
}
if (!is_file('./xboxcalculator-cache-page001.html')) {
print('Fetching list view..' . PHP_EOL);
for ($page = 1; $page < 100; $page++) {
print('.');
$html = $this->getUrl(sprintf($this->gamecollectionFullList,
$this->gamertag,
$page,
$this->gamerid,
$this->regions[$this->region]
));
if ($html && strpos($html, 'class="warningspanel"') === false) {
file_put_contents(sprintf('./xboxcalculator-cache-page%03d.html', $page), $html);
} else {
printf(PHP_EOL . 'Fetched last of %d pages.' . PHP_EOL, $page);
break;
}
}
} else {
$pagecount = count(glob('./xboxcalculator-cache-page*.html'));
printf('Using cached list view of %d pages..' . PHP_EOL, $pagecount);
}
//load html files into xml object array, because we can't combine DOMNodeList objects
$dom = new DOMDocument();
$gamecount = 0;
foreach (glob('./xboxcalculator-cache-page*.html') as $htmlfile) {
$dom->loadHTMLFile($htmlfile);
$basexpath = new DOMXPath($dom);
$this->basexpaths[] = $basexpath;
//get the number of games on this page for total count
$games = $basexpath->query('//tr[contains(@class, "green") or contains(@class, "even") or contains(@class, "odd")]');
$gamecount += $games->length;
}
printf('Done - fetched %s games.' . PHP_EOL, $gamecount);
}
private function fetchPrices()
{
$prices = [];
$pricecachefile = sprintf('./xboxcalculator-prices-%s.json', strtolower($this->region));
if (is_file($pricecachefile) && !$this->skipcache) {
print('Loading cached prices..' . PHP_EOL);
$prices = json_decode(file_get_contents($pricecachefile), true);
}
print('Fetching missing/outdated price info for games...' . PHP_EOL);
$totalworth = 0;
$highestpricedgame = ['price' => 0];
$freegamescount = 0;
$salecount = 0;
$totalsaved = 0;
$delistedcount = 0;
$unavailablecount = 0;
$i = 0;
foreach ($this->basexpaths as $basexpath) {
$games = $basexpath->query('//tr[contains(@class, "green") or contains(@class, "even") or contains(@class, "odd")]');
foreach ($games as $tablerow) {
$status = 'available';
$gameinfo = $this->parseBasicInfo($basexpath, $tablerow);
list($gameid, $title, $url) = $gameinfo;
if ($gameid > 0) {
$skipPriceCheck = false;
if (isset($prices[$gameid])) {
$lastTimestamp = $prices[$gameid]['timestamp'];
$price = $prices[$gameid]['price'];
//skip price check if price is not older than a week
if (new DateTime($lastTimestamp) > (new DateTime)->sub(new DateInterval('P7D')) && !$this->skipcache) {
$skipPriceCheck = true;
}
}
if (!$skipPriceCheck) {
$timestamp = (new DateTime)->format('Y-m-d H:i:s');
$priceInfo = $this->fetchPriceInfo($gameid, $title);
list($price, $status, $saleFrom) = $priceInfo;
if (is_numeric($price)) {
//check because price can also be 'Free'
$totalworth += $price;
}
} else {
//use cached data, don't modify entry
$price = $prices[$gameid]['price'];
$status = $prices[$gameid]['status'];
$saleFrom = $prices[$gameid]['saleFrom'];
$timestamp = $prices[$gameid]['timestamp'];
if (preg_match('/\d+[.,]\d+/', $price, $match)) {
$price = $match[0];
$totalworth += $price;
}
}
} else {
printf('- %s: Unable to find game id!' . PHP_EOL, $title);
$status = 'error';
$price = null;
}
$gamePrice = [
'id' => $gameid,
'name' => $title,
'url' => $url,
'price' => number_format((double) $price, 2),
'saleFrom' => ($status == 'sale' ? (double) $saleFrom : null),
'status' => $status,
'timestamp' => $timestamp,
];
$prices[$gameid] = $gamePrice;
if (is_numeric($price) && $price > $highestpricedgame['price']) {
$highestpricedgame = $gamePrice;
}
if (strtolower(trim($price)) == 'free') {
$freegamescount++;
}
switch ($status) {
default:
case 'available':
break;
case 'sale':
$salecount++;
$totalsaved += ($saleFrom - (is_numeric($price) ? $price : 0));
break;
case 'delisted':
$delistedcount++;
break;
case 'region-locked':
case 'unavailable':
case 'error':
$unavailablecount++;
break;
}
$i++;
if ($i > 10) {
//break;
}
}
}
echo PHP_EOL;
print('Done!' . PHP_EOL);
asort($prices);
file_put_contents($pricecachefile, json_encode($prices, JSON_PRETTY_PRINT));
$this->prices = $prices;
$this->summary = [
'totalworth' => $totalworth,
'highestpricedgame' => $highestpricedgame,
'freegamescount' => $freegamescount,
'salecount' => $salecount,
'totalsaved' => number_format((double) $totalsaved, 2),
'delistedcount' => $delistedcount,
'unavailablecount' => $unavailablecount,
];
}
/**
* parse some basic info from a game node on the game collection page
*/
private function parseBasicInfo($basexpath, $tablerow)
{
/*
* cells:
* 0 - thumb picture
* 1 - name + url (class smallgame)
* 2 - platform (game id in id attribute as tdPlatform_xxxx)
* 3 - TA unlocked/total (class score)
* 4 - GS unlocked/total (class score)
* 5 - achievements unlocked/total (class score)
* 6 - completion percentage
* 7 - my rating (class rating, nested div with class rating-[xxx]-stars)
* 8 - started date (class date)
* 9 - completed date (class date)
* 10 - last unlocked date (class date)
* 11 - ownership (class small)
* 12 - media (class small)
* 13 - play status (class small)
* 14 - site completion percentage
* 15 - completion estimate
* 16 - walkthrough link
* 17 - site ratio (class score)
* 18 - site rating (class rating)
* 19 - unobtainables
*/
$namelink = $basexpath->query('td[@class="smallgame"]/a', $tablerow);
$name = $namelink->item(0)->textContent;
$url = $namelink->item(0)->getAttribute('href');
$cells = $basexpath->query('td', $tablerow);
//2 - gameid + platform
$temp = $cells->item(2);
$gameid = 0;
if (preg_match('/_(\d+)$/', $temp->getAttribute('id'), $m)) {
$gameid = $m[1];
}
//return just this since the gamecollection csv will have the rest anyway and is way easier to parse
return [$gameid, $name, $url];
$temp = $basexpath->query('img', $temp);
$platform = $temp->item(0)->getAttribute('title');
//3 - trueachievements score unlocked + total
if (preg_match('/(.+) \/ (.+)/', $cells->item(3)->textContent, $m)) {
$ta_unlocked = str_replace(',', '', $m[1]);
$ta_total = str_replace(',', '', $m[2]);
}
//4 - gamerscore unlocked + total
if (preg_match('/(.+) \/ (.+)/', $cells->item(4)->textContent, $m)) {
$gs_unlocked = str_replace(',', '', $m[1]);
$gs_total = str_replace(',', '', $m[2]);
}
//5 - achievements unlocked + total
if (preg_match('/(.+) \/ (.+)/', $cells->item(5)->textContent, $m)) {
$ach_unlocked = $m[1];
$ach_total = $m[2];
}
//6 - completion percentage
$completion_perc = $cells->item(6)->textContent;
//7 - my rating
$my_rating = 0;
$temp = $basexpath->query('div', $cells->item(7));
if (preg_match('/rating-([0-9-]+)-stars/', $temp->item(0)->getAttribute('class'), $m)) {
$my_rating = str_replace('-', '.', $m[1]);
}
//TODO convert these to proper dates instead of '23 Dec 17'
//8 - started date
$date_started = $cells->item(8)->textContent;
//9 - completion date
$date_completed = $cells->item(9)->textContent;
//10 - last unlock date
$date_lastunlock = $cells->item(10)->textContent;
//11 - ownership status
$ownership = $cells->item(11)->textContent;
//12 - media
$media = $cells->item(12)->textContent;
//13 - play status
$play_status = $cells->item(13)->textContent;
//14 - site completion percentage
$site_completion_perc = $cells->item(14)->textContent;
//15 - completion estimate
$completion_estimate = $cells->item(15)->textContent;
//16 - walkthrough url
$temp = $basexpath->query('a', $cells->item(16));
$walkthrough = $temp->item(0)->getAttribute('href');
//17 - site ratio
$site_ratio = $cells->item(17)->textContent;
//18 - site rating
$site_rating = 0;
$temp = $basexpath->query('div', $cells->item(18));
if (preg_match('/rating-([0-9-]+)-stars/', $temp->item(0)->getAttribute('class'), $m)) {
$site_rating = str_replace('-', '.', $m[1]);
}
//19 - unobtainables (format?)
$unobtainable_count = $cells->item(19)->textContent;
$return = [
'id' => $gameid,
'name' => $name,
'url' => $url,
'platform' => $platform,
'tascore_unlocked' => $ta_unlocked,
'tascore_total' => $ta_total,
'gamerscore_unlocked' => $gs_unlocked,
'gamerscore_total' => $gs_total,
'achievements_unlocked' => $ach_unlocked,
'achievements_total' => $ach_total,
'completion_percentage' => $completion_perc,
'my_rating' => $my_rating,
'date_started' => $date_started,
'date_completed' => $date_completed,
'date_lastunlock' => $date_lastunlock,
'ownership' => $ownership,
'media' => $media,
'play_status' => $play_status,
'site_completion_perc' => $site_completion_perc,
'completion_estimate' => $completion_estimate,
'walkthrough' => $walkthrough,
'site_ratio' => $site_ratio,
'site_rating' => $site_rating,
'unobtainable_count' => $unobtainable_count,
];
var_dump($return);
die('stop');
/*extract title and url from item
$h3 = $this->basexpath->query('h3/a', $gamegroupname);
$title = $h3->item(0)->textContent;
$url = ltrim($h3->item(0)->getAttribute('href'), '/');
//use boxart img to get game id
$img = $this->basexpath->query('div/img[@class="boxart"]', $gamegroupname);
$src = $img->item(0)->getAttribute('src');
$gameid = 0;
if (preg_match('/Game_(.+?)\.png/', $src, $match)) {
$gameid = $match[1];
} else {
//check hardcoded game ids
if (isset($this->missinggameids[$title])) {
$gameid = $this->missinggameids[$title];
}
}*/
return [
$gameid,
$title,
$url,
];
}
/**
* forge an ajax request to fetch the price info on a game
* normally this information is fetched on the game page in a box
* on the right-hand side of the page
*/
private function fetchPriceInfo($gameid, $title)
{
//use game id to craft fake ajax request to get price info
$json = ['PurchaseOptionsRequest' => [
'GameID' => $gameid,
'RegionID' => $this->regions[$this->region],
'PageType' => 'Game',
'GamePropertyID' => 0,
'MobileVersion' => false,
'ResponsiveVersion' => true,
]];
$curl = curl_init($this->pricepage);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($json),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json; charset=UTF-8',
'Content-Length: ' . strlen(json_encode($json)),
],
//we fetched the session cookie after the first curl request
CURLOPT_COOKIE => implode(' ', $this->cookies),
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36 OPR/51.0.2830.55',
]);
$result = curl_exec($curl);
usleep(100000);
$price = false;
$saleFrom = false;
if ($result) {
//extract payload
$result = json_decode($result);
if (!empty($result->d)) {
//extract html from payload
$priceinfo = $result->d;
$dom = new DOMDocument;
$dom->loadHTML($priceinfo);
//extract price from html
$xpath = new DOMXPath($dom);
$priceNode = $xpath->query('//div[contains(@class, "price")]/span');
if ($priceNode->length) {
//multiple prices may be listed - get the lowest one
foreach ($priceNode as $item) {
//extract numerical value from price
if (preg_match('/\d+[.,]\d+/', $item->textContent, $match)) {
$price = (!$price || $match[0] < $price) ? $match[0] : $price;
} elseif (strtolower($item->textContent) == 'free') {
$price = 'Free';
break;
}
}
//check for 'strikethrough' price, meaning the item is on sale
$salePriceNode = $xpath->query('//div[contains(@class, "price")]/span[contains(@class, "strk")]');
if ($salePriceNode->length) {
foreach ($salePriceNode as $item) {
if (preg_match('/\d+[.,]\d+/', $item->textContent, $match)) {
$saleFrom = (!$saleFrom || $match[0] < $saleFrom) ? $match[0] : $saleFrom;
}
}
$status = 'sale';
printf('- %s: %s (on sale from %s)' . PHP_EOL, $title, $price, $saleFrom);
} else {
$status = 'available';
printf('- %s: %s' . PHP_EOL, $title, $price);
}
} else {
//this usually means 'no pricing available for your region'
$status = 'region-locked';
printf('- %s: %s' . PHP_EOL, $title, '(No pricing available in this region)');
}
} else {
//either free or delisted
$status = 'delisted';
printf('- %s: %s' . PHP_EOL, $title, '(Delisted)');
}
} else {
$status = 'unavailable';
printf('- %s: ERROR - query response blank' . PHP_EOL, $title);
}
return [
$price,
$status,
$saleFrom,
];
}
private function printSummary()
{
printf('Done! Your game collection is currently worth %s in %s' . PHP_EOL, $this->summary['totalworth'], $this->region);
printf('- Your most expensive game is %s costing %s' . PHP_EOL,
$this->summary['highestpricedgame']['name'],
$this->summary['highestpricedgame']['price']
);
printf('- %d of your games are currently on sale' . PHP_EOL, $this->summary['salecount']);
printf('- %d of your games are currently free' . PHP_EOL, $this->summary['freegamescount']);
printf('- %d of your games have been delisted, %d are unavailable in the Xbox store' . PHP_EOL,
$this->summary['delistedcount'],
$this->summary['unavailablecount']
);
if ($this->summary['totalsaved'] > 0) {
printf('- If you re-bought all your games at current prices, you would save %s due to current sales', $this->summary['totalsaved']);
}
}
private function getUrl($url)
{
if (empty($this->curl)) {
$this->curl = curl_init($url);
} else {
curl_setopt($this->curl, CURLOPT_URL, $url);
}
curl_setopt_array($this->curl, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
//CURLOPT_HEADER => true,
//CURLOPT_HTTPHEADER = > [],
//CURLOPT_COOKIE => '?',
//CURLOPT_USERAGENT => '?',
]);
if (empty($this->cookies)) {
$cookies = [];
curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, function ($ch, $line) use (&$cookies) {
if (preg_match('/^Set-Cookie:\s*([^;]*)/mi', $line, $cookie)) {
printf('Got session cookie with id %s' . PHP_EOL, $cookie[1]);
$cookies[] = $cookie[1];
}
return strlen($line);
});
}
$result = curl_exec($this->curl);
if (empty($this->cookies)) {
$this->cookies = $cookies;
}
return $result;
}
}