Allow returning null when loading game
[gamestick-pjgsapi.git] / bin / extract-featured.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * List all package names from a reg_server_response.json that are featured
5  * in the menu.
6  */
7 if ($argc <= 1) {
8     fwrite(STDERR, "reg_server_response.json file missing\n");
9     exit(1);
10 }
11
12 $regFile = $argv[1];
13 if (!file_exists($regFile)) {
14     fwrite(STDERR, "json file does not exist: $regFile\n");
15     exit(2);
16 }
17 if (!is_readable($regFile)) {
18     fwrite(STDERR, "Cannot read json file: $regFile\n");
19     exit(2);
20 }
21
22 $regData = json_decode(file_get_contents($regFile));
23 if ($regData === null) {
24     fwrite(STDERR, "Cannot parse JSON data\n");
25     fwrite(STDERR, json_last_error_msg() . "\n");
26     exit(10);
27 }
28
29 if (!isset($regData->body->config->apps)) {
30     fwrite(STDERR, "File contains no apps\n");
31     exit(11);
32 }
33
34 $games = [];
35 foreach ($regData->body->config->apps as $app) {
36     $games[$app->id] = $app->package;
37 }
38
39
40 $featured = [];
41 foreach ($regData->body->config->global->newfeatured->ages as $ageData) {
42     $age = $ageData->age;
43     foreach ($ageData->entries as $columnData) {
44         foreach ($columnData->columnentries as $rowData) {
45             if (!isset($games[$rowData->gameID])) {
46                 continue;
47             }
48             $package = $games[$rowData->gameID];
49             $featured[$age][$package] = $rowData->thumbnail;
50         }
51     }
52 }
53
54 ksort($featured, SORT_NUMERIC);
55
56 echo json_encode(
57     $featured,
58     JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
59 ) . "\n";