scripts to generate game files and internet archive import
[ouya-game-data.git] / bin / find-ia-missing.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * Find ouya game images missing in the internet archive
5  */
6 chdir(__DIR__ . '/../old-data/');
7
8 $iaDataFiles = glob('ia-data/ouya_*_*.json');
9 foreach ($iaDataFiles as $iaJsonFile) {
10     $iaPackage = basename($iaJsonFile, '.json');
11     $parts = explode('_', $iaPackage);
12     if (count($parts) != 3) {
13         continue;
14     }
15     $package = $parts[1];
16     fwrite(STDERR, $package . "\n");
17
18     $iaImages = loadIaImages($iaJsonFile);
19
20     $ouyaDetailsFile = 'devs.ouya.tv/api/v1/apps/' . $package . '.json';
21     if (!file_exists($ouyaDetailsFile)) {
22         fwrite(STDERR, " ERR: ouya file missing $ouyaDetailsFile\n");
23         continue;
24     }
25     $details = json_decode(file_get_contents($ouyaDetailsFile));
26     if ($details === null) {
27         fwrite(STDERR, "error opening " . $ouyaDetailsFile . "\n");
28         exit(1);
29     }
30
31     $pos = 0;
32     foreach ($details->app->filepickerScreenshots as $imageUrl) {
33         $pos++;
34         findIaImage($iaImages, $imageUrl, $iaPackage);
35     }
36
37     findIaImage($iaImages, $details->app->mainImageFullUrl, $iaPackage);
38 }
39
40
41
42 function findIaImage($iaImages, $imageUrl, $iaPackage)
43 {
44     // https://d3e4aumcqn8cw3.cloudfront.net/api/file/tC4RIGJLQvG2uG1av9jN
45     $imageName = basename($imageUrl);
46     if (isset($iaImages[$imageName . '.png'])) {
47         return;
48     }
49     if (isset($iaImages[$imageName . '.jpg'])) {
50         return;
51     }
52
53     $cwUrl = str_replace('https://', 'http://tmp.cweiske.de/', $imageUrl);
54
55     $localPath = str_replace('https://', '/media/cweiske/videos/ouya-backup/', $imageUrl);
56     if (!file_exists($localPath)) {
57         fwrite(STDERR, " local file not found: $localPath\n");
58     }
59
60     echo $iaPackage . ',' . $cwUrl . ',' . $imageUrl . "\n";
61 }
62
63 function loadIaImages($iaJsonFile)
64 {
65     $iaSlug = basename($iaJsonFile, '.json');
66     $data = json_decode(file_get_contents($iaJsonFile));
67     $images = [];
68     foreach ($data->files as $file) {
69         if ($file->source != 'original') {
70             continue;
71         }
72         if ($file->format != 'JPEG' && $file->format != 'PNG') {
73             continue;
74         }
75
76         $images[$file->name] = 'https://archive.org/download/' . $iaSlug . '/' . $file->name;
77     }
78     return $images;
79 }
80
81 exit(0);
82
83 $gamefiles = glob('devs.ouya.tv/api/v1/apps/*.json');
84 //$gamefiles = glob('devs.ouya.tv/api/v1/apps/net.froem*.json');
85
86 foreach ($gamefiles as $file) {
87     echo "Processing $file\n";
88     //die();
89 }
90
91 function mapIaImage($iaImages, $imageUrl)
92 {
93     $newUrl = findIaImage($iaImages, $imageUrl);
94     if ($newUrl !== $imageUrl) {
95         echo " ok\n";
96         file_put_contents(
97             'map-game-images.ia.csv',
98             $imageUrl . ',' . $newUrl . "\n",
99             FILE_APPEND
100         );
101         return;
102     }
103     //not in internet archive
104     echo " Missing in IA: $imageUrl\n";
105 }
106 ?>