Make game data converter more resilient. Do not require downloads file and fall back...
[ouya-game-data.git] / bin / copy-game-images.php
1 <?php
2 $gamefiles = glob('devs.ouya.tv/api/v1/apps/*.json');
3
4 foreach ($gamefiles as $file) {
5     echo "Processing $file\n";
6     $data = json_decode(file_get_contents($file));
7     if ($data === null) {
8         echo "error opening " . $file . "\n";
9         exit(1);
10     }
11     $package = strtolower(basename($file, '.json'));
12
13     $dir = 'game-images/' . $package . '/';
14     if (!is_dir($dir)) {
15         mkdir($dir, 0777, true);
16     }
17
18     $pos = 0;
19     foreach ($data->app->filepickerScreenshots as $imageUrl) {
20         $pos++;
21         copyImageUrl($imageUrl, $dir . 'screenshot-' . $pos);
22     }
23
24     copyImageUrl($data->app->mainImageFullUrl, $dir . 'main');
25     echo "\n";
26 }
27
28 function copyImageUrl($imageUrl, $newPath)
29 {
30     $imageLocal = getLocalPath($imageUrl);
31     if (!file_exists($imageLocal)) {
32         echo "Local file not found: $imageLocal\n";
33         return;
34     }
35     if (file_exists($newPath)) {
36         echo "S";
37         return;
38     }
39     
40     echo ".";
41     copy($imageLocal, $newPath);
42     file_put_contents(
43         'map-game-images.csv',
44         $imageUrl . ','
45         . 'http://ouya.cweiske.de/' . $newPath
46         . "\n",
47         FILE_APPEND
48     );
49 }
50
51 function getLocalPath($imageUrl)
52 {
53     return str_replace('https://', '', $imageUrl);
54 }
55 ?>