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'])
166 addDiscoverRow($data, '# of players', $players);
167 foreach ($players as $num => $title) {
169 'api/v1/discover-data/' . categoryPath($title) . '.json',
170 buildDiscoverCategory($title, filterByPlayers($games, $num))
174 $ages = getAllAges($games);
176 addDiscoverRow($data, 'Content rating', $ages);
177 foreach ($ages as $num => $title) {
179 'api/v1/discover-data/' . categoryPath($title) . '.json',
180 buildDiscoverCategory($title, filterByAge($games, $title))
184 $genres = removeMakeGenres(getAllGenres($games));
186 addChunkedDiscoverRows($data, $genres, 'Genres');
188 foreach ($genres as $genre) {
190 'api/v1/discover-data/' . categoryPath($genre) . '.json',
191 buildDiscoverCategory($genre, filterByGenre($games, $genre))
195 $abc = array_merge(range('A', 'Z'), ['Other']);
196 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
197 foreach ($abc as $letter) {
199 'api/v1/discover-data/' . categoryPath($letter) . '.json',
200 buildDiscoverCategory($letter, filterByLetter($games, $letter))
208 * A genre category page
210 function buildDiscoverCategory($name, $games)
218 $data, 'Last Updated',
219 filterLastUpdated($games, 10)
223 filterBestRated($games, 10)
226 $games = sortByTitle($games);
227 $chunks = array_chunk($games, 4);
228 foreach ($chunks as $chunkGames) {
229 addDiscoverRow($data, '', $chunkGames);
235 function buildMakeCategory($name, $games)
243 $games = sortByTitle($games);
244 addDiscoverRow($data, '', $games);
249 function buildDiscoverHome(array $games)
251 //we do not want anything here for now
256 'title' => 'FEATURED',
257 'showPrice' => false,
268 * Build api/v1/apps/$packageName
270 function buildApps($game)
272 $latestRelease = $game->latestRelease;
275 $gamePromoted = getPromotedProduct($game);
277 $product = buildProduct($gamePromoted);
280 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
283 'uuid' => $latestRelease->uuid,
284 'title' => $game->title,
285 'overview' => $game->overview,
286 'description' => $game->description,
287 'gamerNumbers' => $game->players,
288 'genres' => $game->genres,
290 'website' => $game->website,
291 'contentRating' => $game->contentRating,
292 'premium' => $game->premium,
293 'firstPublishedAt' => $game->firstPublishedAt,
295 'likeCount' => $game->rating->likeCount,
296 'ratingAverage' => $game->rating->average,
297 'ratingCount' => $game->rating->count,
299 'versionNumber' => $latestRelease->name,
300 'latestVersion' => $latestRelease->uuid,
301 'md5sum' => $latestRelease->md5sum,
302 'apkFileSize' => $latestRelease->size,
303 'publishedAt' => $latestRelease->date,
304 'publicSize' => $latestRelease->publicSize,
305 'nativeSize' => $latestRelease->nativeSize,
307 'mainImageFullUrl' => $game->discover,
308 'videoUrl' => getFirstVideoUrl($game->media),
309 'filepickerScreenshots' => getAllImageUrls($game->media),
310 'mobileAppIcon' => null,
312 'developer' => $game->developer->name,
313 'supportEmailAddress' => $game->developer->supportEmail,
314 'supportPhone' => $game->developer->supportPhone,
315 'founder' => $game->developer->founder,
317 'promotedProduct' => $product,
322 function buildAppDownload($game, $release)
326 'fileSize' => $release->size,
327 'version' => $release->uuid,
328 'contentRating' => $game->contentRating,
329 'downloadLink' => $release->url,
334 function buildProduct($product)
336 if ($product === null) {
340 'type' => 'entitlement',
341 'identifier' => $product->identifier,
342 'name' => $product->name,
343 'description' => $product->description ?? '',
344 'localPrice' => $product->localPrice,
345 'originalPrice' => $product->originalPrice,
347 'currency' => $product->currency,
352 * Build /app/v1/details?app=org.example.game
354 function buildDetails($game)
356 $latestRelease = $game->latestRelease;
359 if ($game->discover) {
363 'thumbnail' => $game->discover,
364 'full' => $game->discover,
368 foreach ($game->media as $medium) {
369 if ($medium->type == 'image') {
373 'thumbnail' => $medium->thumb,
374 'full' => $medium->url,
380 'url' => $medium->url,
386 if (isset($game->links->unlocked)) {
388 'text' => 'Show unlocked',
389 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
395 $gamePromoted = getPromotedProduct($game);
397 $product = buildProduct($gamePromoted);
400 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
403 'title' => $game->title,
404 'description' => $game->description,
405 'gamerNumbers' => $game->players,
406 'genres' => $game->genres,
408 'suggestedAge' => $game->contentRating,
409 'premium' => $game->premium,
410 'inAppPurchases' => $game->inAppPurchases,
411 'firstPublishedAt' => strtotime($game->firstPublishedAt),
415 'count' => $game->rating->count,
416 'average' => $game->rating->average,
420 'fileSize' => $latestRelease->size,
421 'nativeSize' => $latestRelease->nativeSize,
422 'publicSize' => $latestRelease->publicSize,
423 'md5sum' => $latestRelease->md5sum,
424 'filename' => 'FIXME',
426 'package' => $game->packageName,
427 'versionCode' => $latestRelease->versionCode,
428 'state' => 'complete',
432 'number' => $latestRelease->name,
433 'publishedAt' => strtotime($latestRelease->date),
434 'uuid' => $latestRelease->uuid,
438 'name' => $game->developer->name,
439 'founder' => $game->developer->founder,
443 'key:rating.average',
444 'key:developer.name',
446 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
449 'tileImage' => $game->discover,
450 'mediaTiles' => $mediaTiles,
451 'mobileAppIcon' => null,
456 'promotedProduct' => $product,
457 'buttons' => $buttons,
461 function buildDeveloperCurrentGamer()
465 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
466 'username' => 'stouyapi',
472 * For /api/v1/developers/xxx/products/?only=yyy
474 function buildDeveloperProductOnly($product, $developer)
477 'developerName' => $developer->name,
478 'currency' => $product->currency,
480 buildProduct($product),
486 * For /api/v1/developers/xxx/products/
488 function buildDeveloperProducts($products, $developer)
491 foreach ($products as $product) {
492 $jsonProducts[] = buildProduct($product);
495 'developerName' => $developer->name,
496 'currency' => $products[0]->currency ?? 'EUR',
497 'products' => $jsonProducts,
501 function buildPurchases($game)
506 $promotedProduct = getPromotedProduct($game);
507 if ($promotedProduct) {
508 $purchasesData['purchases'][] = [
509 'purchaseDate' => time() * 1000,
510 'generateDate' => time() * 1000,
511 'identifier' => $promotedProduct->identifier,
512 'gamer' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
513 'uuid' => '00702342-0000-1111-2222-c3e1500beef3',//transaction ID
514 'priceInCents' => $promotedProduct->originalPrice * 100,
515 'localPrice' => $promotedProduct->localPrice,
516 'currency' => $promotedProduct->currency,
520 $encryptedOnce = dummyEncrypt($purchasesData);
521 $encryptedTwice = dummyEncrypt($encryptedOnce);
522 return $encryptedTwice;
525 function buildSearch($games)
527 $games = sortByTitle($games);
529 foreach ($games as $game) {
531 'title' => $game->title,
532 'url' => 'ouya://launcher/details?app=' . $game->packageName,
533 'contentRating' => $game->contentRating,
537 'count' => count($results),
538 'results' => $results,
542 function dummyEncrypt($data)
545 'key' => base64_encode('0123456789abcdef') . "\n",
546 'iv' => 't3jir1LHpICunvhlM76edQ==' . "\n",//random bytes
547 'blob' => base64_encode(
548 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
553 function addChunkedDiscoverRows(&$data, $games, $title)
555 $chunks = array_chunk($games, 4);
557 foreach ($chunks as $chunk) {
559 $data, $first ? $title : '',
566 function addDiscoverRow(&$data, $title, $games)
574 foreach ($games as $game) {
575 if (is_string($game)) {
577 $tilePos = count($data['tiles']);
578 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
582 if (isset($game->links->original)) {
583 //do not link unlocked games.
584 // people an access them via the original games
587 $tilePos = findTile($data['tiles'], $game->packageName);
588 if ($tilePos === null) {
589 $tilePos = count($data['tiles']);
590 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
593 $row['tiles'][] = $tilePos;
595 $data['rows'][] = $row;
598 function findTile($tiles, $packageName)
600 foreach ($tiles as $pos => $tile) {
601 if ($tile['package'] == $packageName) {
608 function buildDiscoverCategoryTile($title)
611 'url' => 'ouya://launcher/discover/' . categoryPath($title),
618 function buildDiscoverGameTile($game)
620 $latestRelease = $game->latestRelease;
622 'gamerNumbers' => $game->players,
623 'genres' => $game->genres,
624 'url' => 'ouya://launcher/details?app=' . $game->packageName,
627 'md5sum' => $latestRelease->md5sum,
629 'versionNumber' => $latestRelease->name,
630 'uuid' => $latestRelease->uuid,
632 'inAppPurchases' => $game->inAppPurchases,
633 'promotedProduct' => null,
634 'premium' => $game->premium,
636 'package' => $game->packageName,
637 'updated_at' => strtotime($latestRelease->date),
638 'updatedAt' => $latestRelease->date,
639 'title' => $game->title,
640 'image' => $game->discover,
641 'contentRating' => $game->contentRating,
643 'count' => $game->rating->count,
644 'average' => $game->rating->average,
646 'promotedProduct' => buildProduct(getPromotedProduct($game)),
650 function categoryPath($title)
652 return str_replace(['/', '\\', ' ', '+', '?'], '_', $title);
655 function getAllAges($games)
658 foreach ($games as $game) {
659 $ages[] = $game->contentRating;
661 return array_unique($ages);
664 function getAllGenres($games)
667 foreach ($games as $game) {
668 $genres = array_merge($genres, $game->genres);
670 return array_unique($genres);
673 function addMissingGameProperties($game)
675 if (!isset($game->overview)) {
676 $game->overview = null;
678 if (!isset($game->description)) {
679 $game->description = '';
681 if (!isset($game->players)) {
682 $game->players = [1];
684 if (!isset($game->genres)) {
685 $game->genres = ['Unsorted'];
687 if (!isset($game->website)) {
688 $game->website = null;
690 if (!isset($game->contentRating)) {
691 $game->contentRating = 'Everyone';
693 if (!isset($game->premium)) {
694 $game->premium = false;
696 if (!isset($game->firstPublishedAt)) {
697 $game->firstPublishedAt = gmdate('c');
700 if (!isset($game->rating)) {
701 $game->rating = new stdClass();
703 if (!isset($game->rating->likeCount)) {
704 $game->rating->likeCount = 0;
706 if (!isset($game->rating->average)) {
707 $game->rating->average = 0;
709 if (!isset($game->rating->count)) {
710 $game->rating->count = 0;
713 $game->latestRelease = null;
714 $latestReleaseTimestamp = 0;
715 foreach ($game->releases as $release) {
716 if (!isset($release->publicSize)) {
717 $release->publicSize = 0;
719 if (!isset($release->nativeSize)) {
720 $release->nativeSize = 0;
723 $releaseTimestamp = strtotime($release->date);
724 if ($releaseTimestamp > $latestReleaseTimestamp) {
725 $game->latestRelease = $release;
726 $latestReleaseTimestamp = $releaseTimestamp;
729 if ($game->latestRelease === null) {
730 error('No latest release for ' . $game->packageName);
733 if (!isset($game->media)) {
737 if (!isset($game->developer->uuid)) {
738 $game->developer->uuid = null;
740 if (!isset($game->developer->name)) {
741 $game->developer->name = 'unknown';
743 if (!isset($game->developer->supportEmail)) {
744 $game->developer->supportEmail = null;
746 if (!isset($game->developer->supportPhone)) {
747 $game->developer->supportPhone = null;
749 if (!isset($game->developer->founder)) {
750 $game->developer->founder = false;
755 * Implements a sensible ranking system described in
756 * https://stackoverflow.com/a/1411268/2826013
758 function calculateRank(array $games)
760 $averageRatings = array_map(
762 return $game->rating->average;
766 $average = array_sum($averageRatings) / count($averageRatings);
770 foreach ($games as $game) {
771 $R = $game->rating->average;
772 $v = $game->rating->count;
773 $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
777 function getFirstVideoUrl($media)
779 foreach ($media as $medium) {
780 if ($medium->type == 'video') {
787 function getAllImageUrls($media)
790 foreach ($media as $medium) {
791 if ($medium->type == 'image') {
792 $imageUrls[] = $medium->url;
798 function getPromotedProduct($game)
800 if (!isset($game->products) || !count($game->products)) {
803 foreach ($game->products as $gameProd) {
804 if ($gameProd->promoted) {
811 function removeMakeGames(array $games)
813 return filterByGenre($games, 'Tutorials', true);
816 function removeMakeGenres($genres)
819 foreach ($genres as $genre) {
820 if ($genre != 'Tutorials' && $genre != 'Builds') {
821 $filtered[] = $genre;
827 function writeJson($path, $data)
830 $fullPath = $wwwDir . $path;
831 $dir = dirname($fullPath);
833 mkdir($dir, 0777, true);
837 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
843 fwrite(STDERR, $msg . "\n");