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 //default configuration values
20 $GLOBALS['packagelists'] = [];
21 $cfgFile = __DIR__ . '/../config.php';
22 if (file_exists($cfgFile)) {
26 $wwwDir = __DIR__ . '/../www/';
28 $baseDir = dirname($foldersFile);
30 foreach (file($foldersFile) as $line) {
33 if (strpos($line, '..') !== false) {
34 error('Path attack in ' . $folder);
36 $folder = $baseDir . '/' . $line;
37 if (!is_dir($folder)) {
38 error('Folder does not exist: ' . $folder);
40 $gameFiles = array_merge($gameFiles, glob($folder . '/*.json'));
47 foreach ($gameFiles as $gameFile) {
48 $game = json_decode(file_get_contents($gameFile));
50 error('JSON invalid at ' . $gameFile);
52 addMissingGameProperties($game);
53 $games[$game->packageName] = $game;
56 'api/v1/details-data/' . $game->packageName . '.json',
60 if (!isset($developers[$game->developer->uuid])) {
61 $developers[$game->developer->uuid] = [
62 'info' => $game->developer,
67 $products = $game->products ?? [];
68 foreach ($products as $product) {
70 'api/v1/developers/' . $game->developer->uuid
71 . '/products/' . $product->identifier . '.json',
72 buildDeveloperProductOnly($product, $game->developer)
74 $developers[$game->developer->uuid]['products'][] = $product;
79 'api/v1/games/' . $game->packageName . '/purchases',
84 'api/v1/apps/' . $game->packageName . '.json',
87 $latestRelease = $game->latestRelease;
89 'api/v1/apps/' . $latestRelease->uuid . '.json',
94 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
95 buildAppDownload($game, $latestRelease)
103 calculateRank($games);
105 foreach ($developers as $developer) {
107 //index.htm does not need a rewrite rule
108 'api/v1/developers/' . $developer['info']->uuid
109 . '/products/index.htm',
110 buildDeveloperProducts($developer['products'], $developer['info'])
113 //index.htm does not need a rewrite rule
114 'api/v1/developers/' . $developer['info']->uuid
116 buildDeveloperCurrentGamer()
120 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
121 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
125 'api/v1/discover-data/tutorials.json',
126 buildMakeCategory('Tutorials', filterByGenre($games, 'Tutorials'))
129 $searchLetters = 'abcdefghijklmnopqrstuvwxyz0123456789., ';
130 foreach (str_split($searchLetters) as $letter) {
131 $letterGames = filterBySearchWord($games, $letter);
133 'api/v1/search-data/' . $letter . '.json',
134 buildSearch($letterGames)
139 function buildDiscover(array $games)
141 $games = removeMakeGames($games);
143 'title' => 'DISCOVER',
149 $data, 'Last Updated',
150 filterLastUpdated($games, 10)
154 filterBestRated($games, 10)
157 foreach ($GLOBALS['packagelists'] as $listTitle => $listPackageNames) {
160 filterByPackageNames($games, $listPackageNames)
173 'api/v1/discover-data/' . categoryPath('Best rated') . '.json',
174 buildSpecialCategory('Best rated', filterBestRated($games, 99))
177 'api/v1/discover-data/' . categoryPath('Most rated') . '.json',
178 buildSpecialCategory('Most rated', filterMostDownloaded($games, 99))
181 'api/v1/discover-data/' . categoryPath('Random') . '.json',
182 buildSpecialCategory(
183 'Random ' . date('Y-m-d H:i'),
184 filterRandom($games, 99)
194 addDiscoverRow($data, 'Multiplayer', $players);
195 foreach ($players as $num => $title) {
197 'api/v1/discover-data/' . categoryPath($title) . '.json',
198 buildDiscoverCategory($title, filterByPlayers($games, $num))
202 $ages = getAllAges($games);
204 addDiscoverRow($data, 'Content rating', $ages);
205 foreach ($ages as $num => $title) {
207 'api/v1/discover-data/' . categoryPath($title) . '.json',
208 buildDiscoverCategory($title, filterByAge($games, $title))
212 $genres = removeMakeGenres(getAllGenres($games));
214 addChunkedDiscoverRows($data, $genres, 'Genres');
216 foreach ($genres as $genre) {
218 'api/v1/discover-data/' . categoryPath($genre) . '.json',
219 buildDiscoverCategory($genre, filterByGenre($games, $genre))
223 $abc = array_merge(range('A', 'Z'), ['Other']);
224 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
225 foreach ($abc as $letter) {
227 'api/v1/discover-data/' . categoryPath($letter) . '.json',
228 buildDiscoverCategory($letter, filterByLetter($games, $letter))
236 * A genre category page
238 function buildDiscoverCategory($name, $games)
246 $data, 'Last Updated',
247 filterLastUpdated($games, 10)
251 filterBestRated($games, 10)
254 $games = sortByTitle($games);
255 $chunks = array_chunk($games, 4);
256 foreach ($chunks as $chunkGames) {
257 addDiscoverRow($data, '', $chunkGames);
263 function buildMakeCategory($name, $games)
271 $games = sortByTitle($games);
272 addDiscoverRow($data, '', $games);
277 function buildSpecialCategory($name, $games)
285 $first3 = array_slice($games, 0, 3);
286 $chunks = array_chunk(array_slice($games, 3), 4);
287 array_unshift($chunks, $first3);
289 foreach ($chunks as $chunkGames) {
290 addDiscoverRow($data, '', $chunkGames);
296 function buildDiscoverHome(array $games)
298 //we do not want anything here for now
303 'title' => 'FEATURED',
304 'showPrice' => false,
315 * Build api/v1/apps/$packageName
317 function buildApps($game)
319 $latestRelease = $game->latestRelease;
322 $gamePromoted = getPromotedProduct($game);
324 $product = buildProduct($gamePromoted);
327 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
330 'uuid' => $latestRelease->uuid,
331 'title' => $game->title,
332 'overview' => $game->overview,
333 'description' => $game->description,
334 'gamerNumbers' => $game->players,
335 'genres' => $game->genres,
337 'website' => $game->website,
338 'contentRating' => $game->contentRating,
339 'premium' => $game->premium,
340 'firstPublishedAt' => $game->firstPublishedAt,
342 'likeCount' => $game->rating->likeCount,
343 'ratingAverage' => $game->rating->average,
344 'ratingCount' => $game->rating->count,
346 'versionNumber' => $latestRelease->name,
347 'latestVersion' => $latestRelease->uuid,
348 'md5sum' => $latestRelease->md5sum,
349 'apkFileSize' => $latestRelease->size,
350 'publishedAt' => $latestRelease->date,
351 'publicSize' => $latestRelease->publicSize,
352 'nativeSize' => $latestRelease->nativeSize,
354 'mainImageFullUrl' => $game->discover,
355 'videoUrl' => getFirstVideoUrl($game->media),
356 'filepickerScreenshots' => getAllImageUrls($game->media),
357 'mobileAppIcon' => null,
359 'developer' => $game->developer->name,
360 'supportEmailAddress' => $game->developer->supportEmail,
361 'supportPhone' => $game->developer->supportPhone,
362 'founder' => $game->developer->founder,
364 'promotedProduct' => $product,
369 function buildAppDownload($game, $release)
373 'fileSize' => $release->size,
374 'version' => $release->uuid,
375 'contentRating' => $game->contentRating,
376 'downloadLink' => $release->url,
381 function buildProduct($product)
383 if ($product === null) {
387 'type' => $product->type ?? 'entitlement',
388 'identifier' => $product->identifier,
389 'name' => $product->name,
390 'description' => $product->description ?? '',
391 'localPrice' => $product->localPrice,
392 'originalPrice' => $product->originalPrice,
393 'priceInCents' => $product->originalPrice * 100,
395 'currency' => $product->currency,
400 * Build /app/v1/details?app=org.example.game
402 function buildDetails($game)
404 $latestRelease = $game->latestRelease;
407 if ($game->discover) {
411 'thumbnail' => $game->discover,
412 'full' => $game->discover,
416 foreach ($game->media as $medium) {
417 if ($medium->type == 'image') {
421 'thumbnail' => $medium->thumb,
422 'full' => $medium->url,
426 if (!isUnsupportedVideoUrl($medium->url)) {
429 'url' => $medium->url,
436 if (isset($game->links->unlocked)) {
438 'text' => 'Show unlocked',
439 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
445 $gamePromoted = getPromotedProduct($game);
447 $product = buildProduct($gamePromoted);
450 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
453 'title' => $game->title,
454 'description' => $game->description,
455 'gamerNumbers' => $game->players,
456 'genres' => $game->genres,
458 'suggestedAge' => $game->contentRating,
459 'premium' => $game->premium,
460 'inAppPurchases' => $game->inAppPurchases,
461 'firstPublishedAt' => strtotime($game->firstPublishedAt),
465 'count' => $game->rating->count,
466 'average' => $game->rating->average,
470 'fileSize' => $latestRelease->size,
471 'nativeSize' => $latestRelease->nativeSize,
472 'publicSize' => $latestRelease->publicSize,
473 'md5sum' => $latestRelease->md5sum,
474 'filename' => 'FIXME',
476 'package' => $game->packageName,
477 'versionCode' => $latestRelease->versionCode,
478 'state' => 'complete',
482 'number' => $latestRelease->name,
483 'publishedAt' => strtotime($latestRelease->date),
484 'uuid' => $latestRelease->uuid,
488 'name' => $game->developer->name,
489 'founder' => $game->developer->founder,
493 'key:rating.average',
494 'key:developer.name',
496 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
499 'tileImage' => $game->discover,
500 'mediaTiles' => $mediaTiles,
501 'mobileAppIcon' => null,
506 'promotedProduct' => $product,
507 'buttons' => $buttons,
511 function buildDeveloperCurrentGamer()
515 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
516 'username' => 'stouyapi',
522 * For /api/v1/developers/xxx/products/?only=yyy
524 function buildDeveloperProductOnly($product, $developer)
527 'developerName' => $developer->name,
528 'currency' => $product->currency,
530 buildProduct($product),
536 * For /api/v1/developers/xxx/products/
538 function buildDeveloperProducts($products, $developer)
541 foreach ($products as $product) {
542 $jsonProducts[] = buildProduct($product);
545 'developerName' => $developer->name,
546 'currency' => $products[0]->currency ?? 'EUR',
547 'products' => $jsonProducts,
551 function buildPurchases($game)
556 $promotedProduct = getPromotedProduct($game);
557 if ($promotedProduct) {
558 $purchasesData['purchases'][] = [
559 'purchaseDate' => time() * 1000,
560 'generateDate' => time() * 1000,
561 'identifier' => $promotedProduct->identifier,
562 'gamer' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
563 'uuid' => '00702342-0000-1111-2222-c3e1500beef3',//transaction ID
564 'priceInCents' => $promotedProduct->originalPrice * 100,
565 'localPrice' => $promotedProduct->localPrice,
566 'currency' => $promotedProduct->currency,
570 $encryptedOnce = dummyEncrypt($purchasesData);
571 $encryptedTwice = dummyEncrypt($encryptedOnce);
572 return $encryptedTwice;
575 function buildSearch($games)
577 $games = sortByTitle($games);
579 foreach ($games as $game) {
581 'title' => $game->title,
582 'url' => 'ouya://launcher/details?app=' . $game->packageName,
583 'contentRating' => $game->contentRating,
587 'count' => count($results),
588 'results' => $results,
592 function dummyEncrypt($data)
595 'key' => base64_encode('0123456789abcdef'),
596 'iv' => 't3jir1LHpICunvhlM76edQ==',//random bytes
597 'blob' => base64_encode(
598 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
603 function addChunkedDiscoverRows(&$data, $games, $title)
605 $chunks = array_chunk($games, 4);
607 foreach ($chunks as $chunk) {
609 $data, $first ? $title : '',
616 function addDiscoverRow(&$data, $title, $games)
624 foreach ($games as $game) {
625 if (is_string($game)) {
627 $tilePos = count($data['tiles']);
628 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
632 if (isset($game->links->original)) {
633 //do not link unlocked games.
634 // people an access them via the original games
637 $tilePos = findTile($data['tiles'], $game->packageName);
638 if ($tilePos === null) {
639 $tilePos = count($data['tiles']);
640 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
643 $row['tiles'][] = $tilePos;
645 $data['rows'][] = $row;
648 function findTile($tiles, $packageName)
650 foreach ($tiles as $pos => $tile) {
651 if ($tile['package'] == $packageName) {
658 function buildDiscoverCategoryTile($title)
661 'url' => 'ouya://launcher/discover/' . categoryPath($title),
668 function buildDiscoverGameTile($game)
670 $latestRelease = $game->latestRelease;
672 'gamerNumbers' => $game->players,
673 'genres' => $game->genres,
674 'url' => 'ouya://launcher/details?app=' . $game->packageName,
677 'md5sum' => $latestRelease->md5sum,
679 'versionNumber' => $latestRelease->name,
680 'uuid' => $latestRelease->uuid,
682 'inAppPurchases' => $game->inAppPurchases,
683 'promotedProduct' => null,
684 'premium' => $game->premium,
686 'package' => $game->packageName,
687 'updated_at' => strtotime($latestRelease->date),
688 'updatedAt' => $latestRelease->date,
689 'title' => $game->title,
690 'image' => $game->discover,
691 'contentRating' => $game->contentRating,
693 'count' => $game->rating->count,
694 'average' => $game->rating->average,
696 'promotedProduct' => buildProduct(getPromotedProduct($game)),
700 function categoryPath($title)
702 return str_replace(['/', '\\', ' ', '+', '?'], '_', $title);
705 function getAllAges($games)
708 foreach ($games as $game) {
709 $ages[] = $game->contentRating;
711 return array_unique($ages);
714 function getAllGenres($games)
717 foreach ($games as $game) {
718 $genres = array_merge($genres, $game->genres);
720 return array_unique($genres);
723 function addMissingGameProperties($game)
725 if (!isset($game->overview)) {
726 $game->overview = null;
728 if (!isset($game->description)) {
729 $game->description = '';
731 if (!isset($game->players)) {
732 $game->players = [1];
734 if (!isset($game->genres)) {
735 $game->genres = ['Unsorted'];
737 if (!isset($game->website)) {
738 $game->website = null;
740 if (!isset($game->contentRating)) {
741 $game->contentRating = 'Everyone';
743 if (!isset($game->premium)) {
744 $game->premium = false;
746 if (!isset($game->firstPublishedAt)) {
747 $game->firstPublishedAt = gmdate('c');
750 if (!isset($game->rating)) {
751 $game->rating = new stdClass();
753 if (!isset($game->rating->likeCount)) {
754 $game->rating->likeCount = 0;
756 if (!isset($game->rating->average)) {
757 $game->rating->average = 0;
759 if (!isset($game->rating->count)) {
760 $game->rating->count = 0;
763 $game->latestRelease = null;
764 $latestReleaseTimestamp = 0;
765 foreach ($game->releases as $release) {
766 if (!isset($release->publicSize)) {
767 $release->publicSize = 0;
769 if (!isset($release->nativeSize)) {
770 $release->nativeSize = 0;
773 $releaseTimestamp = strtotime($release->date);
774 if ($releaseTimestamp > $latestReleaseTimestamp) {
775 $game->latestRelease = $release;
776 $latestReleaseTimestamp = $releaseTimestamp;
779 if ($game->latestRelease === null) {
780 error('No latest release for ' . $game->packageName);
783 if (!isset($game->media)) {
787 if (!isset($game->developer->uuid)) {
788 $game->developer->uuid = null;
790 if (!isset($game->developer->name)) {
791 $game->developer->name = 'unknown';
793 if (!isset($game->developer->supportEmail)) {
794 $game->developer->supportEmail = null;
796 if (!isset($game->developer->supportPhone)) {
797 $game->developer->supportPhone = null;
799 if (!isset($game->developer->founder)) {
800 $game->developer->founder = false;
805 * Implements a sensible ranking system described in
806 * https://stackoverflow.com/a/1411268/2826013
808 function calculateRank(array $games)
810 $averageRatings = array_map(
812 return $game->rating->average;
816 $average = array_sum($averageRatings) / count($averageRatings);
820 foreach ($games as $game) {
821 $R = $game->rating->average;
822 $v = $game->rating->count;
823 $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
827 function getFirstVideoUrl($media)
829 foreach ($media as $medium) {
830 if ($medium->type == 'video') {
837 function getAllImageUrls($media)
840 foreach ($media as $medium) {
841 if ($medium->type == 'image') {
842 $imageUrls[] = $medium->url;
848 function getPromotedProduct($game)
850 if (!isset($game->products) || !count($game->products)) {
853 foreach ($game->products as $gameProd) {
854 if ($gameProd->promoted) {
862 * vimeo only work with HTTPS now,
863 * and the OUYA does not support SNI.
864 * We get SSL errors and no video for them :/
866 function isUnsupportedVideoUrl($url)
868 return strpos($url, '://vimeo.com/') !== false;
871 function removeMakeGames(array $games)
873 return filterByGenre($games, 'Tutorials', true);
876 function removeMakeGenres($genres)
879 foreach ($genres as $genre) {
880 if ($genre != 'Tutorials' && $genre != 'Builds') {
881 $filtered[] = $genre;
887 function writeJson($path, $data)
890 $fullPath = $wwwDir . $path;
891 $dir = dirname($fullPath);
893 mkdir($dir, 0777, true);
897 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
903 fwrite(STDERR, $msg . "\n");