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 $games[$game->package] = $game;
30 'api/v1/details-data/' . $game->package . '.json',
35 'api/v1/apps/' . $game->package . '.json',
38 $latestRelease = getLatestRelease($game);
40 'api/v1/apps/' . $latestRelease->uuid . '.json',
45 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
46 buildAppDownload($game, $latestRelease)
52 writeJson('api/v1/discover.json', buildDiscover($games));
53 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
56 * Build api/v1/apps/$package
58 function buildApps($game)
60 $latestRelease = getLatestRelease($game);
62 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
65 'uuid' => $game->uuid,
66 'title' => $game->title,
67 'overview' => $game->overview,
68 'description' => $game->description,
69 'gamerNumbers' => $game->players,
70 'genres' => $game->genres,
72 'website' => $game->website,
73 'contentRating' => $game->contentRating,
74 'premium' => $game->premium,
75 'firstPublishedAt' => $game->firstPublishedAt,
77 'likeCount' => $game->rating->likeCount,
78 'ratingAverage' => $game->rating->average,
79 'ratingCount' => $game->rating->count,
81 'versionNumber' => $latestRelease->name,
82 'latestVersion' => $latestRelease->uuid,
83 'md5sum' => $latestRelease->md5sum,
84 'apkFileSize' => $latestRelease->size,
85 'publishedAt' => $latestRelease->date,
86 'publicSize' => $latestRelease->publicSize,
87 'nativeSize' => $latestRelease->nativeSize,
89 'mainImageFullUrl' => $game->media->large,
90 'videoUrl' => $game->media->video,
91 'filepickerScreenshots' => $game->media->screenshots,
92 'mobileAppIcon' => null,
94 'developer' => $game->developer->name,
95 'supportEmailAddress' => $game->developer->supportEmail,
96 'supportPhone' => $game->developer->supportPhone,
97 'founder' => $game->developer->founder,
99 'promotedProduct' => null,
104 function buildAppDownload($game, $release)
108 'fileSize' => $release->size,
109 'version' => $release->uuid,
110 'contentRating' => $game->contentRating,
111 'downloadLink' => $release->url,
117 * Build /app/v1/details?app=org.example.game
119 function buildDetails($game)
121 $latestRelease = getLatestRelease($game);
124 if ($game->media->large) {
128 'thumbnail' => $game->media->large,
129 'full' => $game->media->large,
131 'fp_url' => $game->media->large,
134 if ($game->media->video) {
137 'url' => $game->media->video,
140 foreach ($game->media->screenshots as $screenshot) {
144 'thumbnail' => $screenshot,
145 'full' => $screenshot,
147 'fp_url' => $screenshot,
151 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
154 'title' => $game->title,
155 'description' => $game->description,
156 'gamerNumbers' => $game->players,
157 'genres' => $game->genres,
159 'suggestedAge' => $game->contentRating,
160 'premium' => $game->premium,
161 'inAppPurchases' => $game->inAppPurchases,
162 'firstPublishedAt' => strtotime($game->firstPublishedAt),
166 'count' => $game->rating->count,
167 'average' => $game->rating->average,
171 'fileSize' => $latestRelease->size,
172 'nativeSize' => $latestRelease->nativeSize,
173 'publicSize' => $latestRelease->publicSize,
174 'md5sum' => $latestRelease->md5sum,
175 'filename' => 'FIXME',
177 'package' => $game->package,
178 'versionCode' => $latestRelease->versionCode,
179 'state' => 'complete',
183 'number' => $latestRelease->name,
184 'publishedAt' => strtotime($latestRelease->date),
185 'uuid' => $latestRelease->uuid,
189 'name' => $game->developer->name,
190 'founder' => $game->developer->founder,
194 'key:rating.average',
195 'key:developer.name',
197 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
200 'tileImage' => $game->media->discover,
201 'mediaTiles' => $mediaTiles,
202 'mobileAppIcon' => null,
207 'promotedProduct' => null,
211 function buildDiscover(array $games)
214 'title' => 'DISCOVER',
221 'title' => 'ALL GAMES',
222 'showPrice' => false,
226 foreach ($games as $game) {
227 $tilePos = count($tileMap);
228 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
229 $tileMap[$game->package] = $tilePos;
231 $rowAll['tiles'][] = $tilePos;
233 $data['rows'][] = $rowAll;
238 function buildDiscoverHome(array $games)
240 //we do not want anything here for now
245 'title' => 'FEATURED',
246 'showPrice' => false,
256 function buildDiscoverGameTile($game)
258 $latestRelease = getLatestRelease($game);
260 'gamerNumbers' => $game->players,
261 'genres' => $game->genres,
262 'url' => 'ouya://launcher/details?app=' . $game->package,
265 'md5sum' => $latestRelease->md5sum,
267 'versionNumber' => $latestRelease->name,
268 'uuid' => $latestRelease->uuid,
270 'inAppPurchases' => $game->inAppPurchases,
271 'promotedProduct' => null,
272 'premium' => $game->premium,
274 'package' => $game->package,
275 'updated_at' => strtotime($latestRelease->date),
276 'updatedAt' => $latestRelease->date,
277 'title' => $game->title,
278 'image' => $game->media->discover,
279 'contentRating' => $game->contentRating,
281 'count' => $game->rating->count,
282 'average' => $game->rating->average,
287 function addMissingGameProperties($game)
289 if (!isset($game->overview)) {
290 $game->overview = null;
292 if (!isset($game->description)) {
293 $game->description = '';
295 if (!isset($game->players)) {
296 $game->players = [1];
298 if (!isset($game->genres)) {
299 $game->genres = ['Unsorted'];
301 if (!isset($game->website)) {
302 $game->website = null;
304 if (!isset($game->contentRating)) {
305 $game->contentRating = 'Everyone';
307 if (!isset($game->premium)) {
308 $game->premium = false;
310 if (!isset($game->firstPublishedAt)) {
311 $game->firstPublishedAt = gmdate('c');
314 if (!isset($game->rating)) {
315 $game->rating = new stdClass();
317 if (!isset($game->rating->likeCount)) {
318 $game->rating->likeCount = 0;
320 if (!isset($game->rating->average)) {
321 $game->rating->average = 0;
323 if (!isset($game->rating->count)) {
324 $game->rating->count = 0;
327 foreach ($game->releases as $release) {
328 if (!isset($release->publicSize)) {
329 $release->publicSize = 0;
331 if (!isset($release->nativeSize)) {
332 $release->nativeSize = 0;
336 if (!isset($game->media->video)) {
337 $game->media->video = null;
339 if (!isset($game->media->screenshots)) {
340 $game->media->screenshots = [];
342 if (!isset($game->developer->uuid)) {
343 $game->developer->uuid = null;
345 if (!isset($game->developer->name)) {
346 $game->developer->name = 'unknown';
348 if (!isset($game->developer->supportEmail)) {
349 $game->developer->supportEmail = null;
351 if (!isset($game->developer->supportPhone)) {
352 $game->developer->supportPhone = null;
354 if (!isset($game->developer->founder)) {
355 $game->developer->founder = false;
359 function getLatestRelease($game)
361 $latestRelease = null;
362 foreach ($game->releases as $release) {
363 if ($release->latest ?? false) {
364 $latestRelease = $release;
368 if ($latestRelease === null) {
369 error('No latest release for ' . $game->package);
371 return $latestRelease;
374 function writeJson($path, $data)
377 $fullPath = $wwwDir . $path;
378 $dir = dirname($fullPath);
380 mkdir($dir, 0777, true);
384 json_encode($data, JSON_PRETTY_PRINT) . "\n"
390 fwrite(STDERR, $msg . "\n");