Allow returning null when loading game
[gamestick-pjgsapi.git] / bin / extract-popular.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * List all package names from a reg_server_response.json that are "popular"
5  */
6 if ($argc <= 1) {
7     fwrite(STDERR, "reg_server_response.json file missing\n");
8     exit(1);
9 }
10
11 $regFile = $argv[1];
12 if (!file_exists($regFile)) {
13     fwrite(STDERR, "json file does not exist: $regFile\n");
14     exit(2);
15 }
16 if (!is_readable($regFile)) {
17     fwrite(STDERR, "Cannot read json file: $regFile\n");
18     exit(2);
19 }
20
21 $regData = json_decode(file_get_contents($regFile));
22 if ($regData === null) {
23     fwrite(STDERR, "Cannot parse JSON data\n");
24     fwrite(STDERR, json_last_error_msg() . "\n");
25     exit(10);
26 }
27
28 if (!isset($regData->body->config->apps)) {
29     fwrite(STDERR, "File contains no apps\n");
30     exit(11);
31 }
32
33 $popular = [];
34 foreach ($regData->body->config->apps as $app) {
35     if ($app->popular > 0) {
36         $popular[$app->package] = $app->popular;
37     }
38 }
39
40 asort($popular);
41 echo implode("\n", array_flip($popular)) . "\n";