4 * Import games from a OUYA game data repository
6 * @link https://github.com/cweiske/ouya-game-data/
7 * @author Christian Weiske <cweiske@cweiske.de>
9 if (!isset($argv[1])) {
10 error('Pass the path to a directory with game data json files');
12 $gameDataDir = $argv[1];
13 if (!is_dir($gameDataDir)) {
14 error('Given path is not a directory: ' . $gameDataDir);
17 $wwwDir = __DIR__ . '/../www/';
19 $gameFiles = glob($gameDataDir . '/*.json');
21 foreach ($gameFiles as $gameFile) {
22 $game = json_decode(file_get_contents($gameFile));
24 error('JSON invalid at ' . $gameFile);
26 addMissingGameProperties($game);
27 //FIXME: add missing properties?
28 $games[$game->package] = $game;
30 'api/v1/apps/' . $game->package,
34 'api/v1/details-data/' . $game->package . '.json',
41 //FIXME: build discover section
44 * Build api/v1/apps/$package
46 function buildApps($game)
48 $latestRelease = getLatestRelease($game);
50 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
53 'uuid' => $game->uuid,
54 'title' => $game->title,
55 'overview' => $game->overview,
56 'description' => $game->description,
57 'gamerNumbers' => $game->players,
58 'genres' => $game->genres,
60 'website' => $game->website,
61 'contentRating' => $game->contentRating,
62 'premium' => $game->premium,
63 'firstPublishedAt' => $game->firstPublishedAt,
65 'likeCount' => $game->rating->likeCount,
66 'ratingAverage' => $game->rating->average,
67 'ratingCount' => $game->rating->count,
69 'versionNumber' => $latestRelease->name,
70 'latestVersion' => $latestRelease->uuid,
71 'md5sum' => $latestRelease->md5sum,
72 'apkFileSize' => $latestRelease->size,
73 'publishedAt' => $latestRelease->date,
74 'publicSize' => $latestRelease->publicSize,
75 'nativeSize' => $latestRelease->nativeSize,
77 'mainImageFullUrl' => $game->media->large,
78 'videoUrl' => $game->media->video,
79 'filepickerScreenshots' => $game->media->screenshots,
80 'mobileAppIcon' => null,
82 'developer' => $game->developer->name,
83 'supportEmailAddress' => $game->developer->supportEmail,
84 'supportPhone' => $game->developer->supportPhone,
85 'founder' => $game->developer->founder,
87 'promotedProduct' => null,
93 * Build /app/v1/details?app=org.example.game
95 function buildDetails($game)
97 $latestRelease = getLatestRelease($game);
100 if ($game->media->large) {
104 'thumbnail' => $game->media->large,
105 'full' => $game->media->large,
107 'fp_url' => $game->media->large,
110 if ($game->media->video) {
113 'url' => $game->media->video,
116 foreach ($game->media->screenshots as $screenshot) {
120 'thumbnail' => $screenshot,
121 'full' => $screenshot,
123 'fp_url' => $screenshot,
127 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
130 'title' => $game->title,
131 'description' => $game->description,
132 'gamerNumbers' => $game->players,
133 'genres' => $game->genres,
135 'suggestedAge' => $game->contentRating,
136 'premium' => $game->premium,
137 'inAppPurchases' => $game->inAppPurchases,
138 'firstPublishedAt' => strtotime($game->firstPublishedAt),
142 'count' => $game->rating->count,
143 'average' => $game->rating->average,
147 'fileSize' => $latestRelease->size,
148 'nativeSize' => $latestRelease->nativeSize,
149 'publicSize' => $latestRelease->publicSize,
150 'md5sum' => $latestRelease->md5sum,
151 'filename' => 'FIXME',
153 'package' => $game->package,
154 'versionCode' => $latestRelease->versionCode,
155 'state' => 'complete',
159 'number' => $latestRelease->name,
160 'publishedAt' => strtotime($latestRelease->date),
161 'uuid' => $latestRelease->uuid,
165 'name' => $game->developer->name,
166 'founder' => $game->developer->founder,
170 'key:rating.average',
171 'key:developer.name',
173 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
176 'tileImage' => $game->media->discover,
177 'mediaTiles' => $mediaTiles,
178 'mobileAppIcon' => null,
183 'promotedProduct' => null,
187 function addMissingGameProperties($game)
189 if (!isset($game->overview)) {
190 $game->overview = null;
192 if (!isset($game->description)) {
193 $game->description = '';
195 if (!isset($game->players)) {
196 $game->players = [1];
198 if (!isset($game->genres)) {
199 $game->genres = ['Unsorted'];
201 if (!isset($game->website)) {
202 $game->website = null;
204 if (!isset($game->contentRating)) {
205 $game->contentRating = 'Everyone';
207 if (!isset($game->premium)) {
208 $game->premium = false;
210 if (!isset($game->firstPublishedAt)) {
211 $game->firstPublishedAt = gmdate('c');
214 if (!isset($game->rating)) {
215 $game->rating = new stdClass();
217 if (!isset($game->rating->likeCount)) {
218 $game->rating->likeCount = 0;
220 if (!isset($game->rating->average)) {
221 $game->rating->average = 0;
223 if (!isset($game->rating->count)) {
224 $game->rating->count = 0;
227 foreach ($game->releases as $release) {
228 if (!isset($release->publicSize)) {
229 $release->publicSize = 0;
231 if (!isset($release->nativeSize)) {
232 $release->nativeSize = 0;
236 if (!isset($game->media->video)) {
237 $game->media->video = null;
239 if (!isset($game->media->screenshots)) {
240 $game->media->screenshots = [];
242 if (!isset($game->developer->uuid)) {
243 $game->developer->uuid = null;
245 if (!isset($game->developer->name)) {
246 $game->developer->name = 'unknown';
248 if (!isset($game->developer->supportEmail)) {
249 $game->developer->supportEmail = null;
251 if (!isset($game->developer->supportPhone)) {
252 $game->developer->supportPhone = null;
254 if (!isset($game->developer->founder)) {
255 $game->developer->founder = false;
259 function getLatestRelease($game)
261 $latestRelease = null;
262 foreach ($game->releases as $release) {
263 if ($release->latest ?? false) {
264 $latestRelease = $release;
268 if ($latestRelease === null) {
269 error('No latest release for ' . $game->package);
271 return $latestRelease;
274 function writeJson($path, $data)
277 $fullPath = $wwwDir . $path;
278 $dir = dirname($fullPath);
280 mkdir($dir, 0777, true);
284 json_encode($data, JSON_PRETTY_PRINT) . "\n"
290 fwrite(STDERR, $msg . "\n");