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',
22 'com.inverseblue.skyriders',
25 $wwwDir = __DIR__ . '/../www/';
27 $baseDir = dirname($foldersFile);
29 foreach (file($foldersFile) as $line) {
32 if (strpos($line, '..') !== false) {
33 error('Path attack in ' . $folder);
35 $folder = $baseDir . '/' . $line;
36 if (!is_dir($folder)) {
37 error('Folder does not exist: ' . $folder);
39 $gameFiles = array_merge($gameFiles, glob($folder . '/*.json'));
46 foreach ($gameFiles as $gameFile) {
47 $game = json_decode(file_get_contents($gameFile));
49 error('JSON invalid at ' . $gameFile);
51 addMissingGameProperties($game);
52 $games[$game->packageName] = $game;
55 'api/v1/details-data/' . $game->packageName . '.json',
59 if (!isset($developers[$game->developer->uuid])) {
60 $developers[$game->developer->uuid] = [
61 'info' => $game->developer,
66 $products = $game->products ?? [];
67 foreach ($products as $product) {
69 'api/v1/developers/' . $game->developer->uuid
70 . '/products/' . $product->identifier . '.json',
71 buildDeveloperProductOnly($product, $game->developer)
73 $developers[$game->developer->uuid]['products'][] = $product;
78 'api/v1/games/' . $game->packageName . '/purchases',
83 'api/v1/apps/' . $game->packageName . '.json',
86 $latestRelease = $game->latestRelease;
88 'api/v1/apps/' . $latestRelease->uuid . '.json',
93 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
94 buildAppDownload($game, $latestRelease)
102 calculateRank($games);
104 foreach ($developers as $developer) {
106 //index.htm does not need a rewrite rule
107 'api/v1/developers/' . $developer['info']->uuid
108 . '/products/index.htm',
109 buildDeveloperProducts($developer['products'], $developer['info'])
112 //index.htm does not need a rewrite rule
113 'api/v1/developers/' . $developer['info']->uuid
115 buildDeveloperCurrentGamer()
119 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
120 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
124 'api/v1/discover-data/tutorials.json',
125 buildMakeCategory('Tutorials', filterByGenre($games, 'Tutorials'))
128 $searchLetters = 'abcdefghijklmnopqrstuvwxyz0123456789., ';
129 foreach (str_split($searchLetters) as $letter) {
130 $letterGames = filterBySearchWord($games, $letter);
132 'api/v1/search-data/' . $letter . '.json',
133 buildSearch($letterGames)
138 function buildDiscover(array $games)
140 $games = removeMakeGames($games);
142 'title' => 'DISCOVER',
148 $data, 'Last Updated',
149 filterLastUpdated($games, 10)
153 filterBestRated($games, 10)
156 $data, "cweiske's picks",
157 filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
169 'api/v1/discover-data/' . categoryPath('Best rated') . '.json',
170 buildSpecialCategory('Best rated', filterBestRated($games, 99))
173 'api/v1/discover-data/' . categoryPath('Most rated') . '.json',
174 buildSpecialCategory('Most rated', filterMostDownloaded($games, 99))
177 'api/v1/discover-data/' . categoryPath('Random') . '.json',
178 buildSpecialCategory(
179 'Random ' . date('Y-m-d H:i'),
180 filterRandom($games, 99)
190 addDiscoverRow($data, 'Multiplayer', $players);
191 foreach ($players as $num => $title) {
193 'api/v1/discover-data/' . categoryPath($title) . '.json',
194 buildDiscoverCategory($title, filterByPlayers($games, $num))
198 $ages = getAllAges($games);
200 addDiscoverRow($data, 'Content rating', $ages);
201 foreach ($ages as $num => $title) {
203 'api/v1/discover-data/' . categoryPath($title) . '.json',
204 buildDiscoverCategory($title, filterByAge($games, $title))
208 $genres = removeMakeGenres(getAllGenres($games));
210 addChunkedDiscoverRows($data, $genres, 'Genres');
212 foreach ($genres as $genre) {
214 'api/v1/discover-data/' . categoryPath($genre) . '.json',
215 buildDiscoverCategory($genre, filterByGenre($games, $genre))
219 $abc = array_merge(range('A', 'Z'), ['Other']);
220 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
221 foreach ($abc as $letter) {
223 'api/v1/discover-data/' . categoryPath($letter) . '.json',
224 buildDiscoverCategory($letter, filterByLetter($games, $letter))
232 * A genre category page
234 function buildDiscoverCategory($name, $games)
242 $data, 'Last Updated',
243 filterLastUpdated($games, 10)
247 filterBestRated($games, 10)
250 $games = sortByTitle($games);
251 $chunks = array_chunk($games, 4);
252 foreach ($chunks as $chunkGames) {
253 addDiscoverRow($data, '', $chunkGames);
259 function buildMakeCategory($name, $games)
267 $games = sortByTitle($games);
268 addDiscoverRow($data, '', $games);
273 function buildSpecialCategory($name, $games)
281 $first3 = array_slice($games, 0, 3);
282 $chunks = array_chunk(array_slice($games, 3), 4);
283 array_unshift($chunks, $first3);
285 foreach ($chunks as $chunkGames) {
286 addDiscoverRow($data, '', $chunkGames);
292 function buildDiscoverHome(array $games)
294 //we do not want anything here for now
299 'title' => 'FEATURED',
300 'showPrice' => false,
311 * Build api/v1/apps/$packageName
313 function buildApps($game)
315 $latestRelease = $game->latestRelease;
318 $gamePromoted = getPromotedProduct($game);
320 $product = buildProduct($gamePromoted);
323 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
326 'uuid' => $latestRelease->uuid,
327 'title' => $game->title,
328 'overview' => $game->overview,
329 'description' => $game->description,
330 'gamerNumbers' => $game->players,
331 'genres' => $game->genres,
333 'website' => $game->website,
334 'contentRating' => $game->contentRating,
335 'premium' => $game->premium,
336 'firstPublishedAt' => $game->firstPublishedAt,
338 'likeCount' => $game->rating->likeCount,
339 'ratingAverage' => $game->rating->average,
340 'ratingCount' => $game->rating->count,
342 'versionNumber' => $latestRelease->name,
343 'latestVersion' => $latestRelease->uuid,
344 'md5sum' => $latestRelease->md5sum,
345 'apkFileSize' => $latestRelease->size,
346 'publishedAt' => $latestRelease->date,
347 'publicSize' => $latestRelease->publicSize,
348 'nativeSize' => $latestRelease->nativeSize,
350 'mainImageFullUrl' => $game->discover,
351 'videoUrl' => getFirstVideoUrl($game->media),
352 'filepickerScreenshots' => getAllImageUrls($game->media),
353 'mobileAppIcon' => null,
355 'developer' => $game->developer->name,
356 'supportEmailAddress' => $game->developer->supportEmail,
357 'supportPhone' => $game->developer->supportPhone,
358 'founder' => $game->developer->founder,
360 'promotedProduct' => $product,
365 function buildAppDownload($game, $release)
369 'fileSize' => $release->size,
370 'version' => $release->uuid,
371 'contentRating' => $game->contentRating,
372 'downloadLink' => $release->url,
377 function buildProduct($product)
379 if ($product === null) {
383 'type' => $product->type ?? 'entitlement',
384 'identifier' => $product->identifier,
385 'name' => $product->name,
386 'description' => $product->description ?? '',
387 'localPrice' => $product->localPrice,
388 'originalPrice' => $product->originalPrice,
389 'priceInCents' => $product->originalPrice * 100,
391 'currency' => $product->currency,
396 * Build /app/v1/details?app=org.example.game
398 function buildDetails($game)
400 $latestRelease = $game->latestRelease;
403 if ($game->discover) {
407 'thumbnail' => $game->discover,
408 'full' => $game->discover,
412 foreach ($game->media as $medium) {
413 if ($medium->type == 'image') {
417 'thumbnail' => $medium->thumb,
418 'full' => $medium->url,
422 if (!isUnsupportedVideoUrl($medium->url)) {
425 'url' => $medium->url,
432 if (isset($game->links->unlocked)) {
434 'text' => 'Show unlocked',
435 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
441 $gamePromoted = getPromotedProduct($game);
443 $product = buildProduct($gamePromoted);
446 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
449 'title' => $game->title,
450 'description' => $game->description,
451 'gamerNumbers' => $game->players,
452 'genres' => $game->genres,
454 'suggestedAge' => $game->contentRating,
455 'premium' => $game->premium,
456 'inAppPurchases' => $game->inAppPurchases,
457 'firstPublishedAt' => strtotime($game->firstPublishedAt),
461 'count' => $game->rating->count,
462 'average' => $game->rating->average,
466 'fileSize' => $latestRelease->size,
467 'nativeSize' => $latestRelease->nativeSize,
468 'publicSize' => $latestRelease->publicSize,
469 'md5sum' => $latestRelease->md5sum,
470 'filename' => 'FIXME',
472 'package' => $game->packageName,
473 'versionCode' => $latestRelease->versionCode,
474 'state' => 'complete',
478 'number' => $latestRelease->name,
479 'publishedAt' => strtotime($latestRelease->date),
480 'uuid' => $latestRelease->uuid,
484 'name' => $game->developer->name,
485 'founder' => $game->developer->founder,
489 'key:rating.average',
490 'key:developer.name',
492 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
495 'tileImage' => $game->discover,
496 'mediaTiles' => $mediaTiles,
497 'mobileAppIcon' => null,
502 'promotedProduct' => $product,
503 'buttons' => $buttons,
507 function buildDeveloperCurrentGamer()
511 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
512 'username' => 'stouyapi',
518 * For /api/v1/developers/xxx/products/?only=yyy
520 function buildDeveloperProductOnly($product, $developer)
523 'developerName' => $developer->name,
524 'currency' => $product->currency,
526 buildProduct($product),
532 * For /api/v1/developers/xxx/products/
534 function buildDeveloperProducts($products, $developer)
537 foreach ($products as $product) {
538 $jsonProducts[] = buildProduct($product);
541 'developerName' => $developer->name,
542 'currency' => $products[0]->currency ?? 'EUR',
543 'products' => $jsonProducts,
547 function buildPurchases($game)
552 $promotedProduct = getPromotedProduct($game);
553 if ($promotedProduct) {
554 $purchasesData['purchases'][] = [
555 'purchaseDate' => time() * 1000,
556 'generateDate' => time() * 1000,
557 'identifier' => $promotedProduct->identifier,
558 'gamer' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
559 'uuid' => '00702342-0000-1111-2222-c3e1500beef3',//transaction ID
560 'priceInCents' => $promotedProduct->originalPrice * 100,
561 'localPrice' => $promotedProduct->localPrice,
562 'currency' => $promotedProduct->currency,
566 $encryptedOnce = dummyEncrypt($purchasesData);
567 $encryptedTwice = dummyEncrypt($encryptedOnce);
568 return $encryptedTwice;
571 function buildSearch($games)
573 $games = sortByTitle($games);
575 foreach ($games as $game) {
577 'title' => $game->title,
578 'url' => 'ouya://launcher/details?app=' . $game->packageName,
579 'contentRating' => $game->contentRating,
583 'count' => count($results),
584 'results' => $results,
588 function dummyEncrypt($data)
591 'key' => base64_encode('0123456789abcdef'),
592 'iv' => 't3jir1LHpICunvhlM76edQ==',//random bytes
593 'blob' => base64_encode(
594 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
599 function addChunkedDiscoverRows(&$data, $games, $title)
601 $chunks = array_chunk($games, 4);
603 foreach ($chunks as $chunk) {
605 $data, $first ? $title : '',
612 function addDiscoverRow(&$data, $title, $games)
620 foreach ($games as $game) {
621 if (is_string($game)) {
623 $tilePos = count($data['tiles']);
624 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
628 if (isset($game->links->original)) {
629 //do not link unlocked games.
630 // people an access them via the original games
633 $tilePos = findTile($data['tiles'], $game->packageName);
634 if ($tilePos === null) {
635 $tilePos = count($data['tiles']);
636 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
639 $row['tiles'][] = $tilePos;
641 $data['rows'][] = $row;
644 function findTile($tiles, $packageName)
646 foreach ($tiles as $pos => $tile) {
647 if ($tile['package'] == $packageName) {
654 function buildDiscoverCategoryTile($title)
657 'url' => 'ouya://launcher/discover/' . categoryPath($title),
664 function buildDiscoverGameTile($game)
666 $latestRelease = $game->latestRelease;
668 'gamerNumbers' => $game->players,
669 'genres' => $game->genres,
670 'url' => 'ouya://launcher/details?app=' . $game->packageName,
673 'md5sum' => $latestRelease->md5sum,
675 'versionNumber' => $latestRelease->name,
676 'uuid' => $latestRelease->uuid,
678 'inAppPurchases' => $game->inAppPurchases,
679 'promotedProduct' => null,
680 'premium' => $game->premium,
682 'package' => $game->packageName,
683 'updated_at' => strtotime($latestRelease->date),
684 'updatedAt' => $latestRelease->date,
685 'title' => $game->title,
686 'image' => $game->discover,
687 'contentRating' => $game->contentRating,
689 'count' => $game->rating->count,
690 'average' => $game->rating->average,
692 'promotedProduct' => buildProduct(getPromotedProduct($game)),
696 function categoryPath($title)
698 return str_replace(['/', '\\', ' ', '+', '?'], '_', $title);
701 function getAllAges($games)
704 foreach ($games as $game) {
705 $ages[] = $game->contentRating;
707 return array_unique($ages);
710 function getAllGenres($games)
713 foreach ($games as $game) {
714 $genres = array_merge($genres, $game->genres);
716 return array_unique($genres);
719 function addMissingGameProperties($game)
721 if (!isset($game->overview)) {
722 $game->overview = null;
724 if (!isset($game->description)) {
725 $game->description = '';
727 if (!isset($game->players)) {
728 $game->players = [1];
730 if (!isset($game->genres)) {
731 $game->genres = ['Unsorted'];
733 if (!isset($game->website)) {
734 $game->website = null;
736 if (!isset($game->contentRating)) {
737 $game->contentRating = 'Everyone';
739 if (!isset($game->premium)) {
740 $game->premium = false;
742 if (!isset($game->firstPublishedAt)) {
743 $game->firstPublishedAt = gmdate('c');
746 if (!isset($game->rating)) {
747 $game->rating = new stdClass();
749 if (!isset($game->rating->likeCount)) {
750 $game->rating->likeCount = 0;
752 if (!isset($game->rating->average)) {
753 $game->rating->average = 0;
755 if (!isset($game->rating->count)) {
756 $game->rating->count = 0;
759 $game->latestRelease = null;
760 $latestReleaseTimestamp = 0;
761 foreach ($game->releases as $release) {
762 if (!isset($release->publicSize)) {
763 $release->publicSize = 0;
765 if (!isset($release->nativeSize)) {
766 $release->nativeSize = 0;
769 $releaseTimestamp = strtotime($release->date);
770 if ($releaseTimestamp > $latestReleaseTimestamp) {
771 $game->latestRelease = $release;
772 $latestReleaseTimestamp = $releaseTimestamp;
775 if ($game->latestRelease === null) {
776 error('No latest release for ' . $game->packageName);
779 if (!isset($game->media)) {
783 if (!isset($game->developer->uuid)) {
784 $game->developer->uuid = null;
786 if (!isset($game->developer->name)) {
787 $game->developer->name = 'unknown';
789 if (!isset($game->developer->supportEmail)) {
790 $game->developer->supportEmail = null;
792 if (!isset($game->developer->supportPhone)) {
793 $game->developer->supportPhone = null;
795 if (!isset($game->developer->founder)) {
796 $game->developer->founder = false;
801 * Implements a sensible ranking system described in
802 * https://stackoverflow.com/a/1411268/2826013
804 function calculateRank(array $games)
806 $averageRatings = array_map(
808 return $game->rating->average;
812 $average = array_sum($averageRatings) / count($averageRatings);
816 foreach ($games as $game) {
817 $R = $game->rating->average;
818 $v = $game->rating->count;
819 $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
823 function getFirstVideoUrl($media)
825 foreach ($media as $medium) {
826 if ($medium->type == 'video') {
833 function getAllImageUrls($media)
836 foreach ($media as $medium) {
837 if ($medium->type == 'image') {
838 $imageUrls[] = $medium->url;
844 function getPromotedProduct($game)
846 if (!isset($game->products) || !count($game->products)) {
849 foreach ($game->products as $gameProd) {
850 if ($gameProd->promoted) {
858 * vimeo only work with HTTPS now,
859 * and the OUYA does not support SNI.
860 * We get SSL errors and no video for them :/
862 function isUnsupportedVideoUrl($url)
864 return strpos($url, '://vimeo.com/') !== false;
867 function removeMakeGames(array $games)
869 return filterByGenre($games, 'Tutorials', true);
872 function removeMakeGenres($genres)
875 foreach ($genres as $genre) {
876 if ($genre != 'Tutorials' && $genre != 'Builds') {
877 $filtered[] = $genre;
883 function writeJson($path, $data)
886 $fullPath = $wwwDir . $path;
887 $dir = dirname($fullPath);
889 mkdir($dir, 0777, true);
893 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
899 fwrite(STDERR, $msg . "\n");