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 /* this crashes babylonian twins
84 'api/v1/games/' . $game->packageName . '/purchases',
90 'api/v1/apps/' . $game->packageName . '.json',
93 $latestRelease = $game->latestRelease;
95 'api/v1/apps/' . $latestRelease->uuid . '.json',
100 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
101 buildAppDownload($game, $latestRelease)
109 foreach ($developers as $developer) {
111 //index.htm does not need a rewrite rule
112 'api/v1/developers/' . $developer['info']->uuid
113 . '/products/index.htm',
114 buildDeveloperProducts($developer['products'], $developer['info'])
117 //index.htm does not need a rewrite rule
118 'api/v1/developers/' . $developer['info']->uuid
120 buildDeveloperCurrentGamer()
124 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
125 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
129 'api/v1/discover-data/tutorials.json',
130 buildMakeCategory('Tutorials', filterByGenre($games, 'Tutorials'))
135 function buildDiscover(array $games)
138 'title' => 'DISCOVER',
144 $data, 'Last Updated',
145 filterLastUpdated($games, 10)
149 filterBestRated($games, 10)
152 $data, "cweiske's picks",
153 filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
162 addDiscoverRow($data, '# of players', $players);
163 foreach ($players as $num => $title) {
165 'api/v1/discover-data/' . categoryPath($title) . '.json',
166 buildDiscoverCategory($title, filterByPlayers($games, $num))
170 $ages = getAllAges($games);
172 addDiscoverRow($data, 'Content rating', $ages);
173 foreach ($ages as $num => $title) {
175 'api/v1/discover-data/' . categoryPath($title) . '.json',
176 buildDiscoverCategory($title, filterByAge($games, $title))
180 $genres = removeMakeGenres(getAllGenres($games));
182 addChunkedDiscoverRows($data, $genres, 'Genres');
184 foreach ($genres as $genre) {
186 'api/v1/discover-data/' . categoryPath($genre) . '.json',
187 buildDiscoverCategory($genre, filterByGenre($games, $genre))
191 $abc = array_merge(range('A', 'Z'), ['Other']);
192 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
193 foreach ($abc as $letter) {
195 'api/v1/discover-data/' . categoryPath($letter) . '.json',
196 buildDiscoverCategory($letter, filterByLetter($games, $letter))
204 * A genre category page
206 function buildDiscoverCategory($name, $games)
214 $data, 'Last Updated',
215 filterLastUpdated($games, 10)
219 filterBestRated($games, 10)
224 function ($gameA, $gameB) {
225 return strcasecmp($gameA->title, $gameB->title);
228 $chunks = array_chunk($games, 4);
229 foreach ($chunks as $chunkGames) {
230 addDiscoverRow($data, '', $chunkGames);
236 function buildMakeCategory($name, $games)
246 function ($gameA, $gameB) {
247 return strcasecmp($gameA->title, $gameB->title);
250 addDiscoverRow($data, '', $games);
255 function buildDiscoverHome(array $games)
257 //we do not want anything here for now
262 'title' => 'FEATURED',
263 'showPrice' => false,
274 * Build api/v1/apps/$packageName
276 function buildApps($game)
278 $latestRelease = $game->latestRelease;
281 $gamePromoted = getPromotedProduct($game);
283 $product = buildProduct($gamePromoted);
286 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
289 'uuid' => $latestRelease->uuid,
290 'title' => $game->title,
291 'overview' => $game->overview,
292 'description' => $game->description,
293 'gamerNumbers' => $game->players,
294 'genres' => $game->genres,
296 'website' => $game->website,
297 'contentRating' => $game->contentRating,
298 'premium' => $game->premium,
299 'firstPublishedAt' => $game->firstPublishedAt,
301 'likeCount' => $game->rating->likeCount,
302 'ratingAverage' => $game->rating->average,
303 'ratingCount' => $game->rating->count,
305 'versionNumber' => $latestRelease->name,
306 'latestVersion' => $latestRelease->uuid,
307 'md5sum' => $latestRelease->md5sum,
308 'apkFileSize' => $latestRelease->size,
309 'publishedAt' => $latestRelease->date,
310 'publicSize' => $latestRelease->publicSize,
311 'nativeSize' => $latestRelease->nativeSize,
313 'mainImageFullUrl' => $game->discover,
314 'videoUrl' => getFirstVideoUrl($game->media),
315 'filepickerScreenshots' => getAllImageUrls($game->media),
316 'mobileAppIcon' => null,
318 'developer' => $game->developer->name,
319 'supportEmailAddress' => $game->developer->supportEmail,
320 'supportPhone' => $game->developer->supportPhone,
321 'founder' => $game->developer->founder,
323 'promotedProduct' => $product,
328 function buildAppDownload($game, $release)
332 'fileSize' => $release->size,
333 'version' => $release->uuid,
334 'contentRating' => $game->contentRating,
335 'downloadLink' => $release->url,
340 function buildProduct($product)
342 if ($product === null) {
346 'type' => 'entitlement',
347 'identifier' => $product->identifier,
348 'name' => $product->name,
349 'description' => $product->description ?? '',
350 'localPrice' => $product->localPrice,
351 'originalPrice' => $product->originalPrice,
353 'currency' => $product->currency,
358 * Build /app/v1/details?app=org.example.game
360 function buildDetails($game)
362 $latestRelease = $game->latestRelease;
365 if ($game->discover) {
369 'thumbnail' => $game->discover,
370 'full' => $game->discover,
374 foreach ($game->media as $medium) {
375 if ($medium->type == 'image') {
379 'thumbnail' => $medium->thumb,
380 'full' => $medium->url,
386 'url' => $medium->url,
392 if (isset($game->links->unlocked)) {
394 'text' => 'Show unlocked',
395 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
401 $gamePromoted = getPromotedProduct($game);
403 $product = buildProduct($gamePromoted);
406 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
409 'title' => $game->title,
410 'description' => $game->description,
411 'gamerNumbers' => $game->players,
412 'genres' => $game->genres,
414 'suggestedAge' => $game->contentRating,
415 'premium' => $game->premium,
416 'inAppPurchases' => $game->inAppPurchases,
417 'firstPublishedAt' => strtotime($game->firstPublishedAt),
421 'count' => $game->rating->count,
422 'average' => $game->rating->average,
426 'fileSize' => $latestRelease->size,
427 'nativeSize' => $latestRelease->nativeSize,
428 'publicSize' => $latestRelease->publicSize,
429 'md5sum' => $latestRelease->md5sum,
430 'filename' => 'FIXME',
432 'package' => $game->packageName,
433 'versionCode' => $latestRelease->versionCode,
434 'state' => 'complete',
438 'number' => $latestRelease->name,
439 'publishedAt' => strtotime($latestRelease->date),
440 'uuid' => $latestRelease->uuid,
444 'name' => $game->developer->name,
445 'founder' => $game->developer->founder,
449 'key:rating.average',
450 'key:developer.name',
452 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
455 'tileImage' => $game->discover,
456 'mediaTiles' => $mediaTiles,
457 'mobileAppIcon' => null,
462 'promotedProduct' => $product,
463 'buttons' => $buttons,
467 function buildDeveloperCurrentGamer()
471 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
472 'username' => 'stouyapi',
478 * For /api/v1/developers/xxx/products/?only=yyy
480 function buildDeveloperProductOnly($product, $developer)
483 'developerName' => $developer->name,
484 'currency' => $product->currency,
486 buildProduct($product),
492 * For /api/v1/developers/xxx/products/
494 function buildDeveloperProducts($products, $developer)
497 foreach ($products as $product) {
498 $jsonProducts[] = buildProduct($product);
501 'developerName' => $developer->name,
502 'currency' => $products[0]->currency ?? 'EUR',
503 'products' => $jsonProducts,
507 function buildPurchases($game)
512 $promotedProduct = getPromotedProduct($game);
513 if ($promotedProduct) {
514 $purchasesData['purchases'][] = [
515 'purchaseDate' => time() * 1000,
516 'generateDate' => time() * 1000,
517 'identifier' => $promotedProduct->identifier,
518 'gamer' => 'stouyapi',
519 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
520 'priceInCents' => $promotedProduct->originalPrice * 100,
521 'localPrice' => $promotedProduct->localPrice,
522 'currency' => $promotedProduct->currency,
526 $encryptedOnce = dummyEncrypt($purchasesData);
527 $encryptedTwice = dummyEncrypt($encryptedOnce);
528 return $encryptedTwice;
531 function dummyEncrypt($data)
534 'key' => base64_encode('0123456789abcdef') . "\n",
535 'iv' => 't3jir1LHpICunvhlM76edQ==' . "\n",//random bytes
536 'blob' => base64_encode(
537 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
542 function addChunkedDiscoverRows(&$data, $games, $title)
544 $chunks = array_chunk($games, 4);
546 foreach ($chunks as $chunk) {
548 $data, $first ? $title : '',
555 function addDiscoverRow(&$data, $title, $games)
563 foreach ($games as $game) {
564 if (is_string($game)) {
566 $tilePos = count($data['tiles']);
567 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
571 if (isset($game->links->original)) {
572 //do not link unlocked games.
573 // people an access them via the original games
576 $tilePos = findTile($data['tiles'], $game->packageName);
577 if ($tilePos === null) {
578 $tilePos = count($data['tiles']);
579 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
582 $row['tiles'][] = $tilePos;
584 $data['rows'][] = $row;
587 function findTile($tiles, $packageName)
589 foreach ($tiles as $pos => $tile) {
590 if ($tile['package'] == $packageName) {
597 function buildDiscoverCategoryTile($title)
600 'url' => 'ouya://launcher/discover/' . categoryPath($title),
607 function buildDiscoverGameTile($game)
609 $latestRelease = $game->latestRelease;
611 'gamerNumbers' => $game->players,
612 'genres' => $game->genres,
613 'url' => 'ouya://launcher/details?app=' . $game->packageName,
616 'md5sum' => $latestRelease->md5sum,
618 'versionNumber' => $latestRelease->name,
619 'uuid' => $latestRelease->uuid,
621 'inAppPurchases' => $game->inAppPurchases,
622 'promotedProduct' => null,
623 'premium' => $game->premium,
625 'package' => $game->packageName,
626 'updated_at' => strtotime($latestRelease->date),
627 'updatedAt' => $latestRelease->date,
628 'title' => $game->title,
629 'image' => $game->discover,
630 'contentRating' => $game->contentRating,
632 'count' => $game->rating->count,
633 'average' => $game->rating->average,
635 'promotedProduct' => buildProduct(getPromotedProduct($game)),
639 function categoryPath($title)
641 return str_replace(['/', '\\', ' ', '+'], '_', $title);
644 function getAllAges($games)
647 foreach ($games as $game) {
648 $ages[] = $game->contentRating;
650 return array_unique($ages);
653 function getAllGenres($games)
656 foreach ($games as $game) {
657 $genres = array_merge($genres, $game->genres);
659 return array_unique($genres);
662 function addMissingGameProperties($game)
664 if (!isset($game->overview)) {
665 $game->overview = null;
667 if (!isset($game->description)) {
668 $game->description = '';
670 if (!isset($game->players)) {
671 $game->players = [1];
673 if (!isset($game->genres)) {
674 $game->genres = ['Unsorted'];
676 if (!isset($game->website)) {
677 $game->website = null;
679 if (!isset($game->contentRating)) {
680 $game->contentRating = 'Everyone';
682 if (!isset($game->premium)) {
683 $game->premium = false;
685 if (!isset($game->firstPublishedAt)) {
686 $game->firstPublishedAt = gmdate('c');
689 if (!isset($game->rating)) {
690 $game->rating = new stdClass();
692 if (!isset($game->rating->likeCount)) {
693 $game->rating->likeCount = 0;
695 if (!isset($game->rating->average)) {
696 $game->rating->average = 0;
698 if (!isset($game->rating->count)) {
699 $game->rating->count = 0;
702 $game->latestRelease = null;
703 $latestReleaseTimestamp = 0;
704 foreach ($game->releases as $release) {
705 if (!isset($release->publicSize)) {
706 $release->publicSize = 0;
708 if (!isset($release->nativeSize)) {
709 $release->nativeSize = 0;
712 $releaseTimestamp = strtotime($release->date);
713 if ($releaseTimestamp > $latestReleaseTimestamp) {
714 $game->latestRelease = $release;
715 $latestReleaseTimestamp = $releaseTimestamp;
718 if ($game->latestRelease === null) {
719 error('No latest release for ' . $game->packageName);
722 if (!isset($game->media)) {
726 if (!isset($game->developer->uuid)) {
727 $game->developer->uuid = null;
729 if (!isset($game->developer->name)) {
730 $game->developer->name = 'unknown';
732 if (!isset($game->developer->supportEmail)) {
733 $game->developer->supportEmail = null;
735 if (!isset($game->developer->supportPhone)) {
736 $game->developer->supportPhone = null;
738 if (!isset($game->developer->founder)) {
739 $game->developer->founder = false;
743 function getFirstVideoUrl($media)
745 foreach ($media as $medium) {
746 if ($medium->type == 'video') {
753 function getAllImageUrls($media)
756 foreach ($media as $medium) {
757 if ($medium->type == 'image') {
758 $imageUrls[] = $medium->url;
764 function getPromotedProduct($game)
766 if (!isset($game->products) || !count($game->products)) {
769 foreach ($game->products as $gameProd) {
770 if ($gameProd->promoted) {
777 function removeMakeGenres($genres)
780 foreach ($genres as $genre) {
781 if ($genre != 'Tutorials' && $genre != 'Builds') {
782 $filtered[] = $genre;
788 function writeJson($path, $data)
791 $fullPath = $wwwDir . $path;
792 $dir = dirname($fullPath);
794 mkdir($dir, 0777, true);
798 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
804 fwrite(STDERR, $msg . "\n");