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'))
129 function buildDiscover(array $games)
131 $games = removeMakeGames($games);
133 'title' => 'DISCOVER',
139 $data, 'Last Updated',
140 filterLastUpdated($games, 10)
144 filterBestRated($games, 10)
147 $data, "cweiske's picks",
148 filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
157 addDiscoverRow($data, '# of players', $players);
158 foreach ($players as $num => $title) {
160 'api/v1/discover-data/' . categoryPath($title) . '.json',
161 buildDiscoverCategory($title, filterByPlayers($games, $num))
165 $ages = getAllAges($games);
167 addDiscoverRow($data, 'Content rating', $ages);
168 foreach ($ages as $num => $title) {
170 'api/v1/discover-data/' . categoryPath($title) . '.json',
171 buildDiscoverCategory($title, filterByAge($games, $title))
175 $genres = removeMakeGenres(getAllGenres($games));
177 addChunkedDiscoverRows($data, $genres, 'Genres');
179 foreach ($genres as $genre) {
181 'api/v1/discover-data/' . categoryPath($genre) . '.json',
182 buildDiscoverCategory($genre, filterByGenre($games, $genre))
186 $abc = array_merge(range('A', 'Z'), ['Other']);
187 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
188 foreach ($abc as $letter) {
190 'api/v1/discover-data/' . categoryPath($letter) . '.json',
191 buildDiscoverCategory($letter, filterByLetter($games, $letter))
199 * A genre category page
201 function buildDiscoverCategory($name, $games)
209 $data, 'Last Updated',
210 filterLastUpdated($games, 10)
214 filterBestRated($games, 10)
219 function ($gameA, $gameB) {
220 return strcasecmp($gameA->title, $gameB->title);
223 $chunks = array_chunk($games, 4);
224 foreach ($chunks as $chunkGames) {
225 addDiscoverRow($data, '', $chunkGames);
231 function buildMakeCategory($name, $games)
241 function ($gameA, $gameB) {
242 return strcasecmp($gameA->title, $gameB->title);
245 addDiscoverRow($data, '', $games);
250 function buildDiscoverHome(array $games)
252 //we do not want anything here for now
257 'title' => 'FEATURED',
258 'showPrice' => false,
269 * Build api/v1/apps/$packageName
271 function buildApps($game)
273 $latestRelease = $game->latestRelease;
276 $gamePromoted = getPromotedProduct($game);
278 $product = buildProduct($gamePromoted);
281 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
284 'uuid' => $latestRelease->uuid,
285 'title' => $game->title,
286 'overview' => $game->overview,
287 'description' => $game->description,
288 'gamerNumbers' => $game->players,
289 'genres' => $game->genres,
291 'website' => $game->website,
292 'contentRating' => $game->contentRating,
293 'premium' => $game->premium,
294 'firstPublishedAt' => $game->firstPublishedAt,
296 'likeCount' => $game->rating->likeCount,
297 'ratingAverage' => $game->rating->average,
298 'ratingCount' => $game->rating->count,
300 'versionNumber' => $latestRelease->name,
301 'latestVersion' => $latestRelease->uuid,
302 'md5sum' => $latestRelease->md5sum,
303 'apkFileSize' => $latestRelease->size,
304 'publishedAt' => $latestRelease->date,
305 'publicSize' => $latestRelease->publicSize,
306 'nativeSize' => $latestRelease->nativeSize,
308 'mainImageFullUrl' => $game->discover,
309 'videoUrl' => getFirstVideoUrl($game->media),
310 'filepickerScreenshots' => getAllImageUrls($game->media),
311 'mobileAppIcon' => null,
313 'developer' => $game->developer->name,
314 'supportEmailAddress' => $game->developer->supportEmail,
315 'supportPhone' => $game->developer->supportPhone,
316 'founder' => $game->developer->founder,
318 'promotedProduct' => $product,
323 function buildAppDownload($game, $release)
327 'fileSize' => $release->size,
328 'version' => $release->uuid,
329 'contentRating' => $game->contentRating,
330 'downloadLink' => $release->url,
335 function buildProduct($product)
337 if ($product === null) {
341 'type' => 'entitlement',
342 'identifier' => $product->identifier,
343 'name' => $product->name,
344 'description' => $product->description ?? '',
345 'localPrice' => $product->localPrice,
346 'originalPrice' => $product->originalPrice,
348 'currency' => $product->currency,
353 * Build /app/v1/details?app=org.example.game
355 function buildDetails($game)
357 $latestRelease = $game->latestRelease;
360 if ($game->discover) {
364 'thumbnail' => $game->discover,
365 'full' => $game->discover,
369 foreach ($game->media as $medium) {
370 if ($medium->type == 'image') {
374 'thumbnail' => $medium->thumb,
375 'full' => $medium->url,
381 'url' => $medium->url,
387 if (isset($game->links->unlocked)) {
389 'text' => 'Show unlocked',
390 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
396 $gamePromoted = getPromotedProduct($game);
398 $product = buildProduct($gamePromoted);
401 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
404 'title' => $game->title,
405 'description' => $game->description,
406 'gamerNumbers' => $game->players,
407 'genres' => $game->genres,
409 'suggestedAge' => $game->contentRating,
410 'premium' => $game->premium,
411 'inAppPurchases' => $game->inAppPurchases,
412 'firstPublishedAt' => strtotime($game->firstPublishedAt),
416 'count' => $game->rating->count,
417 'average' => $game->rating->average,
421 'fileSize' => $latestRelease->size,
422 'nativeSize' => $latestRelease->nativeSize,
423 'publicSize' => $latestRelease->publicSize,
424 'md5sum' => $latestRelease->md5sum,
425 'filename' => 'FIXME',
427 'package' => $game->packageName,
428 'versionCode' => $latestRelease->versionCode,
429 'state' => 'complete',
433 'number' => $latestRelease->name,
434 'publishedAt' => strtotime($latestRelease->date),
435 'uuid' => $latestRelease->uuid,
439 'name' => $game->developer->name,
440 'founder' => $game->developer->founder,
444 'key:rating.average',
445 'key:developer.name',
447 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
450 'tileImage' => $game->discover,
451 'mediaTiles' => $mediaTiles,
452 'mobileAppIcon' => null,
457 'promotedProduct' => $product,
458 'buttons' => $buttons,
462 function buildDeveloperCurrentGamer()
466 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
467 'username' => 'stouyapi',
473 * For /api/v1/developers/xxx/products/?only=yyy
475 function buildDeveloperProductOnly($product, $developer)
478 'developerName' => $developer->name,
479 'currency' => $product->currency,
481 buildProduct($product),
487 * For /api/v1/developers/xxx/products/
489 function buildDeveloperProducts($products, $developer)
492 foreach ($products as $product) {
493 $jsonProducts[] = buildProduct($product);
496 'developerName' => $developer->name,
497 'currency' => $products[0]->currency ?? 'EUR',
498 'products' => $jsonProducts,
502 function buildPurchases($game)
507 $promotedProduct = getPromotedProduct($game);
508 if ($promotedProduct) {
509 $purchasesData['purchases'][] = [
510 'purchaseDate' => time() * 1000,
511 'generateDate' => time() * 1000,
512 'identifier' => $promotedProduct->identifier,
513 'gamer' => 'stouyapi',
514 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
515 'priceInCents' => $promotedProduct->originalPrice * 100,
516 'localPrice' => $promotedProduct->localPrice,
517 'currency' => $promotedProduct->currency,
521 $encryptedOnce = dummyEncrypt($purchasesData);
522 $encryptedTwice = dummyEncrypt($encryptedOnce);
523 return $encryptedTwice;
526 function dummyEncrypt($data)
529 'key' => base64_encode('0123456789abcdef') . "\n",
530 'iv' => 't3jir1LHpICunvhlM76edQ==' . "\n",//random bytes
531 'blob' => base64_encode(
532 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
537 function addChunkedDiscoverRows(&$data, $games, $title)
539 $chunks = array_chunk($games, 4);
541 foreach ($chunks as $chunk) {
543 $data, $first ? $title : '',
550 function addDiscoverRow(&$data, $title, $games)
558 foreach ($games as $game) {
559 if (is_string($game)) {
561 $tilePos = count($data['tiles']);
562 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
566 if (isset($game->links->original)) {
567 //do not link unlocked games.
568 // people an access them via the original games
571 $tilePos = findTile($data['tiles'], $game->packageName);
572 if ($tilePos === null) {
573 $tilePos = count($data['tiles']);
574 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
577 $row['tiles'][] = $tilePos;
579 $data['rows'][] = $row;
582 function findTile($tiles, $packageName)
584 foreach ($tiles as $pos => $tile) {
585 if ($tile['package'] == $packageName) {
592 function buildDiscoverCategoryTile($title)
595 'url' => 'ouya://launcher/discover/' . categoryPath($title),
602 function buildDiscoverGameTile($game)
604 $latestRelease = $game->latestRelease;
606 'gamerNumbers' => $game->players,
607 'genres' => $game->genres,
608 'url' => 'ouya://launcher/details?app=' . $game->packageName,
611 'md5sum' => $latestRelease->md5sum,
613 'versionNumber' => $latestRelease->name,
614 'uuid' => $latestRelease->uuid,
616 'inAppPurchases' => $game->inAppPurchases,
617 'promotedProduct' => null,
618 'premium' => $game->premium,
620 'package' => $game->packageName,
621 'updated_at' => strtotime($latestRelease->date),
622 'updatedAt' => $latestRelease->date,
623 'title' => $game->title,
624 'image' => $game->discover,
625 'contentRating' => $game->contentRating,
627 'count' => $game->rating->count,
628 'average' => $game->rating->average,
630 'promotedProduct' => buildProduct(getPromotedProduct($game)),
634 function categoryPath($title)
636 return str_replace(['/', '\\', ' ', '+'], '_', $title);
639 function getAllAges($games)
642 foreach ($games as $game) {
643 $ages[] = $game->contentRating;
645 return array_unique($ages);
648 function getAllGenres($games)
651 foreach ($games as $game) {
652 $genres = array_merge($genres, $game->genres);
654 return array_unique($genres);
657 function addMissingGameProperties($game)
659 if (!isset($game->overview)) {
660 $game->overview = null;
662 if (!isset($game->description)) {
663 $game->description = '';
665 if (!isset($game->players)) {
666 $game->players = [1];
668 if (!isset($game->genres)) {
669 $game->genres = ['Unsorted'];
671 if (!isset($game->website)) {
672 $game->website = null;
674 if (!isset($game->contentRating)) {
675 $game->contentRating = 'Everyone';
677 if (!isset($game->premium)) {
678 $game->premium = false;
680 if (!isset($game->firstPublishedAt)) {
681 $game->firstPublishedAt = gmdate('c');
684 if (!isset($game->rating)) {
685 $game->rating = new stdClass();
687 if (!isset($game->rating->likeCount)) {
688 $game->rating->likeCount = 0;
690 if (!isset($game->rating->average)) {
691 $game->rating->average = 0;
693 if (!isset($game->rating->count)) {
694 $game->rating->count = 0;
697 $game->latestRelease = null;
698 $latestReleaseTimestamp = 0;
699 foreach ($game->releases as $release) {
700 if (!isset($release->publicSize)) {
701 $release->publicSize = 0;
703 if (!isset($release->nativeSize)) {
704 $release->nativeSize = 0;
707 $releaseTimestamp = strtotime($release->date);
708 if ($releaseTimestamp > $latestReleaseTimestamp) {
709 $game->latestRelease = $release;
710 $latestReleaseTimestamp = $releaseTimestamp;
713 if ($game->latestRelease === null) {
714 error('No latest release for ' . $game->packageName);
717 if (!isset($game->media)) {
721 if (!isset($game->developer->uuid)) {
722 $game->developer->uuid = null;
724 if (!isset($game->developer->name)) {
725 $game->developer->name = 'unknown';
727 if (!isset($game->developer->supportEmail)) {
728 $game->developer->supportEmail = null;
730 if (!isset($game->developer->supportPhone)) {
731 $game->developer->supportPhone = null;
733 if (!isset($game->developer->founder)) {
734 $game->developer->founder = false;
739 * Implements a sensible ranking system described in
740 * https://stackoverflow.com/a/1411268/2826013
742 function calculateRank(array $games)
744 $averageRatings = array_map(
746 return $game->rating->average;
750 $average = array_sum($averageRatings) / count($averageRatings);
754 foreach ($games as $game) {
755 $R = $game->rating->average;
756 $v = $game->rating->count;
757 $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
761 function getFirstVideoUrl($media)
763 foreach ($media as $medium) {
764 if ($medium->type == 'video') {
771 function getAllImageUrls($media)
774 foreach ($media as $medium) {
775 if ($medium->type == 'image') {
776 $imageUrls[] = $medium->url;
782 function getPromotedProduct($game)
784 if (!isset($game->products) || !count($game->products)) {
787 foreach ($game->products as $gameProd) {
788 if ($gameProd->promoted) {
795 function removeMakeGames(array $games)
797 return filterByGenre($games, 'Tutorials', true);
800 function removeMakeGenres($genres)
803 foreach ($genres as $genre) {
804 if ($genre != 'Tutorials' && $genre != 'Builds') {
805 $filtered[] = $genre;
811 function writeJson($path, $data)
814 $fullPath = $wwwDir . $path;
815 $dir = dirname($fullPath);
817 mkdir($dir, 0777, true);
821 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
827 fwrite(STDERR, $msg . "\n");