Fix wrongly imported inAppPurchases properties
[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 $package = basename($detailsFile, '.json');
51
52 if (file_exists($downloadFile)) {
53     $downloadJson = file_get_contents($downloadFile);
54     if ($downloadJson === false || trim($downloadJson) === '') {
55         error('Download file is empty');
56     }
57     $downloadData = json_decode($downloadJson);
58     if ($downloadData === null) {
59         error('Download JSON cannot de loaded');
60     }
61     $downloadUrl = $downloadData->app->downloadLink;
62 } else {
63     $downloadData = null;
64     $downloadUrl  = null;
65     //fetch download URL from internet archive files
66     $version = $appsData->app->versionNumber;
67     $iaJsonFile = __DIR__ . '/../old-data/ia-data/'
68         . 'ouya_' . $package . '_' . $version . '.json';
69     if (!file_exists($iaJsonFile)) {
70         error('No download file given, and no internet archive version found');
71     }
72     $iaData = json_decode(file_get_contents($iaJsonFile));
73     foreach ($iaData->files as $iaFile) {
74         if ($iaFile->format == 'Android Package Archive') {
75             $iaSlug = basename($iaJsonFile, '.json');
76             $downloadUrl = 'https://archive.org/download/' . $iaSlug . '/' . $iaFile->name;
77         }
78     }
79     if ($downloadUrl === null) {
80         error('No .apk download URL found in internet archive json file');
81     }
82 }
83
84
85 //data building
86
87 $developerUuid = null;
88 if (isset($detailsData->developer->url)) {
89     parse_str(parse_url($detailsData->developer->url, PHP_URL_QUERY), $devParams);
90     $developerUuid = $devParams['developer'];
91 }
92
93 $gameData = [
94     'packageName' => $package,
95     'title'       => $appsData->app->title,
96     'description' => $appsData->app->description,
97     'players'     => $appsData->app->gamerNumbers,
98     'genres'      => $appsData->app->genres,
99     
100     'releases' => [
101         [
102             'name'        => $appsData->app->versionNumber,
103             'versionCode' => (int) $detailsData->apk->versionCode,
104             'uuid'        => $appsData->app->latestVersion,
105             'date'        => $appsData->app->publishedAt,
106             'url'         => $downloadUrl,
107             'size'        => isset($downloadData->app->fileSize)
108                 ? intval($downloadData->app->fileSize)
109                 : intval($appsData->app->apkFileSize),
110             'md5sum'      => $appsData->app->md5sum,
111             'publicSize'  => $appsData->app->publicSize,
112             'nativeSize'  => $appsData->app->nativeSize,
113         ]
114     ],
115
116     'media' => [
117         'discover'    => 'http://ouya.cweiske.de/game-images/' . strtolower($package) . '/discover',
118         'large'       => $appsData->app->mainImageFullUrl,
119         'video'       => $appsData->app->videoUrl,
120         'screenshots' => $appsData->app->filepickerScreenshots,
121         'details'     => details($detailsData->mediaTiles),
122     ],
123
124     'developer' => [
125         'uuid'         => $developerUuid,
126         'name'         => $appsData->app->developer,
127         'supportEmail' => $appsData->app->supportEmailAddress != ''
128             ? $appsData->app->supportEmailAddress : null,
129         'supportPhone' => $appsData->app->supportPhone,
130         'founder'      => $appsData->app->founder,
131     ],
132
133     'contentRating'    => $appsData->app->contentRating,
134     'website'          => $appsData->app->website,
135     'firstPublishedAt' => $appsData->app->firstPublishedAt,
136     'inAppPurchases'   => false,//FIXME: we would need discover data here
137     'overview'         => $appsData->app->overview,
138     'premium'          => $appsData->app->premium,
139
140     'rating' => [
141         'likeCount' => $appsData->app->likeCount,
142         'average'   => $appsData->app->ratingAverage,
143         'count'     => $appsData->app->ratingCount,
144     ],
145 ];
146
147 if (isset($appsData->app->promotedProduct)) {
148     $gameData['products'][] = [
149         'promoted'      => true,
150         'identifier'    => $appsData->app->promotedProduct->identifier,
151         'name'          => $appsData->app->promotedProduct->name,
152         'description'   => $appsData->app->promotedProduct->description,
153         'localPrice'    => $appsData->app->promotedProduct->localPrice,
154         'originalPrice' => $appsData->app->promotedProduct->originalPrice,
155         'currency'      => $appsData->app->promotedProduct->currency,
156     ];
157 }
158
159 echo json_encode($gameData, JSON_PRETTY_PRINT) . "\n";
160
161 function details($mediaTiles)
162 {
163     $details = [];
164     foreach ($mediaTiles as $tile) {
165         if ($tile->type == 'video') {
166             $details[] = [
167                 'type' => 'video',
168                 'url'  => $tile->url,
169             ];
170         } else {
171             $details[] = [
172                 'type'  => 'image',
173                 'url'   => $tile->urls->full,
174                 'thumb' => $tile->urls->thumbnail,
175             ];
176         }
177     }
178     return $details;
179 }
180
181 function error($msg)
182 {
183     file_put_contents('php://stderr', $msg . "\n");
184     exit(1);
185 }
186 ?>