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 $GLOBALS['urlRewrites'] = [];
22 $cfgFile = __DIR__ . '/../config.php';
23 if (file_exists($cfgFile)) {
27 $wwwDir = __DIR__ . '/../www/';
29 $baseDir = dirname($foldersFile);
31 foreach (file($foldersFile) as $line) {
34 if (strpos($line, '..') !== false) {
35 error('Path attack in ' . $folder);
37 $folder = $baseDir . '/' . $line;
38 if (!is_dir($folder)) {
39 error('Folder does not exist: ' . $folder);
41 $gameFiles = array_merge($gameFiles, glob($folder . '/*.json'));
48 foreach ($gameFiles as $gameFile) {
49 $game = json_decode(file_get_contents($gameFile));
51 error('JSON invalid at ' . $gameFile);
53 addMissingGameProperties($game);
54 $games[$game->packageName] = $game;
57 'api/v1/details-data/' . $game->packageName . '.json',
61 if (!isset($developers[$game->developer->uuid])) {
62 $developers[$game->developer->uuid] = [
63 'info' => $game->developer,
68 $products = $game->products ?? [];
69 foreach ($products as $product) {
71 'api/v1/developers/' . $game->developer->uuid
72 . '/products/' . $product->identifier . '.json',
73 buildDeveloperProductOnly($product, $game->developer)
75 $developers[$game->developer->uuid]['products'][] = $product;
80 'api/v1/games/' . $game->packageName . '/purchases',
85 'api/v1/apps/' . $game->packageName . '.json',
88 $latestRelease = $game->latestRelease;
90 'api/v1/apps/' . $latestRelease->uuid . '.json',
95 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
96 buildAppDownload($game, $latestRelease)
104 calculateRank($games);
106 foreach ($developers as $developer) {
108 //index.htm does not need a rewrite rule
109 'api/v1/developers/' . $developer['info']->uuid
110 . '/products/index.htm',
111 buildDeveloperProducts($developer['products'], $developer['info'])
114 //index.htm does not need a rewrite rule
115 'api/v1/developers/' . $developer['info']->uuid
117 buildDeveloperCurrentGamer()
121 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
122 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
126 'api/v1/discover-data/tutorials.json',
127 buildMakeCategory('Tutorials', filterByGenre($games, 'Tutorials'))
130 $searchLetters = 'abcdefghijklmnopqrstuvwxyz0123456789., ';
131 foreach (str_split($searchLetters) as $letter) {
132 $letterGames = filterBySearchWord($games, $letter);
134 'api/v1/search-data/' . $letter . '.json',
135 buildSearch($letterGames)
140 function buildDiscover(array $games)
142 $games = removeMakeGames($games);
144 'title' => 'DISCOVER',
150 $data, 'Last Updated',
151 filterLastUpdated($games, 10)
155 filterBestRated($games, 10)
158 foreach ($GLOBALS['packagelists'] as $listTitle => $listPackageNames) {
161 filterByPackageNames($games, $listPackageNames)
174 'api/v1/discover-data/' . categoryPath('Best rated') . '.json',
175 buildSpecialCategory('Best rated', filterBestRated($games, 99))
178 'api/v1/discover-data/' . categoryPath('Most rated') . '.json',
179 buildSpecialCategory('Most rated', filterMostDownloaded($games, 99))
182 'api/v1/discover-data/' . categoryPath('Random') . '.json',
183 buildSpecialCategory(
184 'Random ' . date('Y-m-d H:i'),
185 filterRandom($games, 99)
195 addDiscoverRow($data, 'Multiplayer', $players);
196 foreach ($players as $num => $title) {
198 'api/v1/discover-data/' . categoryPath($title) . '.json',
199 buildDiscoverCategory($title, filterByPlayers($games, $num))
203 $ages = getAllAges($games);
205 addDiscoverRow($data, 'Content rating', $ages);
206 foreach ($ages as $num => $title) {
208 'api/v1/discover-data/' . categoryPath($title) . '.json',
209 buildDiscoverCategory($title, filterByAge($games, $title))
213 $genres = removeMakeGenres(getAllGenres($games));
215 addChunkedDiscoverRows($data, $genres, 'Genres');
217 foreach ($genres as $genre) {
219 'api/v1/discover-data/' . categoryPath($genre) . '.json',
220 buildDiscoverCategory($genre, filterByGenre($games, $genre))
224 $abc = array_merge(range('A', 'Z'), ['Other']);
225 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
226 foreach ($abc as $letter) {
228 'api/v1/discover-data/' . categoryPath($letter) . '.json',
229 buildDiscoverCategory($letter, filterByLetter($games, $letter))
237 * A genre category page
239 function buildDiscoverCategory($name, $games)
247 $data, 'Last Updated',
248 filterLastUpdated($games, 10)
252 filterBestRated($games, 10)
255 $games = sortByTitle($games);
256 $chunks = array_chunk($games, 4);
257 foreach ($chunks as $chunkGames) {
258 addDiscoverRow($data, '', $chunkGames);
264 function buildMakeCategory($name, $games)
272 $games = sortByTitle($games);
273 addDiscoverRow($data, '', $games);
278 function buildSpecialCategory($name, $games)
286 $first3 = array_slice($games, 0, 3);
287 $chunks = array_chunk(array_slice($games, 3), 4);
288 array_unshift($chunks, $first3);
290 foreach ($chunks as $chunkGames) {
291 addDiscoverRow($data, '', $chunkGames);
297 function buildDiscoverHome(array $games)
299 //we do not want anything here for now
304 'title' => 'FEATURED',
305 'showPrice' => false,
316 * Build api/v1/apps/$packageName
318 function buildApps($game)
320 $latestRelease = $game->latestRelease;
323 $gamePromoted = getPromotedProduct($game);
325 $product = buildProduct($gamePromoted);
328 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
331 'uuid' => $latestRelease->uuid,
332 'title' => $game->title,
333 'overview' => $game->overview,
334 'description' => $game->description,
335 'gamerNumbers' => $game->players,
336 'genres' => $game->genres,
338 'website' => $game->website,
339 'contentRating' => $game->contentRating,
340 'premium' => $game->premium,
341 'firstPublishedAt' => $game->firstPublishedAt,
343 'likeCount' => $game->rating->likeCount,
344 'ratingAverage' => $game->rating->average,
345 'ratingCount' => $game->rating->count,
347 'versionNumber' => $latestRelease->name,
348 'latestVersion' => $latestRelease->uuid,
349 'md5sum' => $latestRelease->md5sum,
350 'apkFileSize' => $latestRelease->size,
351 'publishedAt' => $latestRelease->date,
352 'publicSize' => $latestRelease->publicSize,
353 'nativeSize' => $latestRelease->nativeSize,
355 'mainImageFullUrl' => $game->discover,
356 'videoUrl' => getFirstVideoUrl($game->media),
357 'filepickerScreenshots' => getAllImageUrls($game->media),
358 'mobileAppIcon' => null,
360 'developer' => $game->developer->name,
361 'supportEmailAddress' => $game->developer->supportEmail,
362 'supportPhone' => $game->developer->supportPhone,
363 'founder' => $game->developer->founder,
365 'promotedProduct' => $product,
370 function buildAppDownload($game, $release)
374 'fileSize' => $release->size,
375 'version' => $release->uuid,
376 'contentRating' => $game->contentRating,
377 'downloadLink' => rewriteUrl($release->url),
382 function buildProduct($product)
384 if ($product === null) {
388 'type' => $product->type ?? 'entitlement',
389 'identifier' => $product->identifier,
390 'name' => $product->name,
391 'description' => $product->description ?? '',
392 'localPrice' => $product->localPrice,
393 'originalPrice' => $product->originalPrice,
394 'priceInCents' => $product->originalPrice * 100,
396 'currency' => $product->currency,
401 * Build /app/v1/details?app=org.example.game
403 function buildDetails($game)
405 $latestRelease = $game->latestRelease;
408 if ($game->discover) {
412 'thumbnail' => $game->discover,
413 'full' => $game->discover,
417 foreach ($game->media as $medium) {
418 if ($medium->type == 'image') {
422 'thumbnail' => $medium->thumb,
423 'full' => $medium->url,
427 if (!isUnsupportedVideoUrl($medium->url)) {
430 'url' => $medium->url,
437 if (isset($game->links->unlocked)) {
439 'text' => 'Show unlocked',
440 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
446 $gamePromoted = getPromotedProduct($game);
448 $product = buildProduct($gamePromoted);
451 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
454 'title' => $game->title,
455 'description' => $game->description,
456 'gamerNumbers' => $game->players,
457 'genres' => $game->genres,
459 'suggestedAge' => $game->contentRating,
460 'premium' => $game->premium,
461 'inAppPurchases' => $game->inAppPurchases,
462 'firstPublishedAt' => strtotime($game->firstPublishedAt),
466 'count' => $game->rating->count,
467 'average' => $game->rating->average,
471 'fileSize' => $latestRelease->size,
472 'nativeSize' => $latestRelease->nativeSize,
473 'publicSize' => $latestRelease->publicSize,
474 'md5sum' => $latestRelease->md5sum,
475 'filename' => 'FIXME',
477 'package' => $game->packageName,
478 'versionCode' => $latestRelease->versionCode,
479 'state' => 'complete',
483 'number' => $latestRelease->name,
484 'publishedAt' => strtotime($latestRelease->date),
485 'uuid' => $latestRelease->uuid,
489 'name' => $game->developer->name,
490 'founder' => $game->developer->founder,
494 'key:rating.average',
495 'key:developer.name',
497 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
500 'tileImage' => $game->discover,
501 'mediaTiles' => $mediaTiles,
502 'mobileAppIcon' => null,
507 'promotedProduct' => $product,
508 'buttons' => $buttons,
512 function buildDeveloperCurrentGamer()
516 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
517 'username' => 'stouyapi',
523 * For /api/v1/developers/xxx/products/?only=yyy
525 function buildDeveloperProductOnly($product, $developer)
528 'developerName' => $developer->name,
529 'currency' => $product->currency,
531 buildProduct($product),
537 * For /api/v1/developers/xxx/products/
539 function buildDeveloperProducts($products, $developer)
542 foreach ($products as $product) {
543 $jsonProducts[] = buildProduct($product);
546 'developerName' => $developer->name,
547 'currency' => $products[0]->currency ?? 'EUR',
548 'products' => $jsonProducts,
552 function buildPurchases($game)
557 $promotedProduct = getPromotedProduct($game);
558 if ($promotedProduct) {
559 $purchasesData['purchases'][] = [
560 'purchaseDate' => time() * 1000,
561 'generateDate' => time() * 1000,
562 'identifier' => $promotedProduct->identifier,
563 'gamer' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
564 'uuid' => '00702342-0000-1111-2222-c3e1500beef3',//transaction ID
565 'priceInCents' => $promotedProduct->originalPrice * 100,
566 'localPrice' => $promotedProduct->localPrice,
567 'currency' => $promotedProduct->currency,
571 $encryptedOnce = dummyEncrypt($purchasesData);
572 $encryptedTwice = dummyEncrypt($encryptedOnce);
573 return $encryptedTwice;
576 function buildSearch($games)
578 $games = sortByTitle($games);
580 foreach ($games as $game) {
582 'title' => $game->title,
583 'url' => 'ouya://launcher/details?app=' . $game->packageName,
584 'contentRating' => $game->contentRating,
588 'count' => count($results),
589 'results' => $results,
593 function dummyEncrypt($data)
596 'key' => base64_encode('0123456789abcdef'),
597 'iv' => 't3jir1LHpICunvhlM76edQ==',//random bytes
598 'blob' => base64_encode(
599 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
604 function addChunkedDiscoverRows(&$data, $games, $title)
606 $chunks = array_chunk($games, 4);
608 foreach ($chunks as $chunk) {
610 $data, $first ? $title : '',
617 function addDiscoverRow(&$data, $title, $games)
625 foreach ($games as $game) {
626 if (is_string($game)) {
628 $tilePos = count($data['tiles']);
629 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
633 if (isset($game->links->original)) {
634 //do not link unlocked games.
635 // people an access them via the original games
638 $tilePos = findTile($data['tiles'], $game->packageName);
639 if ($tilePos === null) {
640 $tilePos = count($data['tiles']);
641 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
644 $row['tiles'][] = $tilePos;
646 $data['rows'][] = $row;
649 function findTile($tiles, $packageName)
651 foreach ($tiles as $pos => $tile) {
652 if ($tile['package'] == $packageName) {
659 function buildDiscoverCategoryTile($title)
662 'url' => 'ouya://launcher/discover/' . categoryPath($title),
669 function buildDiscoverGameTile($game)
671 $latestRelease = $game->latestRelease;
673 'gamerNumbers' => $game->players,
674 'genres' => $game->genres,
675 'url' => 'ouya://launcher/details?app=' . $game->packageName,
678 'md5sum' => $latestRelease->md5sum,
680 'versionNumber' => $latestRelease->name,
681 'uuid' => $latestRelease->uuid,
683 'inAppPurchases' => $game->inAppPurchases,
684 'promotedProduct' => null,
685 'premium' => $game->premium,
687 'package' => $game->packageName,
688 'updated_at' => strtotime($latestRelease->date),
689 'updatedAt' => $latestRelease->date,
690 'title' => $game->title,
691 'image' => $game->discover,
692 'contentRating' => $game->contentRating,
694 'count' => $game->rating->count,
695 'average' => $game->rating->average,
697 'promotedProduct' => buildProduct(getPromotedProduct($game)),
701 function categoryPath($title)
703 return str_replace(['/', '\\', ' ', '+', '?'], '_', $title);
706 function getAllAges($games)
709 foreach ($games as $game) {
710 $ages[] = $game->contentRating;
712 return array_unique($ages);
715 function getAllGenres($games)
718 foreach ($games as $game) {
719 $genres = array_merge($genres, $game->genres);
721 return array_unique($genres);
724 function addMissingGameProperties($game)
726 if (!isset($game->overview)) {
727 $game->overview = null;
729 if (!isset($game->description)) {
730 $game->description = '';
732 if (!isset($game->players)) {
733 $game->players = [1];
735 if (!isset($game->genres)) {
736 $game->genres = ['Unsorted'];
738 if (!isset($game->website)) {
739 $game->website = null;
741 if (!isset($game->contentRating)) {
742 $game->contentRating = 'Everyone';
744 if (!isset($game->premium)) {
745 $game->premium = false;
747 if (!isset($game->firstPublishedAt)) {
748 $game->firstPublishedAt = gmdate('c');
751 if (!isset($game->rating)) {
752 $game->rating = new stdClass();
754 if (!isset($game->rating->likeCount)) {
755 $game->rating->likeCount = 0;
757 if (!isset($game->rating->average)) {
758 $game->rating->average = 0;
760 if (!isset($game->rating->count)) {
761 $game->rating->count = 0;
764 $game->latestRelease = null;
765 $latestReleaseTimestamp = 0;
766 foreach ($game->releases as $release) {
767 if (!isset($release->publicSize)) {
768 $release->publicSize = 0;
770 if (!isset($release->nativeSize)) {
771 $release->nativeSize = 0;
774 $releaseTimestamp = strtotime($release->date);
775 if ($releaseTimestamp > $latestReleaseTimestamp) {
776 $game->latestRelease = $release;
777 $latestReleaseTimestamp = $releaseTimestamp;
780 if ($game->latestRelease === null) {
781 error('No latest release for ' . $game->packageName);
784 if (!isset($game->media)) {
788 if (!isset($game->developer->uuid)) {
789 $game->developer->uuid = null;
791 if (!isset($game->developer->name)) {
792 $game->developer->name = 'unknown';
794 if (!isset($game->developer->supportEmail)) {
795 $game->developer->supportEmail = null;
797 if (!isset($game->developer->supportPhone)) {
798 $game->developer->supportPhone = null;
800 if (!isset($game->developer->founder)) {
801 $game->developer->founder = false;
806 * Implements a sensible ranking system described in
807 * https://stackoverflow.com/a/1411268/2826013
809 function calculateRank(array $games)
811 $averageRatings = array_map(
813 return $game->rating->average;
817 $average = array_sum($averageRatings) / count($averageRatings);
821 foreach ($games as $game) {
822 $R = $game->rating->average;
823 $v = $game->rating->count;
824 $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
828 function getFirstVideoUrl($media)
830 foreach ($media as $medium) {
831 if ($medium->type == 'video') {
838 function getAllImageUrls($media)
841 foreach ($media as $medium) {
842 if ($medium->type == 'image') {
843 $imageUrls[] = $medium->url;
849 function getPromotedProduct($game)
851 if (!isset($game->products) || !count($game->products)) {
854 foreach ($game->products as $gameProd) {
855 if ($gameProd->promoted) {
863 * vimeo only work with HTTPS now,
864 * and the OUYA does not support SNI.
865 * We get SSL errors and no video for them :/
867 function isUnsupportedVideoUrl($url)
869 return strpos($url, '://vimeo.com/') !== false;
872 function removeMakeGames(array $games)
874 return filterByGenre($games, 'Tutorials', true);
877 function removeMakeGenres($genres)
880 foreach ($genres as $genre) {
881 if ($genre != 'Tutorials' && $genre != 'Builds') {
882 $filtered[] = $genre;
888 function rewriteUrl($url)
890 foreach ($GLOBALS['urlRewrites'] as $pattern => $replacement) {
891 $url = preg_replace($pattern, $replacement, $url);
896 function writeJson($path, $data)
899 $fullPath = $wwwDir . $path;
900 $dir = dirname($fullPath);
902 mkdir($dir, 0777, true);
906 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
912 fwrite(STDERR, $msg . "\n");