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'));
45 foreach ($gameFiles as $gameFile) {
46 $game = json_decode(file_get_contents($gameFile));
48 error('JSON invalid at ' . $gameFile);
50 addMissingGameProperties($game);
51 $games[$game->packageName] = $game;
54 'api/v1/details-data/' . $game->packageName . '.json',
58 if (!isset($developers[$game->developer->uuid])) {
59 $developers[$game->developer->uuid] = [
60 'info' => $game->developer,
65 $products = $game->products ?? [];
66 foreach ($products as $product) {
68 'api/v1/developers/' . $game->developer->uuid
69 . '/products/' . $product->identifier . '.json',
70 buildDeveloperProductOnly($product, $game->developer)
72 $developers[$game->developer->uuid]['products'][] = $product;
77 'api/v1/games/' . $game->packageName . '/purchases',
82 'api/v1/apps/' . $game->packageName . '.json',
85 $latestRelease = $game->latestRelease;
87 'api/v1/apps/' . $latestRelease->uuid . '.json',
92 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
93 buildAppDownload($game, $latestRelease)
101 foreach ($developers as $developer) {
103 //index.htm does not need a rewrite rule
104 'api/v1/developers/' . $developer['info']->uuid
105 . '/products/index.htm',
106 buildDeveloperProducts($developer['products'], $developer['info'])
109 //index.htm does not need a rewrite rule
110 'api/v1/developers/' . $developer['info']->uuid
112 buildDeveloperCurrentGamer()
116 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
117 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
121 'api/v1/discover-data/tutorials.json',
122 buildMakeCategory('Tutorials', filterByGenre($games, 'Tutorials'))
127 function buildDiscover(array $games)
129 $games = removeMakeGames($games);
131 'title' => 'DISCOVER',
137 $data, 'Last Updated',
138 filterLastUpdated($games, 10)
142 filterBestRated($games, 10)
145 $data, "cweiske's picks",
146 filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
155 addDiscoverRow($data, '# of players', $players);
156 foreach ($players as $num => $title) {
158 'api/v1/discover-data/' . categoryPath($title) . '.json',
159 buildDiscoverCategory($title, filterByPlayers($games, $num))
163 $ages = getAllAges($games);
165 addDiscoverRow($data, 'Content rating', $ages);
166 foreach ($ages as $num => $title) {
168 'api/v1/discover-data/' . categoryPath($title) . '.json',
169 buildDiscoverCategory($title, filterByAge($games, $title))
173 $genres = removeMakeGenres(getAllGenres($games));
175 addChunkedDiscoverRows($data, $genres, 'Genres');
177 foreach ($genres as $genre) {
179 'api/v1/discover-data/' . categoryPath($genre) . '.json',
180 buildDiscoverCategory($genre, filterByGenre($games, $genre))
184 $abc = array_merge(range('A', 'Z'), ['Other']);
185 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
186 foreach ($abc as $letter) {
188 'api/v1/discover-data/' . categoryPath($letter) . '.json',
189 buildDiscoverCategory($letter, filterByLetter($games, $letter))
197 * A genre category page
199 function buildDiscoverCategory($name, $games)
207 $data, 'Last Updated',
208 filterLastUpdated($games, 10)
212 filterBestRated($games, 10)
217 function ($gameA, $gameB) {
218 return strcasecmp($gameA->title, $gameB->title);
221 $chunks = array_chunk($games, 4);
222 foreach ($chunks as $chunkGames) {
223 addDiscoverRow($data, '', $chunkGames);
229 function buildMakeCategory($name, $games)
239 function ($gameA, $gameB) {
240 return strcasecmp($gameA->title, $gameB->title);
243 addDiscoverRow($data, '', $games);
248 function buildDiscoverHome(array $games)
250 //we do not want anything here for now
255 'title' => 'FEATURED',
256 'showPrice' => false,
267 * Build api/v1/apps/$packageName
269 function buildApps($game)
271 $latestRelease = $game->latestRelease;
274 $gamePromoted = getPromotedProduct($game);
276 $product = buildProduct($gamePromoted);
279 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
282 'uuid' => $latestRelease->uuid,
283 'title' => $game->title,
284 'overview' => $game->overview,
285 'description' => $game->description,
286 'gamerNumbers' => $game->players,
287 'genres' => $game->genres,
289 'website' => $game->website,
290 'contentRating' => $game->contentRating,
291 'premium' => $game->premium,
292 'firstPublishedAt' => $game->firstPublishedAt,
294 'likeCount' => $game->rating->likeCount,
295 'ratingAverage' => $game->rating->average,
296 'ratingCount' => $game->rating->count,
298 'versionNumber' => $latestRelease->name,
299 'latestVersion' => $latestRelease->uuid,
300 'md5sum' => $latestRelease->md5sum,
301 'apkFileSize' => $latestRelease->size,
302 'publishedAt' => $latestRelease->date,
303 'publicSize' => $latestRelease->publicSize,
304 'nativeSize' => $latestRelease->nativeSize,
306 'mainImageFullUrl' => $game->discover,
307 'videoUrl' => getFirstVideoUrl($game->media),
308 'filepickerScreenshots' => getAllImageUrls($game->media),
309 'mobileAppIcon' => null,
311 'developer' => $game->developer->name,
312 'supportEmailAddress' => $game->developer->supportEmail,
313 'supportPhone' => $game->developer->supportPhone,
314 'founder' => $game->developer->founder,
316 'promotedProduct' => $product,
321 function buildAppDownload($game, $release)
325 'fileSize' => $release->size,
326 'version' => $release->uuid,
327 'contentRating' => $game->contentRating,
328 'downloadLink' => $release->url,
333 function buildProduct($product)
335 if ($product === null) {
339 'type' => 'entitlement',
340 'identifier' => $product->identifier,
341 'name' => $product->name,
342 'description' => $product->description ?? '',
343 'localPrice' => $product->localPrice,
344 'originalPrice' => $product->originalPrice,
346 'currency' => $product->currency,
351 * Build /app/v1/details?app=org.example.game
353 function buildDetails($game)
355 $latestRelease = $game->latestRelease;
358 if ($game->discover) {
362 'thumbnail' => $game->discover,
363 'full' => $game->discover,
367 foreach ($game->media as $medium) {
368 if ($medium->type == 'image') {
372 'thumbnail' => $medium->thumb,
373 'full' => $medium->url,
379 'url' => $medium->url,
385 if (isset($game->links->unlocked)) {
387 'text' => 'Show unlocked',
388 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
394 $gamePromoted = getPromotedProduct($game);
396 $product = buildProduct($gamePromoted);
399 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
402 'title' => $game->title,
403 'description' => $game->description,
404 'gamerNumbers' => $game->players,
405 'genres' => $game->genres,
407 'suggestedAge' => $game->contentRating,
408 'premium' => $game->premium,
409 'inAppPurchases' => $game->inAppPurchases,
410 'firstPublishedAt' => strtotime($game->firstPublishedAt),
414 'count' => $game->rating->count,
415 'average' => $game->rating->average,
419 'fileSize' => $latestRelease->size,
420 'nativeSize' => $latestRelease->nativeSize,
421 'publicSize' => $latestRelease->publicSize,
422 'md5sum' => $latestRelease->md5sum,
423 'filename' => 'FIXME',
425 'package' => $game->packageName,
426 'versionCode' => $latestRelease->versionCode,
427 'state' => 'complete',
431 'number' => $latestRelease->name,
432 'publishedAt' => strtotime($latestRelease->date),
433 'uuid' => $latestRelease->uuid,
437 'name' => $game->developer->name,
438 'founder' => $game->developer->founder,
442 'key:rating.average',
443 'key:developer.name',
445 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
448 'tileImage' => $game->discover,
449 'mediaTiles' => $mediaTiles,
450 'mobileAppIcon' => null,
455 'promotedProduct' => $product,
456 'buttons' => $buttons,
460 function buildDeveloperCurrentGamer()
464 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
465 'username' => 'stouyapi',
471 * For /api/v1/developers/xxx/products/?only=yyy
473 function buildDeveloperProductOnly($product, $developer)
476 'developerName' => $developer->name,
477 'currency' => $product->currency,
479 buildProduct($product),
485 * For /api/v1/developers/xxx/products/
487 function buildDeveloperProducts($products, $developer)
490 foreach ($products as $product) {
491 $jsonProducts[] = buildProduct($product);
494 'developerName' => $developer->name,
495 'currency' => $products[0]->currency ?? 'EUR',
496 'products' => $jsonProducts,
500 function buildPurchases($game)
505 $promotedProduct = getPromotedProduct($game);
506 if ($promotedProduct) {
507 $purchasesData['purchases'][] = [
508 'purchaseDate' => time() * 1000,
509 'generateDate' => time() * 1000,
510 'identifier' => $promotedProduct->identifier,
511 'gamer' => 'stouyapi',
512 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
513 'priceInCents' => $promotedProduct->originalPrice * 100,
514 'localPrice' => $promotedProduct->localPrice,
515 'currency' => $promotedProduct->currency,
519 $encryptedOnce = dummyEncrypt($purchasesData);
520 $encryptedTwice = dummyEncrypt($encryptedOnce);
521 return $encryptedTwice;
524 function dummyEncrypt($data)
527 'key' => base64_encode('0123456789abcdef') . "\n",
528 'iv' => 't3jir1LHpICunvhlM76edQ==' . "\n",//random bytes
529 'blob' => base64_encode(
530 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
535 function addChunkedDiscoverRows(&$data, $games, $title)
537 $chunks = array_chunk($games, 4);
539 foreach ($chunks as $chunk) {
541 $data, $first ? $title : '',
548 function addDiscoverRow(&$data, $title, $games)
556 foreach ($games as $game) {
557 if (is_string($game)) {
559 $tilePos = count($data['tiles']);
560 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
564 if (isset($game->links->original)) {
565 //do not link unlocked games.
566 // people an access them via the original games
569 $tilePos = findTile($data['tiles'], $game->packageName);
570 if ($tilePos === null) {
571 $tilePos = count($data['tiles']);
572 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
575 $row['tiles'][] = $tilePos;
577 $data['rows'][] = $row;
580 function findTile($tiles, $packageName)
582 foreach ($tiles as $pos => $tile) {
583 if ($tile['package'] == $packageName) {
590 function buildDiscoverCategoryTile($title)
593 'url' => 'ouya://launcher/discover/' . categoryPath($title),
600 function buildDiscoverGameTile($game)
602 $latestRelease = $game->latestRelease;
604 'gamerNumbers' => $game->players,
605 'genres' => $game->genres,
606 'url' => 'ouya://launcher/details?app=' . $game->packageName,
609 'md5sum' => $latestRelease->md5sum,
611 'versionNumber' => $latestRelease->name,
612 'uuid' => $latestRelease->uuid,
614 'inAppPurchases' => $game->inAppPurchases,
615 'promotedProduct' => null,
616 'premium' => $game->premium,
618 'package' => $game->packageName,
619 'updated_at' => strtotime($latestRelease->date),
620 'updatedAt' => $latestRelease->date,
621 'title' => $game->title,
622 'image' => $game->discover,
623 'contentRating' => $game->contentRating,
625 'count' => $game->rating->count,
626 'average' => $game->rating->average,
628 'promotedProduct' => buildProduct(getPromotedProduct($game)),
632 function categoryPath($title)
634 return str_replace(['/', '\\', ' ', '+'], '_', $title);
637 function getAllAges($games)
640 foreach ($games as $game) {
641 $ages[] = $game->contentRating;
643 return array_unique($ages);
646 function getAllGenres($games)
649 foreach ($games as $game) {
650 $genres = array_merge($genres, $game->genres);
652 return array_unique($genres);
655 function addMissingGameProperties($game)
657 if (!isset($game->overview)) {
658 $game->overview = null;
660 if (!isset($game->description)) {
661 $game->description = '';
663 if (!isset($game->players)) {
664 $game->players = [1];
666 if (!isset($game->genres)) {
667 $game->genres = ['Unsorted'];
669 if (!isset($game->website)) {
670 $game->website = null;
672 if (!isset($game->contentRating)) {
673 $game->contentRating = 'Everyone';
675 if (!isset($game->premium)) {
676 $game->premium = false;
678 if (!isset($game->firstPublishedAt)) {
679 $game->firstPublishedAt = gmdate('c');
682 if (!isset($game->rating)) {
683 $game->rating = new stdClass();
685 if (!isset($game->rating->likeCount)) {
686 $game->rating->likeCount = 0;
688 if (!isset($game->rating->average)) {
689 $game->rating->average = 0;
691 if (!isset($game->rating->count)) {
692 $game->rating->count = 0;
695 $game->latestRelease = null;
696 $latestReleaseTimestamp = 0;
697 foreach ($game->releases as $release) {
698 if (!isset($release->publicSize)) {
699 $release->publicSize = 0;
701 if (!isset($release->nativeSize)) {
702 $release->nativeSize = 0;
705 $releaseTimestamp = strtotime($release->date);
706 if ($releaseTimestamp > $latestReleaseTimestamp) {
707 $game->latestRelease = $release;
708 $latestReleaseTimestamp = $releaseTimestamp;
711 if ($game->latestRelease === null) {
712 error('No latest release for ' . $game->packageName);
715 if (!isset($game->media)) {
719 if (!isset($game->developer->uuid)) {
720 $game->developer->uuid = null;
722 if (!isset($game->developer->name)) {
723 $game->developer->name = 'unknown';
725 if (!isset($game->developer->supportEmail)) {
726 $game->developer->supportEmail = null;
728 if (!isset($game->developer->supportPhone)) {
729 $game->developer->supportPhone = null;
731 if (!isset($game->developer->founder)) {
732 $game->developer->founder = false;
736 function getFirstVideoUrl($media)
738 foreach ($media as $medium) {
739 if ($medium->type == 'video') {
746 function getAllImageUrls($media)
749 foreach ($media as $medium) {
750 if ($medium->type == 'image') {
751 $imageUrls[] = $medium->url;
757 function getPromotedProduct($game)
759 if (!isset($game->products) || !count($game->products)) {
762 foreach ($game->products as $gameProd) {
763 if ($gameProd->promoted) {
770 function removeMakeGames(array $games)
772 return filterByGenre($games, 'Tutorials', true);
775 function removeMakeGenres($genres)
778 foreach ($genres as $genre) {
779 if ($genre != 'Tutorials' && $genre != 'Builds') {
780 $filtered[] = $genre;
786 function writeJson($path, $data)
789 $fullPath = $wwwDir . $path;
790 $dir = dirname($fullPath);
792 mkdir($dir, 0777, true);
796 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
802 fwrite(STDERR, $msg . "\n");