Remove "large" from scripts
[ouya-game-data.git] / bin / convert-internetarchive-apk-urls.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * Take OUYA game data files and replace the broken download links
5  * with archive.org links
6  */
7 foreach (glob(__DIR__ . '/../games/*.json') as $gameFile) {
8     $gameData = json_decode(file_get_contents($gameFile));
9     echo $gameFile . "\n";
10     $changed = false;
11     foreach ($gameData->releases as $release) {
12         if (parse_url($release->url, PHP_URL_HOST) != 'devs-ouya-tv-prod.s3.amazonaws.com') {
13             echo " release url ok\n";
14             continue;
15         }
16
17         $releaseNameFile = str_replace(
18             [' ', '(', ')', '!'],
19             ['_', '', '', ''],
20             $release->name
21         );
22         $iaJsonFile = __DIR__ . '/../old-data/ia-data/'
23             . 'ouya_' . $gameData->packageName . '_' . $releaseNameFile . '.json';
24         if (!file_exists($iaJsonFile)) {
25             echo " IA file not found: $iaJsonFile\n";
26             continue;
27         }
28         $iaData = json_decode(file_get_contents($iaJsonFile));
29
30         $url = null;
31         foreach ($iaData->files as $iaFile) {
32             if ($iaFile->format == 'Android Package Archive') {
33                 $iaSlug = basename($iaJsonFile, '.json');
34                 $url = 'https://archive.org/download/' . $iaSlug . '/' . rawurlencode($iaFile->name);
35             }
36         }
37         if ($url === null) {
38             echo " No apk found!\n";
39             continue;
40         }
41
42         $release->url = $url;
43         $changed = true;
44     }
45
46     if ($changed) {
47         echo " Saving game data\n";
48         file_put_contents($gameFile, json_encode($gameData, JSON_PRETTY_PRINT) . "\n");
49     }
50 }