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' => 'entitlement',
384 'identifier' => $product->identifier,
385 'name' => $product->name,
386 'description' => $product->description ?? '',
387 'localPrice' => $product->localPrice,
388 'originalPrice' => $product->originalPrice,
390 'currency' => $product->currency,
395 * Build /app/v1/details?app=org.example.game
397 function buildDetails($game)
399 $latestRelease = $game->latestRelease;
402 if ($game->discover) {
406 'thumbnail' => $game->discover,
407 'full' => $game->discover,
411 foreach ($game->media as $medium) {
412 if ($medium->type == 'image') {
416 'thumbnail' => $medium->thumb,
417 'full' => $medium->url,
423 'url' => $medium->url,
429 if (isset($game->links->unlocked)) {
431 'text' => 'Show unlocked',
432 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
438 $gamePromoted = getPromotedProduct($game);
440 $product = buildProduct($gamePromoted);
443 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
446 'title' => $game->title,
447 'description' => $game->description,
448 'gamerNumbers' => $game->players,
449 'genres' => $game->genres,
451 'suggestedAge' => $game->contentRating,
452 'premium' => $game->premium,
453 'inAppPurchases' => $game->inAppPurchases,
454 'firstPublishedAt' => strtotime($game->firstPublishedAt),
458 'count' => $game->rating->count,
459 'average' => $game->rating->average,
463 'fileSize' => $latestRelease->size,
464 'nativeSize' => $latestRelease->nativeSize,
465 'publicSize' => $latestRelease->publicSize,
466 'md5sum' => $latestRelease->md5sum,
467 'filename' => 'FIXME',
469 'package' => $game->packageName,
470 'versionCode' => $latestRelease->versionCode,
471 'state' => 'complete',
475 'number' => $latestRelease->name,
476 'publishedAt' => strtotime($latestRelease->date),
477 'uuid' => $latestRelease->uuid,
481 'name' => $game->developer->name,
482 'founder' => $game->developer->founder,
486 'key:rating.average',
487 'key:developer.name',
489 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
492 'tileImage' => $game->discover,
493 'mediaTiles' => $mediaTiles,
494 'mobileAppIcon' => null,
499 'promotedProduct' => $product,
500 'buttons' => $buttons,
504 function buildDeveloperCurrentGamer()
508 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
509 'username' => 'stouyapi',
515 * For /api/v1/developers/xxx/products/?only=yyy
517 function buildDeveloperProductOnly($product, $developer)
520 'developerName' => $developer->name,
521 'currency' => $product->currency,
523 buildProduct($product),
529 * For /api/v1/developers/xxx/products/
531 function buildDeveloperProducts($products, $developer)
534 foreach ($products as $product) {
535 $jsonProducts[] = buildProduct($product);
538 'developerName' => $developer->name,
539 'currency' => $products[0]->currency ?? 'EUR',
540 'products' => $jsonProducts,
544 function buildPurchases($game)
549 $promotedProduct = getPromotedProduct($game);
550 if ($promotedProduct) {
551 $purchasesData['purchases'][] = [
552 'purchaseDate' => time() * 1000,
553 'generateDate' => time() * 1000,
554 'identifier' => $promotedProduct->identifier,
555 'gamer' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
556 'uuid' => '00702342-0000-1111-2222-c3e1500beef3',//transaction ID
557 'priceInCents' => $promotedProduct->originalPrice * 100,
558 'localPrice' => $promotedProduct->localPrice,
559 'currency' => $promotedProduct->currency,
563 $encryptedOnce = dummyEncrypt($purchasesData);
564 $encryptedTwice = dummyEncrypt($encryptedOnce);
565 return $encryptedTwice;
568 function buildSearch($games)
570 $games = sortByTitle($games);
572 foreach ($games as $game) {
574 'title' => $game->title,
575 'url' => 'ouya://launcher/details?app=' . $game->packageName,
576 'contentRating' => $game->contentRating,
580 'count' => count($results),
581 'results' => $results,
585 function dummyEncrypt($data)
588 'key' => base64_encode('0123456789abcdef') . "\n",
589 'iv' => 't3jir1LHpICunvhlM76edQ==' . "\n",//random bytes
590 'blob' => base64_encode(
591 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
596 function addChunkedDiscoverRows(&$data, $games, $title)
598 $chunks = array_chunk($games, 4);
600 foreach ($chunks as $chunk) {
602 $data, $first ? $title : '',
609 function addDiscoverRow(&$data, $title, $games)
617 foreach ($games as $game) {
618 if (is_string($game)) {
620 $tilePos = count($data['tiles']);
621 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
625 if (isset($game->links->original)) {
626 //do not link unlocked games.
627 // people an access them via the original games
630 $tilePos = findTile($data['tiles'], $game->packageName);
631 if ($tilePos === null) {
632 $tilePos = count($data['tiles']);
633 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
636 $row['tiles'][] = $tilePos;
638 $data['rows'][] = $row;
641 function findTile($tiles, $packageName)
643 foreach ($tiles as $pos => $tile) {
644 if ($tile['package'] == $packageName) {
651 function buildDiscoverCategoryTile($title)
654 'url' => 'ouya://launcher/discover/' . categoryPath($title),
661 function buildDiscoverGameTile($game)
663 $latestRelease = $game->latestRelease;
665 'gamerNumbers' => $game->players,
666 'genres' => $game->genres,
667 'url' => 'ouya://launcher/details?app=' . $game->packageName,
670 'md5sum' => $latestRelease->md5sum,
672 'versionNumber' => $latestRelease->name,
673 'uuid' => $latestRelease->uuid,
675 'inAppPurchases' => $game->inAppPurchases,
676 'promotedProduct' => null,
677 'premium' => $game->premium,
679 'package' => $game->packageName,
680 'updated_at' => strtotime($latestRelease->date),
681 'updatedAt' => $latestRelease->date,
682 'title' => $game->title,
683 'image' => $game->discover,
684 'contentRating' => $game->contentRating,
686 'count' => $game->rating->count,
687 'average' => $game->rating->average,
689 'promotedProduct' => buildProduct(getPromotedProduct($game)),
693 function categoryPath($title)
695 return str_replace(['/', '\\', ' ', '+', '?'], '_', $title);
698 function getAllAges($games)
701 foreach ($games as $game) {
702 $ages[] = $game->contentRating;
704 return array_unique($ages);
707 function getAllGenres($games)
710 foreach ($games as $game) {
711 $genres = array_merge($genres, $game->genres);
713 return array_unique($genres);
716 function addMissingGameProperties($game)
718 if (!isset($game->overview)) {
719 $game->overview = null;
721 if (!isset($game->description)) {
722 $game->description = '';
724 if (!isset($game->players)) {
725 $game->players = [1];
727 if (!isset($game->genres)) {
728 $game->genres = ['Unsorted'];
730 if (!isset($game->website)) {
731 $game->website = null;
733 if (!isset($game->contentRating)) {
734 $game->contentRating = 'Everyone';
736 if (!isset($game->premium)) {
737 $game->premium = false;
739 if (!isset($game->firstPublishedAt)) {
740 $game->firstPublishedAt = gmdate('c');
743 if (!isset($game->rating)) {
744 $game->rating = new stdClass();
746 if (!isset($game->rating->likeCount)) {
747 $game->rating->likeCount = 0;
749 if (!isset($game->rating->average)) {
750 $game->rating->average = 0;
752 if (!isset($game->rating->count)) {
753 $game->rating->count = 0;
756 $game->latestRelease = null;
757 $latestReleaseTimestamp = 0;
758 foreach ($game->releases as $release) {
759 if (!isset($release->publicSize)) {
760 $release->publicSize = 0;
762 if (!isset($release->nativeSize)) {
763 $release->nativeSize = 0;
766 $releaseTimestamp = strtotime($release->date);
767 if ($releaseTimestamp > $latestReleaseTimestamp) {
768 $game->latestRelease = $release;
769 $latestReleaseTimestamp = $releaseTimestamp;
772 if ($game->latestRelease === null) {
773 error('No latest release for ' . $game->packageName);
776 if (!isset($game->media)) {
780 if (!isset($game->developer->uuid)) {
781 $game->developer->uuid = null;
783 if (!isset($game->developer->name)) {
784 $game->developer->name = 'unknown';
786 if (!isset($game->developer->supportEmail)) {
787 $game->developer->supportEmail = null;
789 if (!isset($game->developer->supportPhone)) {
790 $game->developer->supportPhone = null;
792 if (!isset($game->developer->founder)) {
793 $game->developer->founder = false;
798 * Implements a sensible ranking system described in
799 * https://stackoverflow.com/a/1411268/2826013
801 function calculateRank(array $games)
803 $averageRatings = array_map(
805 return $game->rating->average;
809 $average = array_sum($averageRatings) / count($averageRatings);
813 foreach ($games as $game) {
814 $R = $game->rating->average;
815 $v = $game->rating->count;
816 $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
820 function getFirstVideoUrl($media)
822 foreach ($media as $medium) {
823 if ($medium->type == 'video') {
830 function getAllImageUrls($media)
833 foreach ($media as $medium) {
834 if ($medium->type == 'image') {
835 $imageUrls[] = $medium->url;
841 function getPromotedProduct($game)
843 if (!isset($game->products) || !count($game->products)) {
846 foreach ($game->products as $gameProd) {
847 if ($gameProd->promoted) {
854 function removeMakeGames(array $games)
856 return filterByGenre($games, 'Tutorials', true);
859 function removeMakeGenres($genres)
862 foreach ($genres as $genre) {
863 if ($genre != 'Tutorials' && $genre != 'Builds') {
864 $filtered[] = $genre;
870 function writeJson($path, $data)
873 $fullPath = $wwwDir . $path;
874 $dir = dirname($fullPath);
876 mkdir($dir, 0777, true);
880 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
886 fwrite(STDERR, $msg . "\n");