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 $baseDir = dirname($foldersFile);
32 foreach (file($foldersFile) as $line) {
35 if (strpos($line, '..') !== false) {
36 error('Path attack in ' . $folder);
38 $folder = $baseDir . '/' . $line;
39 if (!is_dir($folder)) {
40 error('Folder does not exist: ' . $folder);
42 $gameFiles = array_merge($gameFiles, glob($folder . '/*.json'));
49 foreach ($gameFiles as $gameFile) {
50 $game = json_decode(file_get_contents($gameFile));
52 error('JSON invalid at ' . $gameFile);
54 addMissingGameProperties($game);
55 $games[$game->packageName] = $game;
58 'api/v1/details-data/' . $game->packageName . '.json',
62 if (!isset($developers[$game->developer->uuid])) {
63 $developers[$game->developer->uuid] = [
64 'info' => $game->developer,
69 $products = $game->products ?? [];
70 foreach ($products as $product) {
72 'api/v1/developers/' . $game->developer->uuid
73 . '/products/' . $product->identifier . '.json',
74 buildDeveloperProductOnly($product, $game->developer)
76 $developers[$game->developer->uuid]['products'][] = $product;
81 'api/v1/games/' . $game->packageName . '/purchases',
86 'api/v1/apps/' . $game->packageName . '.json',
89 $latestRelease = $game->latestRelease;
91 'api/v1/apps/' . $latestRelease->uuid . '.json',
96 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
97 buildAppDownload($game, $latestRelease)
105 calculateRank($games);
107 foreach ($developers as $developer) {
109 //index.htm does not need a rewrite rule
110 'api/v1/developers/' . $developer['info']->uuid
111 . '/products/index.htm',
112 buildDeveloperProducts($developer['products'], $developer['info'])
115 //index.htm does not need a rewrite rule
116 'api/v1/developers/' . $developer['info']->uuid
118 buildDeveloperCurrentGamer()
122 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
123 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
127 'api/v1/discover-data/tutorials.json',
128 buildMakeCategory('Tutorials', filterByGenre($games, 'Tutorials'))
131 $searchLetters = 'abcdefghijklmnopqrstuvwxyz0123456789., ';
132 foreach (str_split($searchLetters) as $letter) {
133 $letterGames = filterBySearchWord($games, $letter);
135 'api/v1/search-data/' . $letter . '.json',
136 buildSearch($letterGames)
141 function buildDiscover(array $games)
143 $games = removeMakeGames($games);
145 'title' => 'DISCOVER',
151 $data, 'Last Updated',
152 filterLastUpdated($games, 10)
156 filterBestRated($games, 10)
159 foreach ($GLOBALS['packagelists'] as $listTitle => $listPackageNames) {
162 filterByPackageNames($games, $listPackageNames)
175 'api/v1/discover-data/' . categoryPath('Best rated') . '.json',
176 buildSpecialCategory('Best rated', filterBestRated($games, 99))
179 'api/v1/discover-data/' . categoryPath('Most rated') . '.json',
180 buildSpecialCategory('Most rated', filterMostDownloaded($games, 99))
183 'api/v1/discover-data/' . categoryPath('Random') . '.json',
184 buildSpecialCategory(
185 'Random ' . date('Y-m-d H:i'),
186 filterRandom($games, 99)
196 addDiscoverRow($data, 'Multiplayer', $players);
197 foreach ($players as $num => $title) {
199 'api/v1/discover-data/' . categoryPath($title) . '.json',
200 buildDiscoverCategory($title, filterByPlayers($games, $num))
204 $ages = getAllAges($games);
206 addDiscoverRow($data, 'Content rating', $ages);
207 foreach ($ages as $num => $title) {
209 'api/v1/discover-data/' . categoryPath($title) . '.json',
210 buildDiscoverCategory($title, filterByAge($games, $title))
214 $genres = removeMakeGenres(getAllGenres($games));
216 addChunkedDiscoverRows($data, $genres, 'Genres');
218 foreach ($genres as $genre) {
220 'api/v1/discover-data/' . categoryPath($genre) . '.json',
221 buildDiscoverCategory($genre, filterByGenre($games, $genre))
225 $abc = array_merge(range('A', 'Z'), ['Other']);
226 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
227 foreach ($abc as $letter) {
229 'api/v1/discover-data/' . categoryPath($letter) . '.json',
230 buildDiscoverCategory($letter, filterByLetter($games, $letter))
238 * A genre category page
240 function buildDiscoverCategory($name, $games)
248 $data, 'Last Updated',
249 filterLastUpdated($games, 10)
253 filterBestRated($games, 10)
256 $games = sortByTitle($games);
257 $chunks = array_chunk($games, 4);
258 foreach ($chunks as $chunkGames) {
259 addDiscoverRow($data, '', $chunkGames);
265 function buildMakeCategory($name, $games)
273 $games = sortByTitle($games);
274 addDiscoverRow($data, '', $games);
279 function buildSpecialCategory($name, $games)
287 $first3 = array_slice($games, 0, 3);
288 $chunks = array_chunk(array_slice($games, 3), 4);
289 array_unshift($chunks, $first3);
291 foreach ($chunks as $chunkGames) {
292 addDiscoverRow($data, '', $chunkGames);
298 function buildDiscoverHome(array $games)
300 //we do not want anything here for now
305 'title' => 'FEATURED',
306 'showPrice' => false,
317 * Build api/v1/apps/$packageName
319 function buildApps($game)
321 $latestRelease = $game->latestRelease;
324 $gamePromoted = getPromotedProduct($game);
326 $product = buildProduct($gamePromoted);
329 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
332 'uuid' => $latestRelease->uuid,
333 'title' => $game->title,
334 'overview' => $game->overview,
335 'description' => $game->description,
336 'gamerNumbers' => $game->players,
337 'genres' => $game->genres,
339 'website' => $game->website,
340 'contentRating' => $game->contentRating,
341 'premium' => $game->premium,
342 'firstPublishedAt' => $game->firstPublishedAt,
344 'likeCount' => $game->rating->likeCount,
345 'ratingAverage' => $game->rating->average,
346 'ratingCount' => $game->rating->count,
348 'versionNumber' => $latestRelease->name,
349 'latestVersion' => $latestRelease->uuid,
350 'md5sum' => $latestRelease->md5sum,
351 'apkFileSize' => $latestRelease->size,
352 'publishedAt' => $latestRelease->date,
353 'publicSize' => $latestRelease->publicSize,
354 'nativeSize' => $latestRelease->nativeSize,
356 'mainImageFullUrl' => $game->discover,
357 'videoUrl' => getFirstVideoUrl($game->media),
358 'filepickerScreenshots' => getAllImageUrls($game->media),
359 'mobileAppIcon' => null,
361 'developer' => $game->developer->name,
362 'supportEmailAddress' => $game->developer->supportEmail,
363 'supportPhone' => $game->developer->supportPhone,
364 'founder' => $game->developer->founder,
366 'promotedProduct' => $product,
371 function buildAppDownload($game, $release)
375 'fileSize' => $release->size,
376 'version' => $release->uuid,
377 'contentRating' => $game->contentRating,
378 'downloadLink' => rewriteUrl($release->url),
383 function buildProduct($product)
385 if ($product === null) {
389 'type' => $product->type ?? 'entitlement',
390 'identifier' => $product->identifier,
391 'name' => $product->name,
392 'description' => $product->description ?? '',
393 'localPrice' => $product->localPrice,
394 'originalPrice' => $product->originalPrice,
395 'priceInCents' => $product->originalPrice * 100,
397 'currency' => $product->currency,
402 * Build /app/v1/details?app=org.example.game
404 function buildDetails($game)
406 $latestRelease = $game->latestRelease;
409 if ($game->discover) {
413 'thumbnail' => $game->discover,
414 'full' => $game->discover,
418 foreach ($game->media as $medium) {
419 if ($medium->type == 'image') {
423 'thumbnail' => $medium->thumb ?? $medium->url,
424 'full' => $medium->url,
428 if (!isUnsupportedVideoUrl($medium->url)) {
431 'url' => $medium->url,
438 if (isset($game->links->unlocked)) {
440 'text' => 'Show unlocked',
441 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
447 $gamePromoted = getPromotedProduct($game);
449 $product = buildProduct($gamePromoted);
453 if (isset($game->latestRelease->url)
454 && substr($game->latestRelease->url, 0, 29) == 'https://archive.org/download/'
456 $iaUrl = dirname($game->latestRelease->url) . '/';
459 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
462 'title' => $game->title,
463 'description' => $game->description,
464 'gamerNumbers' => $game->players,
465 'genres' => $game->genres,
467 'suggestedAge' => $game->contentRating,
468 'premium' => $game->premium,
469 'inAppPurchases' => $game->inAppPurchases,
470 'firstPublishedAt' => strtotime($game->firstPublishedAt),
474 'count' => $game->rating->count,
475 'average' => $game->rating->average,
479 'fileSize' => $latestRelease->size,
480 'nativeSize' => $latestRelease->nativeSize,
481 'publicSize' => $latestRelease->publicSize,
482 'md5sum' => $latestRelease->md5sum,
483 'filename' => 'FIXME',
485 'package' => $game->packageName,
486 'versionCode' => $latestRelease->versionCode,
487 'state' => 'complete',
491 'number' => $latestRelease->name,
492 'publishedAt' => strtotime($latestRelease->date),
493 'uuid' => $latestRelease->uuid,
497 'name' => $game->developer->name,
498 'founder' => $game->developer->founder,
502 'key:rating.average',
503 'key:developer.name',
505 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
508 'tileImage' => $game->discover,
509 'mediaTiles' => $mediaTiles,
510 'mobileAppIcon' => null,
515 'promotedProduct' => $product,
516 'buttons' => $buttons,
519 'internet-archive' => $iaUrl,
524 function buildDeveloperCurrentGamer()
528 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
529 'username' => 'stouyapi',
535 * For /api/v1/developers/xxx/products/?only=yyy
537 function buildDeveloperProductOnly($product, $developer)
540 'developerName' => $developer->name,
541 'currency' => $product->currency,
543 buildProduct($product),
549 * For /api/v1/developers/xxx/products/
551 function buildDeveloperProducts($products, $developer)
554 foreach ($products as $product) {
555 $jsonProducts[] = buildProduct($product);
558 'developerName' => $developer->name,
559 'currency' => $products[0]->currency ?? 'EUR',
560 'products' => $jsonProducts,
564 function buildPurchases($game)
569 $promotedProduct = getPromotedProduct($game);
570 if ($promotedProduct) {
571 $purchasesData['purchases'][] = [
572 'purchaseDate' => time() * 1000,
573 'generateDate' => time() * 1000,
574 'identifier' => $promotedProduct->identifier,
575 'gamer' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
576 'uuid' => '00702342-0000-1111-2222-c3e1500beef3',//transaction ID
577 'priceInCents' => $promotedProduct->originalPrice * 100,
578 'localPrice' => $promotedProduct->localPrice,
579 'currency' => $promotedProduct->currency,
583 $encryptedOnce = dummyEncrypt($purchasesData);
584 $encryptedTwice = dummyEncrypt($encryptedOnce);
585 return $encryptedTwice;
588 function buildSearch($games)
590 $games = sortByTitle($games);
592 foreach ($games as $game) {
594 'title' => $game->title,
595 'url' => 'ouya://launcher/details?app=' . $game->packageName,
596 'contentRating' => $game->contentRating,
600 'count' => count($results),
601 'results' => $results,
605 function dummyEncrypt($data)
608 'key' => base64_encode('0123456789abcdef'),
609 'iv' => 't3jir1LHpICunvhlM76edQ==',//random bytes
610 'blob' => base64_encode(
611 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
616 function addChunkedDiscoverRows(&$data, $games, $title)
618 $chunks = array_chunk($games, 4);
620 foreach ($chunks as $chunk) {
622 $data, $first ? $title : '',
629 function addDiscoverRow(&$data, $title, $games)
637 foreach ($games as $game) {
638 if (is_string($game)) {
640 $tilePos = count($data['tiles']);
641 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
645 if (isset($game->links->original)) {
646 //do not link unlocked games.
647 // people an access them via the original games
650 $tilePos = findTile($data['tiles'], $game->packageName);
651 if ($tilePos === null) {
652 $tilePos = count($data['tiles']);
653 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
656 $row['tiles'][] = $tilePos;
658 $data['rows'][] = $row;
661 function findTile($tiles, $packageName)
663 foreach ($tiles as $pos => $tile) {
664 if ($tile['package'] == $packageName) {
671 function buildDiscoverCategoryTile($title)
674 'url' => 'ouya://launcher/discover/' . categoryPath($title),
681 function buildDiscoverGameTile($game)
683 $latestRelease = $game->latestRelease;
685 'gamerNumbers' => $game->players,
686 'genres' => $game->genres,
687 'url' => 'ouya://launcher/details?app=' . $game->packageName,
690 'md5sum' => $latestRelease->md5sum,
692 'versionNumber' => $latestRelease->name,
693 'uuid' => $latestRelease->uuid,
695 'inAppPurchases' => $game->inAppPurchases,
696 'promotedProduct' => null,
697 'premium' => $game->premium,
699 'package' => $game->packageName,
700 'updated_at' => strtotime($latestRelease->date),
701 'updatedAt' => $latestRelease->date,
702 'title' => $game->title,
703 'image' => $game->discover,
704 'contentRating' => $game->contentRating,
706 'count' => $game->rating->count,
707 'average' => $game->rating->average,
709 'promotedProduct' => buildProduct(getPromotedProduct($game)),
713 function getAllAges($games)
716 foreach ($games as $game) {
717 $ages[] = $game->contentRating;
719 return array_unique($ages);
722 function getAllGenres($games)
725 foreach ($games as $game) {
726 $genres = array_merge($genres, $game->genres);
728 return array_unique($genres);
731 function addMissingGameProperties($game)
733 if (!isset($game->overview)) {
734 $game->overview = null;
736 if (!isset($game->description)) {
737 $game->description = '';
739 if (!isset($game->players)) {
740 $game->players = [1];
742 if (!isset($game->genres)) {
743 $game->genres = ['Unsorted'];
745 if (!isset($game->website)) {
746 $game->website = null;
748 if (!isset($game->contentRating)) {
749 $game->contentRating = 'Everyone';
751 if (!isset($game->premium)) {
752 $game->premium = false;
754 if (!isset($game->firstPublishedAt)) {
755 $game->firstPublishedAt = gmdate('c');
758 if (!isset($game->rating)) {
759 $game->rating = new stdClass();
761 if (!isset($game->rating->likeCount)) {
762 $game->rating->likeCount = 0;
764 if (!isset($game->rating->average)) {
765 $game->rating->average = 0;
767 if (!isset($game->rating->count)) {
768 $game->rating->count = 0;
771 $game->latestRelease = null;
772 $latestReleaseTimestamp = 0;
773 foreach ($game->releases as $release) {
774 if (!isset($release->publicSize)) {
775 $release->publicSize = 0;
777 if (!isset($release->nativeSize)) {
778 $release->nativeSize = 0;
781 $releaseTimestamp = strtotime($release->date);
782 if ($releaseTimestamp > $latestReleaseTimestamp) {
783 $game->latestRelease = $release;
784 $latestReleaseTimestamp = $releaseTimestamp;
787 if ($game->latestRelease === null) {
788 error('No latest release for ' . $game->packageName);
791 if (!isset($game->media)) {
795 if (!isset($game->developer->uuid)) {
796 $game->developer->uuid = null;
798 if (!isset($game->developer->name)) {
799 $game->developer->name = 'unknown';
801 if (!isset($game->developer->supportEmail)) {
802 $game->developer->supportEmail = null;
804 if (!isset($game->developer->supportPhone)) {
805 $game->developer->supportPhone = null;
807 if (!isset($game->developer->founder)) {
808 $game->developer->founder = false;
813 * Implements a sensible ranking system described in
814 * https://stackoverflow.com/a/1411268/2826013
816 function calculateRank(array $games)
818 $averageRatings = array_map(
820 return $game->rating->average;
824 $average = array_sum($averageRatings) / count($averageRatings);
828 foreach ($games as $game) {
829 $R = $game->rating->average;
830 $v = $game->rating->count;
831 $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
835 function getFirstVideoUrl($media)
837 foreach ($media as $medium) {
838 if ($medium->type == 'video') {
845 function getAllImageUrls($media)
848 foreach ($media as $medium) {
849 if ($medium->type == 'image') {
850 $imageUrls[] = $medium->url;
856 function getPromotedProduct($game)
858 if (!isset($game->products) || !count($game->products)) {
861 foreach ($game->products as $gameProd) {
862 if ($gameProd->promoted) {
870 * vimeo only work with HTTPS now,
871 * and the OUYA does not support SNI.
872 * We get SSL errors and no video for them :/
874 function isUnsupportedVideoUrl($url)
876 return strpos($url, '://vimeo.com/') !== false;
879 function removeMakeGames(array $games)
881 return filterByGenre($games, 'Tutorials', true);
884 function removeMakeGenres($genres)
887 foreach ($genres as $genre) {
888 if ($genre != 'Tutorials' && $genre != 'Builds') {
889 $filtered[] = $genre;
895 function rewriteUrl($url)
897 foreach ($GLOBALS['urlRewrites'] as $pattern => $replacement) {
898 $url = preg_replace($pattern, $replacement, $url);
903 function writeJson($path, $data)
906 $fullPath = $wwwDir . $path;
907 $dir = dirname($fullPath);
909 mkdir($dir, 0777, true);
913 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
919 fwrite(STDERR, $msg . "\n");