Replace some missing screenshots and detail images with image from blackcharcz's...
[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         'video'       => $appsData->app->videoUrl,
119         'screenshots' => $appsData->app->filepickerScreenshots,
120         'details'     => details($detailsData->mediaTiles),
121     ],
122
123     'developer' => [
124         'uuid'         => $developerUuid,
125         'name'         => $appsData->app->developer,
126         'supportEmail' => $appsData->app->supportEmailAddress != ''
127             ? $appsData->app->supportEmailAddress : null,
128         'supportPhone' => $appsData->app->supportPhone,
129         'founder'      => $appsData->app->founder,
130     ],
131
132     'contentRating'    => $appsData->app->contentRating,
133     'website'          => $appsData->app->website,
134     'firstPublishedAt' => $appsData->app->firstPublishedAt,
135     'inAppPurchases'   => $detailsData->inAppPurchases,
136     'overview'         => $appsData->app->overview,
137     'premium'          => $appsData->app->premium,
138
139     'rating' => [
140         'likeCount' => $appsData->app->likeCount,
141         'average'   => $appsData->app->ratingAverage,
142         'count'     => $appsData->app->ratingCount,
143     ],
144 ];
145
146 if (isset($appsData->app->promotedProduct)) {
147     $gameData['products'][] = [
148         'promoted'      => true,
149         'identifier'    => $appsData->app->promotedProduct->identifier,
150         'name'          => $appsData->app->promotedProduct->name,
151         'description'   => $appsData->app->promotedProduct->description,
152         'localPrice'    => $appsData->app->promotedProduct->localPrice,
153         'originalPrice' => $appsData->app->promotedProduct->originalPrice,
154         'currency'      => $appsData->app->promotedProduct->currency,
155     ];
156 }
157
158 echo json_encode($gameData, JSON_PRETTY_PRINT) . "\n";
159
160 function details($mediaTiles)
161 {
162     $details = [];
163     foreach ($mediaTiles as $tile) {
164         if ($tile->type == 'video') {
165             $details[] = [
166                 'type' => 'video',
167                 'url'  => $tile->url,
168             ];
169         } else {
170             $details[] = [
171                 'type'  => 'image',
172                 'url'   => $tile->urls->full,
173                 'thumb' => $tile->urls->thumbnail,
174             ];
175         }
176     }
177     return $details;
178 }
179
180 function error($msg)
181 {
182     file_put_contents('php://stderr', $msg . "\n");
183     exit(1);
184 }
185 ?>