final fix to original conversion script
[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 game-details.json game-apps.json game-apps-download.json'
11     );
12 }
13
14 //cli arguments
15 if (!isset($argv[1])) {
16     error('details json file parameter missing (api/v1/details?app=...');
17 }
18 $detailsFile = $argv[1];
19
20 if (!isset($argv[2])) {
21     error('apps json file parameter missing (api/v1/apps/xxx');
22 }
23 $appsFile = $argv[2];
24
25 if (!isset($argv[3])) {
26     error('apps download json file parameter missing (api/v1/apps/xxx/download');
27 }
28 $downloadFile = $argv[3];
29
30
31 //file loading
32 $detailsJson = file_get_contents($detailsFile);
33 if ($detailsJson === false || trim($detailsJson) === '') {
34     error('Details file is empty');
35 }
36 $detailsData = json_decode($detailsJson);
37 if ($detailsData === null) {
38     error('Details JSON cannot de loaded');
39 }
40
41 $appsJson = file_get_contents($appsFile);
42 if ($appsJson === false || trim($appsJson) === '') {
43     error('Apps file is empty');
44 }
45 $appsData = json_decode($appsJson);
46 if ($appsData === null) {
47     error('App JSON cannot de loaded');
48 }
49
50 $downloadJson = file_get_contents($downloadFile);
51 if ($downloadJson === false || trim($downloadJson) === '') {
52     error('Download file is empty');
53 }
54 $downloadData = json_decode($downloadJson);
55 if ($downloadData === null) {
56     error('Download JSON cannot de loaded');
57 }
58
59
60 //data building
61 $package = basename($detailsFile, '.json');
62
63 $developerUuid = null;
64 if (isset($detailsData->developer->url)) {
65     parse_str(parse_url($detailsData->developer->url, PHP_URL_QUERY), $devParams);
66     $developerUuid = $devParams['developer'];
67 }
68
69 $gameData = [
70     'packageName' => $package,
71     'title'       => $appsData->app->title,
72     'description' => $appsData->app->description,
73     'players'     => $appsData->app->gamerNumbers,
74     'genres'      => $appsData->app->genres,
75     
76     'releases' => [
77         [
78             'name'        => $appsData->app->versionNumber,
79             'versionCode' => (int) $detailsData->apk->versionCode,
80             'uuid'        => $appsData->app->latestVersion,
81             'date'        => $appsData->app->publishedAt,
82             'url'         => $downloadData->app->downloadLink,
83             'size'        => (int) $downloadData->app->fileSize,
84             'md5sum'      => $appsData->app->md5sum,
85             'publicSize'  => $appsData->app->publicSize,
86             'nativeSize'  => $appsData->app->nativeSize,
87         ]
88     ],
89
90     'media' => [
91         'discover'    => 'http://ouya.cweiske.de/game-images/' . strtolower($package) . '/discover',
92         'large'       => $appsData->app->mainImageFullUrl,
93         'video'       => $appsData->app->videoUrl,
94         'screenshots' => $appsData->app->filepickerScreenshots,
95         'details'     => details($detailsData->mediaTiles),
96     ],
97
98     'developer' => [
99         'uuid'         => $developerUuid,
100         'name'         => $appsData->app->developer,
101         'supportEmail' => $appsData->app->supportEmailAddress,
102         'supportPhone' => $appsData->app->supportPhone,
103         'founder'      => $appsData->app->founder,
104     ],
105
106     'contentRating'    => $appsData->app->contentRating,
107     'website'          => $appsData->app->website,
108     'firstPublishedAt' => $appsData->app->firstPublishedAt,
109     'inAppPurchases'   => false,//FIXME: we would need discover data here
110     'overview'         => $appsData->app->overview,
111     'premium'          => $appsData->app->premium,
112
113     'rating' => [
114         'likeCount' => $appsData->app->likeCount,
115         'average'   => $appsData->app->ratingAverage,
116         'count'     => $appsData->app->ratingCount,
117     ],
118 ];
119
120 if (isset($appsData->app->promotedProduct)) {
121     $gameData['products'][] = [
122         'promoted'      => true,
123         'identifier'    => $appsData->app->promotedProduct->identifier,
124         'name'          => $appsData->app->promotedProduct->name,
125         'description'   => $appsData->app->promotedProduct->description,
126         'localPrice'    => $appsData->app->promotedProduct->localPrice,
127         'originalPrice' => $appsData->app->promotedProduct->originalPrice,
128         'currency'      => $appsData->app->promotedProduct->currency,
129     ];
130 }
131
132 echo json_encode($gameData, JSON_PRETTY_PRINT) . "\n";
133
134 function details($mediaTiles)
135 {
136     $details = [];
137     foreach ($mediaTiles as $tile) {
138         if ($tile->type == 'video') {
139             $details[] = [
140                 'type' => 'video',
141                 'url'  => $tile->url,
142             ];
143         } else {
144             $details[] = [
145                 'type'  => 'image',
146                 'url'   => $tile->urls->full,
147                 'thumb' => $tile->urls->thumbnail,
148             ];
149         }
150     }
151     return $details;
152 }
153
154 function error($msg)
155 {
156     file_put_contents('php://stderr', $msg . "\n");
157     exit(1);
158 }
159 ?>