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__ . '/functions.php';
11 require_once __DIR__ . '/filters.php';
12 if (!isset($argv[1])) {
13 error('Pass the path to a "folders" file with game data json files folder names');
15 $foldersFile = $argv[1];
16 if (!is_file($foldersFile)) {
17 error('Given path is not a file: ' . $foldersFile);
20 //default configuration values
21 $GLOBALS['packagelists'] = [];
22 $GLOBALS['urlRewrites'] = [];
23 $cfgFile = __DIR__ . '/../config.php';
24 if (file_exists($cfgFile)) {
28 $wwwDir = __DIR__ . '/../www/';
30 $qrDir = $wwwDir . 'gen-qr/';
31 if (!is_dir($qrDir)) {
35 $baseDir = dirname($foldersFile);
37 foreach (file($foldersFile) as $line) {
40 if (strpos($line, '..') !== false) {
41 error('Path attack in ' . $folder);
43 $folder = $baseDir . '/' . $line;
44 if (!is_dir($folder)) {
45 error('Folder does not exist: ' . $folder);
47 $gameFiles = array_merge($gameFiles, glob($folder . '/*.json'));
55 //load game data. doing early to collect a developer's games
56 foreach ($gameFiles as $gameFile) {
57 $game = json_decode(file_get_contents($gameFile));
59 error('JSON invalid at ' . $gameFile);
61 addMissingGameProperties($game);
62 $games[$game->packageName] = $game;
64 if (!isset($developers[$game->developer->uuid])) {
65 $developers[$game->developer->uuid] = [
66 'info' => $game->developer,
71 $developers[$game->developer->uuid]['gameNames'][] = $game->packageName;
74 //write json api files
75 foreach ($games as $game) {
76 $products = $game->products ?? [];
77 foreach ($products as $product) {
79 'api/v1/developers/' . $game->developer->uuid
80 . '/products/' . $product->identifier . '.json',
81 buildDeveloperProductOnly($product, $game->developer)
83 $developers[$game->developer->uuid]['products'][] = $product;
87 'api/v1/details-data/' . $game->packageName . '.json',
90 count($developers[$game->developer->uuid]['gameNames']) > 1
95 'api/v1/games/' . $game->packageName . '/purchases',
100 'api/v1/apps/' . $game->packageName . '.json',
103 $latestRelease = $game->latestRelease;
105 'api/v1/apps/' . $latestRelease->uuid . '.json',
110 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
111 buildAppDownload($game, $latestRelease)
119 calculateRank($games);
121 foreach ($developers as $developer) {
123 //index.htm does not need a rewrite rule
124 'api/v1/developers/' . $developer['info']->uuid
125 . '/products/index.htm',
126 buildDeveloperProducts($developer['products'], $developer['info'])
129 'api/v1/developers/' . $developer['info']->uuid
131 buildDeveloperCurrentGamer()
134 if (count($developer['gameNames']) > 1) {
136 'api/v1/discover-data/dev--' . $developer['info']->uuid . '.json',
137 buildSpecialCategory(
138 'Developer: ' . $developer['info']->name,
139 filterByPackageNames($games, $developer['gameNames'])
145 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
146 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
150 'api/v1/discover-data/tutorials.json',
151 buildMakeCategory('Tutorials', filterByGenre($games, 'Tutorials'))
154 $searchLetters = 'abcdefghijklmnopqrstuvwxyz0123456789., ';
155 foreach (str_split($searchLetters) as $letter) {
156 $letterGames = filterBySearchWord($games, $letter);
158 'api/v1/search-data/' . $letter . '.json',
159 buildSearch($letterGames)
164 function buildDiscover(array $games)
166 $games = removeMakeGames($games);
168 'title' => 'DISCOVER',
175 filterLastAdded($games, 10)
179 filterBestRated($games, 10),
183 foreach ($GLOBALS['packagelists'] as $listTitle => $listPackageNames) {
186 filterByPackageNames($games, $listPackageNames)
200 'api/v1/discover-data/' . categoryPath('Best rated') . '.json',
201 buildSpecialCategory('Best rated', filterBestRated($games, 99))
204 'api/v1/discover-data/' . categoryPath('Most rated') . '.json',
205 buildSpecialCategory('Most rated', filterMostDownloaded($games, 99))
208 'api/v1/discover-data/' . categoryPath('Random') . '.json',
209 buildSpecialCategory(
210 'Random ' . date('Y-m-d H:i'),
211 filterRandom($games, 99)
215 'api/v1/discover-data/' . categoryPath('Last updated') . '.json',
216 buildSpecialCategory('Last updated', filterLastUpdated($games, 99))
225 addDiscoverRow($data, 'Multiplayer', $players);
226 foreach ($players as $num => $title) {
228 'api/v1/discover-data/' . categoryPath($title) . '.json',
229 buildDiscoverCategory(
231 //I do not want emulators here,
232 // and neither Streaming apps
235 filterByPlayers($games, $num),
244 $ages = getAllAges($games);
246 addDiscoverRow($data, 'Content rating', $ages);
247 foreach ($ages as $num => $title) {
249 'api/v1/discover-data/' . categoryPath($title) . '.json',
250 buildDiscoverCategory($title, filterByAge($games, $title))
254 $genres = removeMakeGenres(getAllGenres($games));
256 addChunkedDiscoverRows($data, $genres, 'Genres');
258 foreach ($genres as $genre) {
260 'api/v1/discover-data/' . categoryPath($genre) . '.json',
261 buildDiscoverCategory($genre, filterByGenre($games, $genre))
265 $abc = array_merge(range('A', 'Z'), ['Other']);
266 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
267 foreach ($abc as $letter) {
269 'api/v1/discover-data/' . categoryPath($letter) . '.json',
270 buildDiscoverCategory($letter, filterByLetter($games, $letter))
278 * A genre category page
280 function buildDiscoverCategory($name, $games)
288 $data, 'Last Updated',
289 filterLastUpdated($games, 10)
293 filterBestRated($games, 10),
297 $games = sortByTitle($games);
298 $chunks = array_chunk($games, 4);
299 foreach ($chunks as $chunkGames) {
300 addDiscoverRow($data, '', $chunkGames);
306 function buildMakeCategory($name, $games)
314 $games = sortByTitle($games);
315 addDiscoverRow($data, '', $games);
321 * Category without the "Last updated" or "Best rated" top rows
323 * Used for "Best rated", "Most rated", "Random"
325 function buildSpecialCategory($name, $games)
333 $first3 = array_slice($games, 0, 3);
334 $chunks = array_chunk(array_slice($games, 3), 4);
335 array_unshift($chunks, $first3);
337 foreach ($chunks as $chunkGames) {
338 addDiscoverRow($data, '', $chunkGames);
344 function buildDiscoverHome(array $games)
353 if (isset($GLOBALS['home'])) {
354 reset($GLOBALS['home']);
355 $title = key($GLOBALS['home']);
358 filterByPackageNames($games, $GLOBALS['home'][$title])
362 'title' => 'FEATURED',
363 'showPrice' => false,
373 * Build api/v1/apps/$packageName
375 function buildApps($game)
377 $latestRelease = $game->latestRelease;
380 $gamePromoted = getPromotedProduct($game);
382 $product = buildProduct($gamePromoted);
385 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
388 'uuid' => $latestRelease->uuid,
389 'title' => $game->title,
390 'overview' => $game->overview,
391 'description' => $game->description,
392 'gamerNumbers' => $game->players,
393 'genres' => $game->genres,
395 'website' => $game->website,
396 'contentRating' => $game->contentRating,
397 'premium' => $game->premium,
398 'firstPublishedAt' => $game->firstPublishedAt,
400 'likeCount' => $game->rating->likeCount,
401 'ratingAverage' => $game->rating->average,
402 'ratingCount' => $game->rating->count,
404 'versionNumber' => $latestRelease->name,
405 'latestVersion' => $latestRelease->uuid,
406 'md5sum' => $latestRelease->md5sum,
407 'apkFileSize' => $latestRelease->size,
408 'publishedAt' => $latestRelease->date,
409 'publicSize' => $latestRelease->publicSize,
410 'nativeSize' => $latestRelease->nativeSize,
412 'mainImageFullUrl' => $game->discover,
413 'videoUrl' => getFirstVideoUrl($game->media),
414 'filepickerScreenshots' => getAllImageUrls($game->media),
415 'mobileAppIcon' => null,
417 'developer' => $game->developer->name,
418 'supportEmailAddress' => $game->developer->supportEmail,
419 'supportPhone' => $game->developer->supportPhone,
420 'founder' => $game->developer->founder,
422 'promotedProduct' => $product,
427 function buildAppDownload($game, $release)
431 'fileSize' => $release->size,
432 'version' => $release->uuid,
433 'contentRating' => $game->contentRating,
434 'downloadLink' => $release->url,
439 function buildProduct($product)
441 if ($product === null) {
445 'type' => $product->type ?? 'entitlement',
446 'identifier' => $product->identifier,
447 'name' => $product->name,
448 'description' => $product->description ?? '',
449 'localPrice' => $product->localPrice,
450 'originalPrice' => $product->originalPrice,
451 'priceInCents' => $product->originalPrice * 100,
453 'currency' => $product->currency,
458 * Build /app/v1/details?app=org.example.game
460 function buildDetails($game, $linkDeveloperPage = false)
462 $latestRelease = $game->latestRelease;
465 if ($game->discover) {
469 'thumbnail' => $game->discover,
470 'full' => $game->discover,
474 foreach ($game->media as $medium) {
475 if ($medium->type == 'image') {
479 'thumbnail' => $medium->thumb ?? $medium->url,
480 'full' => $medium->url,
484 if (!isUnsupportedVideoUrl($medium->url)) {
487 'url' => $medium->url,
494 if (isset($game->links->unlocked)) {
496 'text' => 'Show unlocked',
497 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
503 $gamePromoted = getPromotedProduct($game);
505 $product = buildProduct($gamePromoted);
509 if (isset($game->latestRelease->url)
510 && substr($game->latestRelease->url, 0, 29) == 'https://archive.org/download/'
512 $iaUrl = dirname($game->latestRelease->url) . '/';
515 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
518 'title' => $game->title,
519 'description' => $game->description,
520 'gamerNumbers' => $game->players,
521 'genres' => $game->genres,
523 'suggestedAge' => $game->contentRating,
524 'premium' => $game->premium,
525 'inAppPurchases' => $game->inAppPurchases,
526 'firstPublishedAt' => strtotime($game->firstPublishedAt),
530 'count' => $game->rating->count,
531 'average' => $game->rating->average,
535 'fileSize' => $latestRelease->size,
536 'nativeSize' => $latestRelease->nativeSize,
537 'publicSize' => $latestRelease->publicSize,
538 'md5sum' => $latestRelease->md5sum,
539 'filename' => 'FIXME',
541 'package' => $game->packageName,
542 'versionCode' => $latestRelease->versionCode,
543 'state' => 'complete',
547 'number' => $latestRelease->name,
548 'publishedAt' => strtotime($latestRelease->date),
549 'uuid' => $latestRelease->uuid,
553 'name' => $game->developer->name,
554 'founder' => $game->developer->founder,
558 'key:rating.average',
559 'key:developer.name',
561 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
564 'tileImage' => $game->discover,
565 'mediaTiles' => $mediaTiles,
566 'mobileAppIcon' => null,
571 'promotedProduct' => $product,
572 'buttons' => $buttons,
575 'internet-archive' => $iaUrl,
576 'developer-url' => $game->developer->website ?? null,
580 if ($linkDeveloperPage) {
581 $data['developer']['url'] = 'ouya://launcher/discover/dev--'
582 . categoryPath($game->developer->uuid);
588 function buildDeveloperCurrentGamer()
592 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
593 'username' => 'stouyapi',
599 * For /api/v1/developers/xxx/products/?only=yyy
601 function buildDeveloperProductOnly($product, $developer)
604 'developerName' => $developer->name,
605 'currency' => $product->currency,
607 buildProduct($product),
613 * For /api/v1/developers/xxx/products/
615 function buildDeveloperProducts($products, $developer)
618 $products = array_values(array_column($products, null, 'identifier'));
621 foreach ($products as $product) {
622 $jsonProducts[] = buildProduct($product);
625 'developerName' => $developer->name,
626 'currency' => $products[0]->currency ?? 'EUR',
627 'products' => $jsonProducts,
631 function buildPurchases($game)
636 $promotedProduct = getPromotedProduct($game);
637 if ($promotedProduct) {
638 $purchasesData['purchases'][] = [
639 'purchaseDate' => time() * 1000,
640 'generateDate' => time() * 1000,
641 'identifier' => $promotedProduct->identifier,
642 'gamer' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
643 'uuid' => '00702342-0000-1111-2222-c3e1500beef3',//transaction ID
644 'priceInCents' => $promotedProduct->originalPrice * 100,
645 'localPrice' => $promotedProduct->localPrice,
646 'currency' => $promotedProduct->currency,
650 $encryptedOnce = dummyEncrypt($purchasesData);
651 $encryptedTwice = dummyEncrypt($encryptedOnce);
652 return $encryptedTwice;
655 function buildSearch($games)
657 $games = sortByTitle($games);
659 foreach ($games as $game) {
661 'title' => $game->title,
662 'url' => 'ouya://launcher/details?app=' . $game->packageName,
663 'contentRating' => $game->contentRating,
667 'count' => count($results),
668 'results' => $results,
672 function dummyEncrypt($data)
675 'key' => base64_encode('0123456789abcdef'),
676 'iv' => 't3jir1LHpICunvhlM76edQ==',//random bytes
677 'blob' => base64_encode(
678 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
683 function addChunkedDiscoverRows(&$data, $games, $title)
685 $chunks = array_chunk($games, 4);
687 foreach ($chunks as $chunk) {
689 $data, $first ? $title : '',
696 function addDiscoverRow(&$data, $title, $games, $ranked = false)
704 foreach ($games as $game) {
705 if (is_string($game)) {
707 $tilePos = count($data['tiles']);
708 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
712 if (isset($game->links->original)) {
713 //do not link unlocked games.
714 // people an access them via the original games
717 $tilePos = findTile($data['tiles'], $game->packageName);
718 if ($tilePos === null) {
719 $tilePos = count($data['tiles']);
720 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
723 $row['tiles'][] = $tilePos;
725 $data['rows'][] = $row;
728 function findTile($tiles, $packageName)
730 foreach ($tiles as $pos => $tile) {
731 if ($tile['package'] == $packageName) {
738 function buildDiscoverCategoryTile($title)
741 'url' => 'ouya://launcher/discover/' . categoryPath($title),
748 function buildDiscoverGameTile($game)
750 $latestRelease = $game->latestRelease;
752 'gamerNumbers' => $game->players,
753 'genres' => $game->genres,
754 'url' => 'ouya://launcher/details?app=' . $game->packageName,
757 'md5sum' => $latestRelease->md5sum,
759 'versionNumber' => $latestRelease->name,
760 'uuid' => $latestRelease->uuid,
762 'inAppPurchases' => $game->inAppPurchases,
763 'promotedProduct' => null,
764 'premium' => $game->premium,
766 'package' => $game->packageName,
767 'updated_at' => strtotime($latestRelease->date),
768 'updatedAt' => $latestRelease->date,
769 'title' => $game->title,
770 'image' => $game->discover,
771 'contentRating' => $game->contentRating,
773 'count' => $game->rating->count,
774 'average' => $game->rating->average,
776 'promotedProduct' => buildProduct(getPromotedProduct($game)),
780 function getAllAges($games)
783 foreach ($games as $game) {
784 $ages[] = $game->contentRating;
786 return array_unique($ages);
789 function getAllGenres($games)
792 foreach ($games as $game) {
793 $genres = array_merge($genres, $game->genres);
795 return array_unique($genres);
798 function addMissingGameProperties($game)
800 if (!isset($game->overview)) {
801 $game->overview = null;
803 if (!isset($game->description)) {
804 $game->description = '';
806 if (!isset($game->players)) {
807 $game->players = [1];
809 if (!isset($game->genres)) {
810 $game->genres = ['Unsorted'];
812 if (!isset($game->website)) {
813 $game->website = null;
815 if (!isset($game->contentRating)) {
816 $game->contentRating = 'Everyone';
818 if (!isset($game->premium)) {
819 $game->premium = false;
821 if (!isset($game->firstPublishedAt)) {
822 $game->firstPublishedAt = gmdate('c');
825 if (!isset($game->rating)) {
826 $game->rating = new stdClass();
828 if (!isset($game->rating->likeCount)) {
829 $game->rating->likeCount = 0;
831 if (!isset($game->rating->average)) {
832 $game->rating->average = 0;
834 if (!isset($game->rating->count)) {
835 $game->rating->count = 0;
838 $game->firstRelease = null;
839 $game->latestRelease = null;
840 $firstReleaseTimestamp = null;
841 $latestReleaseTimestamp = 0;
842 foreach ($game->releases as $release) {
843 if (!isset($release->publicSize)) {
844 $release->publicSize = 0;
846 if (!isset($release->nativeSize)) {
847 $release->nativeSize = 0;
850 $releaseTimestamp = strtotime($release->date);
851 if ($releaseTimestamp > $latestReleaseTimestamp) {
852 $game->latestRelease = $release;
853 $latestReleaseTimestamp = $releaseTimestamp;
855 if ($firstReleaseTimestamp === null
856 || $releaseTimestamp < $firstReleaseTimestamp
858 $game->firstRelease = $release;
859 $firstReleaseTimestamp = $releaseTimestamp;
862 if ($game->firstRelease === null) {
863 error('No first release for ' . $game->packageName);
865 if ($game->latestRelease === null) {
866 error('No latest release for ' . $game->packageName);
869 if (!isset($game->media)) {
873 if (!isset($game->developer->uuid)) {
874 $game->developer->uuid = null;
876 if (!isset($game->developer->name)) {
877 $game->developer->name = 'unknown';
879 if (!isset($game->developer->supportEmail)) {
880 $game->developer->supportEmail = null;
882 if (!isset($game->developer->supportPhone)) {
883 $game->developer->supportPhone = null;
885 if (!isset($game->developer->founder)) {
886 $game->developer->founder = false;
889 if ($game->website) {
890 $qrfileName = preg_replace('#[^\\w\\d._-]#', '_', $game->website) . '.png';
891 $qrfilePath = $GLOBALS['qrDir'] . $qrfileName;
892 if (!file_exists($qrfilePath)) {
893 $cmd = __DIR__ . '/create-qr.sh'
894 . ' ' . escapeshellarg($game->website)
895 . ' ' . escapeshellarg($qrfilePath);
896 passthru($cmd, $retval);
901 $qrUrlPath = $GLOBALS['baseUrl'] . 'gen-qr/' . $qrfileName;
902 $game->media[] = (object) [
908 //rewrite urls from Internet Archive to our servers
909 $game->discover = rewriteUrl($game->discover);
910 foreach ($game->media as $medium) {
911 $medium->url = rewriteUrl($medium->url);
913 foreach ($game->releases as $release) {
914 $release->url = rewriteUrl($release->url);
919 * Implements a sensible ranking system described in
920 * https://stackoverflow.com/a/1411268/2826013
922 function calculateRank(array $games)
924 $averageRatings = array_map(
926 return $game->rating->average;
930 $average = array_sum($averageRatings) / count($averageRatings);
934 foreach ($games as $game) {
935 $R = $game->rating->average;
936 $v = $game->rating->count;
937 $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
941 function getFirstVideoUrl($media)
943 foreach ($media as $medium) {
944 if ($medium->type == 'video') {
951 function getAllImageUrls($media)
954 foreach ($media as $medium) {
955 if ($medium->type == 'image') {
956 $imageUrls[] = $medium->url;
962 function getPromotedProduct($game)
964 if (!isset($game->products) || !count($game->products)) {
967 foreach ($game->products as $gameProd) {
968 if ($gameProd->promoted) {
976 * vimeo only work with HTTPS now,
977 * and the OUYA does not support SNI.
978 * We get SSL errors and no video for them :/
980 function isUnsupportedVideoUrl($url)
982 return strpos($url, '://vimeo.com/') !== false;
985 function removeMakeGames(array $games)
987 return filterByGenre($games, 'Tutorials', true);
990 function removeMakeGenres($genres)
993 foreach ($genres as $genre) {
994 if ($genre != 'Tutorials' && $genre != 'Builds') {
995 $filtered[] = $genre;
1001 function rewriteUrl($url)
1003 foreach ($GLOBALS['urlRewrites'] as $pattern => $replacement) {
1004 $url = preg_replace($pattern, $replacement, $url);
1009 function writeJson($path, $data)
1012 $fullPath = $wwwDir . $path;
1013 $dir = dirname($fullPath);
1014 if (!is_dir($dir)) {
1015 mkdir($dir, 0777, true);
1019 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
1023 function error($msg)
1025 fwrite(STDERR, $msg . "\n");