762e1cef8736b3210a55c669f54d1cd04fbb4e49
[ouya-game-data.git] / bin / convert-original.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * Convert OUYA storefront data to a game json file
5  *
6  * @author Christian Weiske <cweiske@cweiske.de>
7  */
8 if (isset($argv[1]) && in_array($argv[1], ['-h', '--help'])) {
9     error(
10         'Usage: convert-original.php ouya-game-details.json ouya-game-download.json'
11     );
12 }
13
14 //cli arguments
15 if (!isset($argv[1])) {
16     error('details json file parameter missing');
17 }
18 $detailsFile = $argv[1];
19
20 if (!isset($argv[2])) {
21     error('download json file parameter missing');
22 }
23 $downloadFile = $argv[2];
24
25
26 //file loading
27 $detailsJson = file_get_contents($detailsFile);
28 if ($detailsJson === false || trim($detailsJson) === '') {
29     error('Details file is empty');
30 }
31 $detailsData = json_decode($detailsJson);
32 if ($detailsData === null) {
33     error('Details JSON cannot de loaded');
34 }
35
36 $downloadJson = file_get_contents($downloadFile);
37 if ($downloadJson === false || trim($downloadJson) === '') {
38     error('Download file is empty');
39 }
40 $downloadData = json_decode($downloadJson);
41 if ($downloadData === null) {
42     error('Download JSON cannot de loaded');
43 }
44
45 $package = basename($detailsFile, '.json');
46 //data building
47 $gameData = [
48     'uuid'        => $detailsData->app->uuid,
49     'package'     => $package,
50     'title'       => $detailsData->app->title,
51     'description' => $detailsData->app->description,
52     'players'     => $detailsData->app->gamerNumbers,
53     'genres'      => $detailsData->app->genres,
54     
55     'releases' => [
56         [
57             'name'       => $detailsData->app->versionNumber,
58             'uuid'       => $detailsData->app->latestVersion,
59             'date'       => $detailsData->app->publishedAt,
60             'url'        => $downloadData->app->downloadLink,
61             'size'       => $downloadData->app->fileSize,
62             'md5sum'     => $detailsData->app->md5sum,
63             'publicSize' => $detailsData->app->publicSize,
64             'nativeSize' => $detailsData->app->nativeSize,
65         ]
66     ],
67
68     'media' => [
69         'discover'    => 'http://ouya.cweiske.de/game-images/' . strtolower($package) . '/discover',
70         'large'       => newImage($detailsData->app->mainImageFullUrl),
71         'video'       => $detailsData->app->videoUrl,
72         'screenshots' => newImages($detailsData->app->filepickerScreenshots),
73     ],
74
75     'developer' => [
76         'name'         => $detailsData->app->developer,
77         'supportEmail' => $detailsData->app->supportEmailAddress,
78         'supportPhone' => $detailsData->app->supportPhone,
79         'founder'      => $detailsData->app->founder,
80     ],
81
82     'contentRating'    => $detailsData->app->contentRating,
83     'website'          => $detailsData->app->website,
84     'firstPublishedAt' => $detailsData->app->firstPublishedAt,
85     'inAppPurchases'   => false,//FIXME: we would need discover data here
86     'overview'         => $detailsData->app->overview,
87     'premium'          => $detailsData->app->premium,
88
89     'rating' => [
90         'likeCount' => $detailsData->app->likeCount,
91         'average'   => $detailsData->app->ratingAverage,
92         'count'     => $detailsData->app->ratingCount,
93     ],
94 ];
95
96 if (isset($detailsData->app->promotedProduct)) {
97     $gameData['products'][] = [
98         'promoted'      => true,
99         'identifier'    => $detailsData->app->promotedProduct->identifier,
100         'name'          => $detailsData->app->promotedProduct->name,
101         'description'   => $detailsData->app->promotedProduct->description,
102         'localPrice'    => $detailsData->app->promotedProduct->localPrice,
103         'originalPrice' => $detailsData->app->promotedProduct->originalPrice,
104         'currency'      => $detailsData->app->promotedProduct->currency,
105     ];
106 }
107
108 $iaDataFiles = glob(__DIR__ . '/../old-data/ia-data/ouya_' . $package . '_*.json');
109 if (count($iaDataFiles)) {
110     $iaPkg = [];
111     foreach ($iaDataFiles as $iaJsonFile) {
112         $iaData = json_decode(file_get_contents($iaJsonFile));
113         foreach ($iaData->files as $iaFile) {
114             if ($iaFile->format == 'Android Package Archive') {
115                 $iaSlug = basename($iaJsonFile, '.json');
116                 $iaFile->url = 'https://archive.org/download/' . $iaSlug . '/' . $iaFile->name;
117                 $versionName = explode('_', $iaSlug)[2];
118                 $iaPkg[$versionName] = $iaFile;
119             }
120         }
121     }
122
123     //update existing release
124     $exVersion = $gameData['releases'][0]['name'];
125     if (isset($iaPkg[$exVersion])) {
126         $gameData['releases'][0]['url'] = $iaPkg[$exVersion]->url;
127         unset($iaPkg[$exVersion]);
128     }
129     foreach ($iaPkg as $iaVersion => $iaApk) {
130         $gameData['releases'][] = [
131             'name'       => $iaVersion,
132             'uuid'       => null,
133             'date'       => '2010-01-01T00:00:00Z',//gmdate('c', $iaApk->mtime),
134             'url'        => $iaApk->url,
135             'size'       => $iaApk->size,
136             'md5sum'     => $iaApk->md5,
137             'publicSize' => 0,//FIXME
138             'nativeSize' => 0,//FIXME
139         ];
140     }
141     var_dump($iaPkg, $gameData);die();
142 }
143
144
145 echo json_encode($gameData, JSON_PRETTY_PRINT) . "\n";
146
147 function newImage($imageUrl)
148 {
149     return $imageUrl;
150     static $mapping;
151     if ($mapping === null) {
152         $hdl = fopen(__DIR__ . '/../old-data/map-game-images.csv', 'r');
153         if (!$hdl) {
154             error('Cannot load image url map file');
155         }
156         $mapping = [];
157         while ($data = fgetcsv($hdl, 4096, ',')) {
158             if (count($data) == 2) {
159                 $mapping[$data[0]] = $data[1];
160             }
161         }
162     }
163     return $mapping[$imageUrl] ?? $imageUrl;
164 }
165
166 function newImages($urls)
167 {
168     $new = [];
169     foreach ($urls as $url) {
170         $new[] = newImage($url);
171     }
172     return $new;
173 }
174
175 function error($msg)
176 {
177     file_put_contents('php://stderr', $msg . "\n");
178     exit(1);
179 }
180 ?>