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 ini_set('xdebug.halt_level', E_WARNING|E_NOTICE|E_USER_WARNING|E_USER_NOTICE);
10 require_once __DIR__ . '/filters.php';
11 if (!isset($argv[1])) {
12 error('Pass the path to a "folders" file with game data json files folder names');
14 $foldersFile = $argv[1];
15 if (!is_file($foldersFile)) {
16 error('Given path is not a file: ' . $foldersFile);
19 $GLOBALS['packagelists']['cweiskepicks'] = [
20 'de.eiswuxe.blookid2',
21 'com.cosmos.babyloniantwins'
24 $wwwDir = __DIR__ . '/../www/';
26 $baseDir = dirname($foldersFile);
28 foreach (file($foldersFile) as $line) {
31 if (strpos($line, '..') !== false) {
32 error('Path attack in ' . $folder);
34 $folder = $baseDir . '/' . $line;
35 if (!is_dir($folder)) {
36 error('Folder does not exist: ' . $folder);
38 $gameFiles = array_merge($gameFiles, glob($folder . '/*.json'));
44 foreach ($gameFiles as $gameFile) {
45 $game = json_decode(file_get_contents($gameFile));
47 error('JSON invalid at ' . $gameFile);
49 addMissingGameProperties($game);
50 $games[$game->packageName] = $game;
53 'api/v1/details-data/' . $game->packageName . '.json',
56 /* this crashes babylonian twins
58 'api/v1/games/' . $game->packageName . '/purchases',
64 'api/v1/apps/' . $game->packageName . '.json',
67 $latestRelease = $game->latestRelease;
69 'api/v1/apps/' . $latestRelease->uuid . '.json',
74 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
75 buildAppDownload($game, $latestRelease)
83 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
84 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
87 function buildDiscover(array $games)
90 'title' => 'DISCOVER',
96 $data, 'Last Updated',
97 filterLastUpdated($games, 10)
101 filterBestRated($games, 10)
104 $data, "cweiske's picks",
105 filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
114 addDiscoverRow($data, '# of players', $players);
115 foreach ($players as $num => $title) {
117 'api/v1/discover-data/' . categoryPath($title) . '.json',
118 buildDiscoverCategory($title, filterByPlayers($games, $num))
122 $ages = getAllAges($games);
124 addDiscoverRow($data, 'Content rating', $ages);
125 foreach ($ages as $num => $title) {
127 'api/v1/discover-data/' . categoryPath($title) . '.json',
128 buildDiscoverCategory($title, filterByAge($games, $title))
132 $genres = getAllGenres($games);
134 addChunkedDiscoverRows($data, $genres, 'Genres');
136 foreach ($genres as $genre) {
138 'api/v1/discover-data/' . categoryPath($genre) . '.json',
139 buildDiscoverCategory($genre, filterByGenre($games, $genre))
143 $abc = array_merge(range('A', 'Z'), ['Other']);
144 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
145 foreach ($abc as $letter) {
147 'api/v1/discover-data/' . categoryPath($letter) . '.json',
148 buildDiscoverCategory($letter, filterByLetter($games, $letter))
156 * A genre category page
158 function buildDiscoverCategory($name, $games)
166 $data, 'Last Updated',
167 filterLastUpdated($games, 10)
171 filterBestRated($games, 10)
176 function ($gameA, $gameB) {
177 return strcmp($gameA->title, $gameB->title);
180 $chunks = array_chunk($games, 4);
181 foreach ($chunks as $chunkGames) {
182 addDiscoverRow($data, '', $chunkGames);
188 function buildDiscoverHome(array $games)
190 //we do not want anything here for now
195 'title' => 'FEATURED',
196 'showPrice' => false,
207 * Build api/v1/apps/$packageName
209 function buildApps($game)
211 $latestRelease = $game->latestRelease;
214 $gamePromoted = getPromotedProduct($game);
217 'type' => 'entitlement',
218 'identifier' => $gamePromoted->identifier,
219 'name' => $gamePromoted->name,
220 'description' => $gamePromoted->description ?? '',
221 'localPrice' => $gamePromoted->localPrice,
222 'originalPrice' => $gamePromoted->originalPrice,
224 'currency' => $gamePromoted->currency,
228 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
231 'uuid' => $latestRelease->uuid,
232 'title' => $game->title,
233 'overview' => $game->overview,
234 'description' => $game->description,
235 'gamerNumbers' => $game->players,
236 'genres' => $game->genres,
238 'website' => $game->website,
239 'contentRating' => $game->contentRating,
240 'premium' => $game->premium,
241 'firstPublishedAt' => $game->firstPublishedAt,
243 'likeCount' => $game->rating->likeCount,
244 'ratingAverage' => $game->rating->average,
245 'ratingCount' => $game->rating->count,
247 'versionNumber' => $latestRelease->name,
248 'latestVersion' => $latestRelease->uuid,
249 'md5sum' => $latestRelease->md5sum,
250 'apkFileSize' => $latestRelease->size,
251 'publishedAt' => $latestRelease->date,
252 'publicSize' => $latestRelease->publicSize,
253 'nativeSize' => $latestRelease->nativeSize,
255 'mainImageFullUrl' => $game->discover,
256 'videoUrl' => getFirstVideoUrl($game->media),
257 'filepickerScreenshots' => getAllImageUrls($game->media),
258 'mobileAppIcon' => null,
260 'developer' => $game->developer->name,
261 'supportEmailAddress' => $game->developer->supportEmail,
262 'supportPhone' => $game->developer->supportPhone,
263 'founder' => $game->developer->founder,
265 'promotedProduct' => $product,
270 function buildAppDownload($game, $release)
274 'fileSize' => $release->size,
275 'version' => $release->uuid,
276 'contentRating' => $game->contentRating,
277 'downloadLink' => $release->url,
283 * Build /app/v1/details?app=org.example.game
285 function buildDetails($game)
287 $latestRelease = $game->latestRelease;
290 if ($game->discover) {
294 'thumbnail' => $game->discover,
295 'full' => $game->discover,
299 foreach ($game->media as $medium) {
300 if ($medium->type == 'image') {
304 'thumbnail' => $medium->thumb,
305 'full' => $medium->url,
311 'url' => $medium->url,
317 if (isset($game->links->unlocked)) {
319 'text' => 'Show unlocked',
320 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
326 $gamePromoted = getPromotedProduct($game);
329 'type' => 'entitlement',
330 'identifier' => $gamePromoted->identifier,
331 'name' => $gamePromoted->name,
332 'description' => $gamePromoted->description ?? '',
333 'localPrice' => $gamePromoted->localPrice,
334 'originalPrice' => $gamePromoted->originalPrice,
336 'currency' => $gamePromoted->currency,
340 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
343 'title' => $game->title,
344 'description' => $game->description,
345 'gamerNumbers' => $game->players,
346 'genres' => $game->genres,
348 'suggestedAge' => $game->contentRating,
349 'premium' => $game->premium,
350 'inAppPurchases' => $game->inAppPurchases,
351 'firstPublishedAt' => strtotime($game->firstPublishedAt),
355 'count' => $game->rating->count,
356 'average' => $game->rating->average,
360 'fileSize' => $latestRelease->size,
361 'nativeSize' => $latestRelease->nativeSize,
362 'publicSize' => $latestRelease->publicSize,
363 'md5sum' => $latestRelease->md5sum,
364 'filename' => 'FIXME',
366 'package' => $game->packageName,
367 'versionCode' => $latestRelease->versionCode,
368 'state' => 'complete',
372 'number' => $latestRelease->name,
373 'publishedAt' => strtotime($latestRelease->date),
374 'uuid' => $latestRelease->uuid,
378 'name' => $game->developer->name,
379 'founder' => $game->developer->founder,
383 'key:rating.average',
384 'key:developer.name',
386 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
389 'tileImage' => $game->discover,
390 'mediaTiles' => $mediaTiles,
391 'mobileAppIcon' => null,
396 'promotedProduct' => $product,
397 'buttons' => $buttons,
401 function addChunkedDiscoverRows(&$data, $games, $title)
403 $chunks = array_chunk($games, 4);
405 foreach ($chunks as $chunk) {
407 $data, $first ? $title : '',
414 function addDiscoverRow(&$data, $title, $games)
418 'showPrice' => false,
422 foreach ($games as $game) {
423 if (is_string($game)) {
425 $tilePos = count($data['tiles']);
426 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
430 if (isset($game->links->original)) {
431 //do not link unlocked games.
432 // people an access them via the original games
435 $tilePos = findTile($data['tiles'], $game->packageName);
436 if ($tilePos === null) {
437 $tilePos = count($data['tiles']);
438 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
441 $row['tiles'][] = $tilePos;
443 $data['rows'][] = $row;
446 function findTile($tiles, $packageName)
448 foreach ($tiles as $pos => $tile) {
449 if ($tile['package'] == $packageName) {
456 function buildDiscoverCategoryTile($title)
459 'url' => 'ouya://launcher/discover/' . categoryPath($title),
466 function buildDiscoverGameTile($game)
468 $latestRelease = $game->latestRelease;
470 'gamerNumbers' => $game->players,
471 'genres' => $game->genres,
472 'url' => 'ouya://launcher/details?app=' . $game->packageName,
475 'md5sum' => $latestRelease->md5sum,
477 'versionNumber' => $latestRelease->name,
478 'uuid' => $latestRelease->uuid,
480 'inAppPurchases' => $game->inAppPurchases,
481 'promotedProduct' => null,
482 'premium' => $game->premium,
484 'package' => $game->packageName,
485 'updated_at' => strtotime($latestRelease->date),
486 'updatedAt' => $latestRelease->date,
487 'title' => $game->title,
488 'image' => $game->discover,
489 'contentRating' => $game->contentRating,
491 'count' => $game->rating->count,
492 'average' => $game->rating->average,
497 function categoryPath($title)
499 return str_replace(['/', '\\', ' ', '+'], '_', $title);
502 function getAllAges($games)
505 foreach ($games as $game) {
506 $ages[] = $game->contentRating;
508 return array_unique($ages);
511 function getAllGenres($games)
514 foreach ($games as $game) {
515 $genres = array_merge($genres, $game->genres);
517 return array_unique($genres);
520 function addMissingGameProperties($game)
522 if (!isset($game->overview)) {
523 $game->overview = null;
525 if (!isset($game->description)) {
526 $game->description = '';
528 if (!isset($game->players)) {
529 $game->players = [1];
531 if (!isset($game->genres)) {
532 $game->genres = ['Unsorted'];
534 if (!isset($game->website)) {
535 $game->website = null;
537 if (!isset($game->contentRating)) {
538 $game->contentRating = 'Everyone';
540 if (!isset($game->premium)) {
541 $game->premium = false;
543 if (!isset($game->firstPublishedAt)) {
544 $game->firstPublishedAt = gmdate('c');
547 if (!isset($game->rating)) {
548 $game->rating = new stdClass();
550 if (!isset($game->rating->likeCount)) {
551 $game->rating->likeCount = 0;
553 if (!isset($game->rating->average)) {
554 $game->rating->average = 0;
556 if (!isset($game->rating->count)) {
557 $game->rating->count = 0;
560 $game->latestRelease = null;
561 $latestReleaseTimestamp = 0;
562 foreach ($game->releases as $release) {
563 if (!isset($release->publicSize)) {
564 $release->publicSize = 0;
566 if (!isset($release->nativeSize)) {
567 $release->nativeSize = 0;
570 $releaseTimestamp = strtotime($release->date);
571 if ($releaseTimestamp > $latestReleaseTimestamp) {
572 $game->latestRelease = $release;
573 $latestReleaseTimestamp = $releaseTimestamp;
576 if ($game->latestRelease === null) {
577 error('No latest release for ' . $game->packageName);
580 if (!isset($game->media)) {
584 if (!isset($game->developer->uuid)) {
585 $game->developer->uuid = null;
587 if (!isset($game->developer->name)) {
588 $game->developer->name = 'unknown';
590 if (!isset($game->developer->supportEmail)) {
591 $game->developer->supportEmail = null;
593 if (!isset($game->developer->supportPhone)) {
594 $game->developer->supportPhone = null;
596 if (!isset($game->developer->founder)) {
597 $game->developer->founder = false;
601 function getFirstVideoUrl($media)
603 foreach ($media as $medium) {
604 if ($medium->type == 'video') {
611 function getAllImageUrls($media)
614 foreach ($media as $medium) {
615 if ($medium->type == 'image') {
616 $imageUrls[] = $medium->url;
622 function getPromotedProduct($game)
624 if (!isset($game->products) || !count($game->products)) {
627 foreach ($game->products as $gameProd) {
628 if ($gameProd->promoted) {
635 function writeJson($path, $data)
638 $fullPath = $wwwDir . $path;
639 $dir = dirname($fullPath);
641 mkdir($dir, 0777, true);
645 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
651 fwrite(STDERR, $msg . "\n");