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,
424 'url' => $medium->url,
430 if (isset($game->links->unlocked)) {
432 'text' => 'Show unlocked',
433 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
439 $gamePromoted = getPromotedProduct($game);
441 $product = buildProduct($gamePromoted);
444 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
447 'title' => $game->title,
448 'description' => $game->description,
449 'gamerNumbers' => $game->players,
450 'genres' => $game->genres,
452 'suggestedAge' => $game->contentRating,
453 'premium' => $game->premium,
454 'inAppPurchases' => $game->inAppPurchases,
455 'firstPublishedAt' => strtotime($game->firstPublishedAt),
459 'count' => $game->rating->count,
460 'average' => $game->rating->average,
464 'fileSize' => $latestRelease->size,
465 'nativeSize' => $latestRelease->nativeSize,
466 'publicSize' => $latestRelease->publicSize,
467 'md5sum' => $latestRelease->md5sum,
468 'filename' => 'FIXME',
470 'package' => $game->packageName,
471 'versionCode' => $latestRelease->versionCode,
472 'state' => 'complete',
476 'number' => $latestRelease->name,
477 'publishedAt' => strtotime($latestRelease->date),
478 'uuid' => $latestRelease->uuid,
482 'name' => $game->developer->name,
483 'founder' => $game->developer->founder,
487 'key:rating.average',
488 'key:developer.name',
490 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
493 'tileImage' => $game->discover,
494 'mediaTiles' => $mediaTiles,
495 'mobileAppIcon' => null,
500 'promotedProduct' => $product,
501 'buttons' => $buttons,
505 function buildDeveloperCurrentGamer()
509 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
510 'username' => 'stouyapi',
516 * For /api/v1/developers/xxx/products/?only=yyy
518 function buildDeveloperProductOnly($product, $developer)
521 'developerName' => $developer->name,
522 'currency' => $product->currency,
524 buildProduct($product),
530 * For /api/v1/developers/xxx/products/
532 function buildDeveloperProducts($products, $developer)
535 foreach ($products as $product) {
536 $jsonProducts[] = buildProduct($product);
539 'developerName' => $developer->name,
540 'currency' => $products[0]->currency ?? 'EUR',
541 'products' => $jsonProducts,
545 function buildPurchases($game)
550 $promotedProduct = getPromotedProduct($game);
551 if ($promotedProduct) {
552 $purchasesData['purchases'][] = [
553 'purchaseDate' => time() * 1000,
554 'generateDate' => time() * 1000,
555 'identifier' => $promotedProduct->identifier,
556 'gamer' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
557 'uuid' => '00702342-0000-1111-2222-c3e1500beef3',//transaction ID
558 'priceInCents' => $promotedProduct->originalPrice * 100,
559 'localPrice' => $promotedProduct->localPrice,
560 'currency' => $promotedProduct->currency,
564 $encryptedOnce = dummyEncrypt($purchasesData);
565 $encryptedTwice = dummyEncrypt($encryptedOnce);
566 return $encryptedTwice;
569 function buildSearch($games)
571 $games = sortByTitle($games);
573 foreach ($games as $game) {
575 'title' => $game->title,
576 'url' => 'ouya://launcher/details?app=' . $game->packageName,
577 'contentRating' => $game->contentRating,
581 'count' => count($results),
582 'results' => $results,
586 function dummyEncrypt($data)
589 'key' => base64_encode('0123456789abcdef'),
590 'iv' => 't3jir1LHpICunvhlM76edQ==',//random bytes
591 'blob' => base64_encode(
592 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
597 function addChunkedDiscoverRows(&$data, $games, $title)
599 $chunks = array_chunk($games, 4);
601 foreach ($chunks as $chunk) {
603 $data, $first ? $title : '',
610 function addDiscoverRow(&$data, $title, $games)
618 foreach ($games as $game) {
619 if (is_string($game)) {
621 $tilePos = count($data['tiles']);
622 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
626 if (isset($game->links->original)) {
627 //do not link unlocked games.
628 // people an access them via the original games
631 $tilePos = findTile($data['tiles'], $game->packageName);
632 if ($tilePos === null) {
633 $tilePos = count($data['tiles']);
634 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
637 $row['tiles'][] = $tilePos;
639 $data['rows'][] = $row;
642 function findTile($tiles, $packageName)
644 foreach ($tiles as $pos => $tile) {
645 if ($tile['package'] == $packageName) {
652 function buildDiscoverCategoryTile($title)
655 'url' => 'ouya://launcher/discover/' . categoryPath($title),
662 function buildDiscoverGameTile($game)
664 $latestRelease = $game->latestRelease;
666 'gamerNumbers' => $game->players,
667 'genres' => $game->genres,
668 'url' => 'ouya://launcher/details?app=' . $game->packageName,
671 'md5sum' => $latestRelease->md5sum,
673 'versionNumber' => $latestRelease->name,
674 'uuid' => $latestRelease->uuid,
676 'inAppPurchases' => $game->inAppPurchases,
677 'promotedProduct' => null,
678 'premium' => $game->premium,
680 'package' => $game->packageName,
681 'updated_at' => strtotime($latestRelease->date),
682 'updatedAt' => $latestRelease->date,
683 'title' => $game->title,
684 'image' => $game->discover,
685 'contentRating' => $game->contentRating,
687 'count' => $game->rating->count,
688 'average' => $game->rating->average,
690 'promotedProduct' => buildProduct(getPromotedProduct($game)),
694 function categoryPath($title)
696 return str_replace(['/', '\\', ' ', '+', '?'], '_', $title);
699 function getAllAges($games)
702 foreach ($games as $game) {
703 $ages[] = $game->contentRating;
705 return array_unique($ages);
708 function getAllGenres($games)
711 foreach ($games as $game) {
712 $genres = array_merge($genres, $game->genres);
714 return array_unique($genres);
717 function addMissingGameProperties($game)
719 if (!isset($game->overview)) {
720 $game->overview = null;
722 if (!isset($game->description)) {
723 $game->description = '';
725 if (!isset($game->players)) {
726 $game->players = [1];
728 if (!isset($game->genres)) {
729 $game->genres = ['Unsorted'];
731 if (!isset($game->website)) {
732 $game->website = null;
734 if (!isset($game->contentRating)) {
735 $game->contentRating = 'Everyone';
737 if (!isset($game->premium)) {
738 $game->premium = false;
740 if (!isset($game->firstPublishedAt)) {
741 $game->firstPublishedAt = gmdate('c');
744 if (!isset($game->rating)) {
745 $game->rating = new stdClass();
747 if (!isset($game->rating->likeCount)) {
748 $game->rating->likeCount = 0;
750 if (!isset($game->rating->average)) {
751 $game->rating->average = 0;
753 if (!isset($game->rating->count)) {
754 $game->rating->count = 0;
757 $game->latestRelease = null;
758 $latestReleaseTimestamp = 0;
759 foreach ($game->releases as $release) {
760 if (!isset($release->publicSize)) {
761 $release->publicSize = 0;
763 if (!isset($release->nativeSize)) {
764 $release->nativeSize = 0;
767 $releaseTimestamp = strtotime($release->date);
768 if ($releaseTimestamp > $latestReleaseTimestamp) {
769 $game->latestRelease = $release;
770 $latestReleaseTimestamp = $releaseTimestamp;
773 if ($game->latestRelease === null) {
774 error('No latest release for ' . $game->packageName);
777 if (!isset($game->media)) {
781 if (!isset($game->developer->uuid)) {
782 $game->developer->uuid = null;
784 if (!isset($game->developer->name)) {
785 $game->developer->name = 'unknown';
787 if (!isset($game->developer->supportEmail)) {
788 $game->developer->supportEmail = null;
790 if (!isset($game->developer->supportPhone)) {
791 $game->developer->supportPhone = null;
793 if (!isset($game->developer->founder)) {
794 $game->developer->founder = false;
799 * Implements a sensible ranking system described in
800 * https://stackoverflow.com/a/1411268/2826013
802 function calculateRank(array $games)
804 $averageRatings = array_map(
806 return $game->rating->average;
810 $average = array_sum($averageRatings) / count($averageRatings);
814 foreach ($games as $game) {
815 $R = $game->rating->average;
816 $v = $game->rating->count;
817 $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
821 function getFirstVideoUrl($media)
823 foreach ($media as $medium) {
824 if ($medium->type == 'video') {
831 function getAllImageUrls($media)
834 foreach ($media as $medium) {
835 if ($medium->type == 'image') {
836 $imageUrls[] = $medium->url;
842 function getPromotedProduct($game)
844 if (!isset($game->products) || !count($game->products)) {
847 foreach ($game->products as $gameProd) {
848 if ($gameProd->promoted) {
855 function removeMakeGames(array $games)
857 return filterByGenre($games, 'Tutorials', true);
860 function removeMakeGenres($genres)
863 foreach ($genres as $genre) {
864 if ($genre != 'Tutorials' && $genre != 'Builds') {
865 $filtered[] = $genre;
871 function writeJson($path, $data)
874 $fullPath = $wwwDir . $path;
875 $dir = dirname($fullPath);
877 mkdir($dir, 0777, true);
881 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
887 fwrite(STDERR, $msg . "\n");