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'
24 $wwwDir = __DIR__ . '/../www/';
26 $baseDir = dirname($foldersFile);
28 foreach (file($foldersFile) as $line) {
31 if (strpos($line, '..') !== false) {
32 error('Path attack in ' . $folder);
34 $folder = $baseDir . '/' . $line;
35 if (!is_dir($folder)) {
36 error('Folder does not exist: ' . $folder);
38 $gameFiles = array_merge($gameFiles, glob($folder . '/*.json'));
45 foreach ($gameFiles as $gameFile) {
46 $game = json_decode(file_get_contents($gameFile));
48 error('JSON invalid at ' . $gameFile);
50 addMissingGameProperties($game);
51 $games[$game->packageName] = $game;
54 'api/v1/details-data/' . $game->packageName . '.json',
58 if (!isset($developers[$game->developer->uuid])) {
59 $developers[$game->developer->uuid] = [
60 'info' => $game->developer,
65 $products = $game->products ?? [];
66 foreach ($products as $product) {
68 'api/v1/developers/' . $game->developer->uuid
69 . '/products/' . $product->identifier . '.json',
70 buildDeveloperProductOnly($product, $game->developer)
72 $developers[$game->developer->uuid]['products'][] = $product;
77 'api/v1/games/' . $game->packageName . '/purchases',
82 'api/v1/apps/' . $game->packageName . '.json',
85 $latestRelease = $game->latestRelease;
87 'api/v1/apps/' . $latestRelease->uuid . '.json',
92 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
93 buildAppDownload($game, $latestRelease)
101 calculateRank($games);
103 foreach ($developers as $developer) {
105 //index.htm does not need a rewrite rule
106 'api/v1/developers/' . $developer['info']->uuid
107 . '/products/index.htm',
108 buildDeveloperProducts($developer['products'], $developer['info'])
111 //index.htm does not need a rewrite rule
112 'api/v1/developers/' . $developer['info']->uuid
114 buildDeveloperCurrentGamer()
118 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
119 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
123 'api/v1/discover-data/tutorials.json',
124 buildMakeCategory('Tutorials', filterByGenre($games, 'Tutorials'))
127 $searchLetters = 'abcdefghijklmnopqrstuvwxyz0123456789., ';
128 foreach (str_split($searchLetters) as $letter) {
129 $letterGames = filterBySearchWord($games, $letter);
131 'api/v1/search-data/' . $letter . '.json',
132 buildSearch($letterGames)
137 function buildDiscover(array $games)
139 $games = removeMakeGames($games);
141 'title' => 'DISCOVER',
147 $data, 'Last Updated',
148 filterLastUpdated($games, 10)
152 filterBestRated($games, 10)
155 $data, "cweiske's picks",
156 filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
165 addDiscoverRow($data, '# of players', $players);
166 foreach ($players as $num => $title) {
168 'api/v1/discover-data/' . categoryPath($title) . '.json',
169 buildDiscoverCategory($title, filterByPlayers($games, $num))
173 $ages = getAllAges($games);
175 addDiscoverRow($data, 'Content rating', $ages);
176 foreach ($ages as $num => $title) {
178 'api/v1/discover-data/' . categoryPath($title) . '.json',
179 buildDiscoverCategory($title, filterByAge($games, $title))
183 $genres = removeMakeGenres(getAllGenres($games));
185 addChunkedDiscoverRows($data, $genres, 'Genres');
187 foreach ($genres as $genre) {
189 'api/v1/discover-data/' . categoryPath($genre) . '.json',
190 buildDiscoverCategory($genre, filterByGenre($games, $genre))
194 $abc = array_merge(range('A', 'Z'), ['Other']);
195 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
196 foreach ($abc as $letter) {
198 'api/v1/discover-data/' . categoryPath($letter) . '.json',
199 buildDiscoverCategory($letter, filterByLetter($games, $letter))
207 * A genre category page
209 function buildDiscoverCategory($name, $games)
217 $data, 'Last Updated',
218 filterLastUpdated($games, 10)
222 filterBestRated($games, 10)
225 $games = sortByTitle($games);
226 $chunks = array_chunk($games, 4);
227 foreach ($chunks as $chunkGames) {
228 addDiscoverRow($data, '', $chunkGames);
234 function buildMakeCategory($name, $games)
242 $games = sortByTitle($games);
243 addDiscoverRow($data, '', $games);
248 function buildDiscoverHome(array $games)
250 //we do not want anything here for now
255 'title' => 'FEATURED',
256 'showPrice' => false,
267 * Build api/v1/apps/$packageName
269 function buildApps($game)
271 $latestRelease = $game->latestRelease;
274 $gamePromoted = getPromotedProduct($game);
276 $product = buildProduct($gamePromoted);
279 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
282 'uuid' => $latestRelease->uuid,
283 'title' => $game->title,
284 'overview' => $game->overview,
285 'description' => $game->description,
286 'gamerNumbers' => $game->players,
287 'genres' => $game->genres,
289 'website' => $game->website,
290 'contentRating' => $game->contentRating,
291 'premium' => $game->premium,
292 'firstPublishedAt' => $game->firstPublishedAt,
294 'likeCount' => $game->rating->likeCount,
295 'ratingAverage' => $game->rating->average,
296 'ratingCount' => $game->rating->count,
298 'versionNumber' => $latestRelease->name,
299 'latestVersion' => $latestRelease->uuid,
300 'md5sum' => $latestRelease->md5sum,
301 'apkFileSize' => $latestRelease->size,
302 'publishedAt' => $latestRelease->date,
303 'publicSize' => $latestRelease->publicSize,
304 'nativeSize' => $latestRelease->nativeSize,
306 'mainImageFullUrl' => $game->discover,
307 'videoUrl' => getFirstVideoUrl($game->media),
308 'filepickerScreenshots' => getAllImageUrls($game->media),
309 'mobileAppIcon' => null,
311 'developer' => $game->developer->name,
312 'supportEmailAddress' => $game->developer->supportEmail,
313 'supportPhone' => $game->developer->supportPhone,
314 'founder' => $game->developer->founder,
316 'promotedProduct' => $product,
321 function buildAppDownload($game, $release)
325 'fileSize' => $release->size,
326 'version' => $release->uuid,
327 'contentRating' => $game->contentRating,
328 'downloadLink' => $release->url,
333 function buildProduct($product)
335 if ($product === null) {
339 'type' => 'entitlement',
340 'identifier' => $product->identifier,
341 'name' => $product->name,
342 'description' => $product->description ?? '',
343 'localPrice' => $product->localPrice,
344 'originalPrice' => $product->originalPrice,
346 'currency' => $product->currency,
351 * Build /app/v1/details?app=org.example.game
353 function buildDetails($game)
355 $latestRelease = $game->latestRelease;
358 if ($game->discover) {
362 'thumbnail' => $game->discover,
363 'full' => $game->discover,
367 foreach ($game->media as $medium) {
368 if ($medium->type == 'image') {
372 'thumbnail' => $medium->thumb,
373 'full' => $medium->url,
379 'url' => $medium->url,
385 if (isset($game->links->unlocked)) {
387 'text' => 'Show unlocked',
388 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
394 $gamePromoted = getPromotedProduct($game);
396 $product = buildProduct($gamePromoted);
399 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
402 'title' => $game->title,
403 'description' => $game->description,
404 'gamerNumbers' => $game->players,
405 'genres' => $game->genres,
407 'suggestedAge' => $game->contentRating,
408 'premium' => $game->premium,
409 'inAppPurchases' => $game->inAppPurchases,
410 'firstPublishedAt' => strtotime($game->firstPublishedAt),
414 'count' => $game->rating->count,
415 'average' => $game->rating->average,
419 'fileSize' => $latestRelease->size,
420 'nativeSize' => $latestRelease->nativeSize,
421 'publicSize' => $latestRelease->publicSize,
422 'md5sum' => $latestRelease->md5sum,
423 'filename' => 'FIXME',
425 'package' => $game->packageName,
426 'versionCode' => $latestRelease->versionCode,
427 'state' => 'complete',
431 'number' => $latestRelease->name,
432 'publishedAt' => strtotime($latestRelease->date),
433 'uuid' => $latestRelease->uuid,
437 'name' => $game->developer->name,
438 'founder' => $game->developer->founder,
442 'key:rating.average',
443 'key:developer.name',
445 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
448 'tileImage' => $game->discover,
449 'mediaTiles' => $mediaTiles,
450 'mobileAppIcon' => null,
455 'promotedProduct' => $product,
456 'buttons' => $buttons,
460 function buildDeveloperCurrentGamer()
464 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
465 'username' => 'stouyapi',
471 * For /api/v1/developers/xxx/products/?only=yyy
473 function buildDeveloperProductOnly($product, $developer)
476 'developerName' => $developer->name,
477 'currency' => $product->currency,
479 buildProduct($product),
485 * For /api/v1/developers/xxx/products/
487 function buildDeveloperProducts($products, $developer)
490 foreach ($products as $product) {
491 $jsonProducts[] = buildProduct($product);
494 'developerName' => $developer->name,
495 'currency' => $products[0]->currency ?? 'EUR',
496 'products' => $jsonProducts,
500 function buildPurchases($game)
505 $promotedProduct = getPromotedProduct($game);
506 if ($promotedProduct) {
507 $purchasesData['purchases'][] = [
508 'purchaseDate' => time() * 1000,
509 'generateDate' => time() * 1000,
510 'identifier' => $promotedProduct->identifier,
511 'gamer' => 'stouyapi',
512 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
513 'priceInCents' => $promotedProduct->originalPrice * 100,
514 'localPrice' => $promotedProduct->localPrice,
515 'currency' => $promotedProduct->currency,
519 $encryptedOnce = dummyEncrypt($purchasesData);
520 $encryptedTwice = dummyEncrypt($encryptedOnce);
521 return $encryptedTwice;
524 function buildSearch($games)
526 $games = sortByTitle($games);
528 foreach ($games as $game) {
530 'title' => $game->title,
531 'url' => 'ouya://launcher/details?app=' . $game->packageName,
532 'contentRating' => $game->contentRating,
536 'count' => count($results),
537 'results' => $results,
541 function dummyEncrypt($data)
544 'key' => base64_encode('0123456789abcdef') . "\n",
545 'iv' => 't3jir1LHpICunvhlM76edQ==' . "\n",//random bytes
546 'blob' => base64_encode(
547 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
552 function addChunkedDiscoverRows(&$data, $games, $title)
554 $chunks = array_chunk($games, 4);
556 foreach ($chunks as $chunk) {
558 $data, $first ? $title : '',
565 function addDiscoverRow(&$data, $title, $games)
573 foreach ($games as $game) {
574 if (is_string($game)) {
576 $tilePos = count($data['tiles']);
577 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
581 if (isset($game->links->original)) {
582 //do not link unlocked games.
583 // people an access them via the original games
586 $tilePos = findTile($data['tiles'], $game->packageName);
587 if ($tilePos === null) {
588 $tilePos = count($data['tiles']);
589 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
592 $row['tiles'][] = $tilePos;
594 $data['rows'][] = $row;
597 function findTile($tiles, $packageName)
599 foreach ($tiles as $pos => $tile) {
600 if ($tile['package'] == $packageName) {
607 function buildDiscoverCategoryTile($title)
610 'url' => 'ouya://launcher/discover/' . categoryPath($title),
617 function buildDiscoverGameTile($game)
619 $latestRelease = $game->latestRelease;
621 'gamerNumbers' => $game->players,
622 'genres' => $game->genres,
623 'url' => 'ouya://launcher/details?app=' . $game->packageName,
626 'md5sum' => $latestRelease->md5sum,
628 'versionNumber' => $latestRelease->name,
629 'uuid' => $latestRelease->uuid,
631 'inAppPurchases' => $game->inAppPurchases,
632 'promotedProduct' => null,
633 'premium' => $game->premium,
635 'package' => $game->packageName,
636 'updated_at' => strtotime($latestRelease->date),
637 'updatedAt' => $latestRelease->date,
638 'title' => $game->title,
639 'image' => $game->discover,
640 'contentRating' => $game->contentRating,
642 'count' => $game->rating->count,
643 'average' => $game->rating->average,
645 'promotedProduct' => buildProduct(getPromotedProduct($game)),
649 function categoryPath($title)
651 return str_replace(['/', '\\', ' ', '+', '?'], '_', $title);
654 function getAllAges($games)
657 foreach ($games as $game) {
658 $ages[] = $game->contentRating;
660 return array_unique($ages);
663 function getAllGenres($games)
666 foreach ($games as $game) {
667 $genres = array_merge($genres, $game->genres);
669 return array_unique($genres);
672 function addMissingGameProperties($game)
674 if (!isset($game->overview)) {
675 $game->overview = null;
677 if (!isset($game->description)) {
678 $game->description = '';
680 if (!isset($game->players)) {
681 $game->players = [1];
683 if (!isset($game->genres)) {
684 $game->genres = ['Unsorted'];
686 if (!isset($game->website)) {
687 $game->website = null;
689 if (!isset($game->contentRating)) {
690 $game->contentRating = 'Everyone';
692 if (!isset($game->premium)) {
693 $game->premium = false;
695 if (!isset($game->firstPublishedAt)) {
696 $game->firstPublishedAt = gmdate('c');
699 if (!isset($game->rating)) {
700 $game->rating = new stdClass();
702 if (!isset($game->rating->likeCount)) {
703 $game->rating->likeCount = 0;
705 if (!isset($game->rating->average)) {
706 $game->rating->average = 0;
708 if (!isset($game->rating->count)) {
709 $game->rating->count = 0;
712 $game->latestRelease = null;
713 $latestReleaseTimestamp = 0;
714 foreach ($game->releases as $release) {
715 if (!isset($release->publicSize)) {
716 $release->publicSize = 0;
718 if (!isset($release->nativeSize)) {
719 $release->nativeSize = 0;
722 $releaseTimestamp = strtotime($release->date);
723 if ($releaseTimestamp > $latestReleaseTimestamp) {
724 $game->latestRelease = $release;
725 $latestReleaseTimestamp = $releaseTimestamp;
728 if ($game->latestRelease === null) {
729 error('No latest release for ' . $game->packageName);
732 if (!isset($game->media)) {
736 if (!isset($game->developer->uuid)) {
737 $game->developer->uuid = null;
739 if (!isset($game->developer->name)) {
740 $game->developer->name = 'unknown';
742 if (!isset($game->developer->supportEmail)) {
743 $game->developer->supportEmail = null;
745 if (!isset($game->developer->supportPhone)) {
746 $game->developer->supportPhone = null;
748 if (!isset($game->developer->founder)) {
749 $game->developer->founder = false;
754 * Implements a sensible ranking system described in
755 * https://stackoverflow.com/a/1411268/2826013
757 function calculateRank(array $games)
759 $averageRatings = array_map(
761 return $game->rating->average;
765 $average = array_sum($averageRatings) / count($averageRatings);
769 foreach ($games as $game) {
770 $R = $game->rating->average;
771 $v = $game->rating->count;
772 $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
776 function getFirstVideoUrl($media)
778 foreach ($media as $medium) {
779 if ($medium->type == 'video') {
786 function getAllImageUrls($media)
789 foreach ($media as $medium) {
790 if ($medium->type == 'image') {
791 $imageUrls[] = $medium->url;
797 function getPromotedProduct($game)
799 if (!isset($game->products) || !count($game->products)) {
802 foreach ($game->products as $gameProd) {
803 if ($gameProd->promoted) {
810 function removeMakeGames(array $games)
812 return filterByGenre($games, 'Tutorials', true);
815 function removeMakeGenres($genres)
818 foreach ($genres as $genre) {
819 if ($genre != 'Tutorials' && $genre != 'Builds') {
820 $filtered[] = $genre;
826 function writeJson($path, $data)
829 $fullPath = $wwwDir . $path;
830 $dir = dirname($fullPath);
832 mkdir($dir, 0777, true);
836 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
842 fwrite(STDERR, $msg . "\n");