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));
128 function buildDiscover(array $games)
131 'title' => 'DISCOVER',
137 $data, 'Last Updated',
138 filterLastUpdated($games, 10)
142 filterBestRated($games, 10)
145 $data, "cweiske's picks",
146 filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
155 addDiscoverRow($data, '# of players', $players);
156 foreach ($players as $num => $title) {
158 'api/v1/discover-data/' . categoryPath($title) . '.json',
159 buildDiscoverCategory($title, filterByPlayers($games, $num))
163 $ages = getAllAges($games);
165 addDiscoverRow($data, 'Content rating', $ages);
166 foreach ($ages as $num => $title) {
168 'api/v1/discover-data/' . categoryPath($title) . '.json',
169 buildDiscoverCategory($title, filterByAge($games, $title))
173 $genres = getAllGenres($games);
175 addChunkedDiscoverRows($data, $genres, 'Genres');
177 foreach ($genres as $genre) {
179 'api/v1/discover-data/' . categoryPath($genre) . '.json',
180 buildDiscoverCategory($genre, filterByGenre($games, $genre))
184 $abc = array_merge(range('A', 'Z'), ['Other']);
185 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
186 foreach ($abc as $letter) {
188 'api/v1/discover-data/' . categoryPath($letter) . '.json',
189 buildDiscoverCategory($letter, filterByLetter($games, $letter))
197 * A genre category page
199 function buildDiscoverCategory($name, $games)
207 $data, 'Last Updated',
208 filterLastUpdated($games, 10)
212 filterBestRated($games, 10)
217 function ($gameA, $gameB) {
218 return strcasecmp($gameA->title, $gameB->title);
221 $chunks = array_chunk($games, 4);
222 foreach ($chunks as $chunkGames) {
223 addDiscoverRow($data, '', $chunkGames);
229 function buildDiscoverHome(array $games)
231 //we do not want anything here for now
236 'title' => 'FEATURED',
237 'showPrice' => false,
248 * Build api/v1/apps/$packageName
250 function buildApps($game)
252 $latestRelease = $game->latestRelease;
255 $gamePromoted = getPromotedProduct($game);
257 $product = buildProduct($gamePromoted);
260 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
263 'uuid' => $latestRelease->uuid,
264 'title' => $game->title,
265 'overview' => $game->overview,
266 'description' => $game->description,
267 'gamerNumbers' => $game->players,
268 'genres' => $game->genres,
270 'website' => $game->website,
271 'contentRating' => $game->contentRating,
272 'premium' => $game->premium,
273 'firstPublishedAt' => $game->firstPublishedAt,
275 'likeCount' => $game->rating->likeCount,
276 'ratingAverage' => $game->rating->average,
277 'ratingCount' => $game->rating->count,
279 'versionNumber' => $latestRelease->name,
280 'latestVersion' => $latestRelease->uuid,
281 'md5sum' => $latestRelease->md5sum,
282 'apkFileSize' => $latestRelease->size,
283 'publishedAt' => $latestRelease->date,
284 'publicSize' => $latestRelease->publicSize,
285 'nativeSize' => $latestRelease->nativeSize,
287 'mainImageFullUrl' => $game->discover,
288 'videoUrl' => getFirstVideoUrl($game->media),
289 'filepickerScreenshots' => getAllImageUrls($game->media),
290 'mobileAppIcon' => null,
292 'developer' => $game->developer->name,
293 'supportEmailAddress' => $game->developer->supportEmail,
294 'supportPhone' => $game->developer->supportPhone,
295 'founder' => $game->developer->founder,
297 'promotedProduct' => $product,
302 function buildAppDownload($game, $release)
306 'fileSize' => $release->size,
307 'version' => $release->uuid,
308 'contentRating' => $game->contentRating,
309 'downloadLink' => $release->url,
314 function buildProduct($product)
316 if ($product === null) {
320 'type' => 'entitlement',
321 'identifier' => $product->identifier,
322 'name' => $product->name,
323 'description' => $product->description ?? '',
324 'localPrice' => $product->localPrice,
325 'originalPrice' => $product->originalPrice,
327 'currency' => $product->currency,
332 * Build /app/v1/details?app=org.example.game
334 function buildDetails($game)
336 $latestRelease = $game->latestRelease;
339 if ($game->discover) {
343 'thumbnail' => $game->discover,
344 'full' => $game->discover,
348 foreach ($game->media as $medium) {
349 if ($medium->type == 'image') {
353 'thumbnail' => $medium->thumb,
354 'full' => $medium->url,
360 'url' => $medium->url,
366 if (isset($game->links->unlocked)) {
368 'text' => 'Show unlocked',
369 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
375 $gamePromoted = getPromotedProduct($game);
377 $product = buildProduct($gamePromoted);
380 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
383 'title' => $game->title,
384 'description' => $game->description,
385 'gamerNumbers' => $game->players,
386 'genres' => $game->genres,
388 'suggestedAge' => $game->contentRating,
389 'premium' => $game->premium,
390 'inAppPurchases' => $game->inAppPurchases,
391 'firstPublishedAt' => strtotime($game->firstPublishedAt),
395 'count' => $game->rating->count,
396 'average' => $game->rating->average,
400 'fileSize' => $latestRelease->size,
401 'nativeSize' => $latestRelease->nativeSize,
402 'publicSize' => $latestRelease->publicSize,
403 'md5sum' => $latestRelease->md5sum,
404 'filename' => 'FIXME',
406 'package' => $game->packageName,
407 'versionCode' => $latestRelease->versionCode,
408 'state' => 'complete',
412 'number' => $latestRelease->name,
413 'publishedAt' => strtotime($latestRelease->date),
414 'uuid' => $latestRelease->uuid,
418 'name' => $game->developer->name,
419 'founder' => $game->developer->founder,
423 'key:rating.average',
424 'key:developer.name',
426 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
429 'tileImage' => $game->discover,
430 'mediaTiles' => $mediaTiles,
431 'mobileAppIcon' => null,
436 'promotedProduct' => $product,
437 'buttons' => $buttons,
441 function buildDeveloperCurrentGamer()
445 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',
446 'username' => 'stouyapi',
452 * For /api/v1/developers/xxx/products/?only=yyy
454 function buildDeveloperProductOnly($product, $developer)
457 'developerName' => $developer->name,
458 'currency' => $product->currency,
460 buildProduct($product),
466 * For /api/v1/developers/xxx/products/
468 function buildDeveloperProducts($products, $developer)
471 foreach ($products as $product) {
472 $jsonProducts[] = buildProduct($product);
475 'developerName' => $developer->name,
476 'currency' => $products[0]->currency ?? 'EUR',
477 'products' => $jsonProducts,
481 function buildPurchases($game)
486 $promotedProduct = getPromotedProduct($game);
487 if ($promotedProduct) {
488 $purchasesData['purchases'][] = [
489 'purchaseDate' => time() * 1000,
490 'generateDate' => time() * 1000,
491 'identifier' => $promotedProduct->identifier,
492 'gamer' => 'stouyapi',
493 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
494 'priceInCents' => $promotedProduct->originalPrice * 100,
495 'localPrice' => $promotedProduct->localPrice,
496 'currency' => $promotedProduct->currency,
500 $encryptedOnce = dummyEncrypt($purchasesData);
501 $encryptedTwice = dummyEncrypt($encryptedOnce);
502 return $encryptedTwice;
505 function dummyEncrypt($data)
508 'key' => base64_encode('0123456789abcdef') . "\n",
509 'iv' => 't3jir1LHpICunvhlM76edQ==' . "\n",//random bytes
510 'blob' => base64_encode(
511 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
516 function addChunkedDiscoverRows(&$data, $games, $title)
518 $chunks = array_chunk($games, 4);
520 foreach ($chunks as $chunk) {
522 $data, $first ? $title : '',
529 function addDiscoverRow(&$data, $title, $games)
537 foreach ($games as $game) {
538 if (is_string($game)) {
540 $tilePos = count($data['tiles']);
541 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
545 if (isset($game->links->original)) {
546 //do not link unlocked games.
547 // people an access them via the original games
550 $tilePos = findTile($data['tiles'], $game->packageName);
551 if ($tilePos === null) {
552 $tilePos = count($data['tiles']);
553 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
556 $row['tiles'][] = $tilePos;
558 $data['rows'][] = $row;
561 function findTile($tiles, $packageName)
563 foreach ($tiles as $pos => $tile) {
564 if ($tile['package'] == $packageName) {
571 function buildDiscoverCategoryTile($title)
574 'url' => 'ouya://launcher/discover/' . categoryPath($title),
581 function buildDiscoverGameTile($game)
583 $latestRelease = $game->latestRelease;
585 'gamerNumbers' => $game->players,
586 'genres' => $game->genres,
587 'url' => 'ouya://launcher/details?app=' . $game->packageName,
590 'md5sum' => $latestRelease->md5sum,
592 'versionNumber' => $latestRelease->name,
593 'uuid' => $latestRelease->uuid,
595 'inAppPurchases' => $game->inAppPurchases,
596 'promotedProduct' => null,
597 'premium' => $game->premium,
599 'package' => $game->packageName,
600 'updated_at' => strtotime($latestRelease->date),
601 'updatedAt' => $latestRelease->date,
602 'title' => $game->title,
603 'image' => $game->discover,
604 'contentRating' => $game->contentRating,
606 'count' => $game->rating->count,
607 'average' => $game->rating->average,
609 'promotedProduct' => buildProduct(getPromotedProduct($game)),
613 function categoryPath($title)
615 return str_replace(['/', '\\', ' ', '+'], '_', $title);
618 function getAllAges($games)
621 foreach ($games as $game) {
622 $ages[] = $game->contentRating;
624 return array_unique($ages);
627 function getAllGenres($games)
630 foreach ($games as $game) {
631 $genres = array_merge($genres, $game->genres);
633 return array_unique($genres);
636 function addMissingGameProperties($game)
638 if (!isset($game->overview)) {
639 $game->overview = null;
641 if (!isset($game->description)) {
642 $game->description = '';
644 if (!isset($game->players)) {
645 $game->players = [1];
647 if (!isset($game->genres)) {
648 $game->genres = ['Unsorted'];
650 if (!isset($game->website)) {
651 $game->website = null;
653 if (!isset($game->contentRating)) {
654 $game->contentRating = 'Everyone';
656 if (!isset($game->premium)) {
657 $game->premium = false;
659 if (!isset($game->firstPublishedAt)) {
660 $game->firstPublishedAt = gmdate('c');
663 if (!isset($game->rating)) {
664 $game->rating = new stdClass();
666 if (!isset($game->rating->likeCount)) {
667 $game->rating->likeCount = 0;
669 if (!isset($game->rating->average)) {
670 $game->rating->average = 0;
672 if (!isset($game->rating->count)) {
673 $game->rating->count = 0;
676 $game->latestRelease = null;
677 $latestReleaseTimestamp = 0;
678 foreach ($game->releases as $release) {
679 if (!isset($release->publicSize)) {
680 $release->publicSize = 0;
682 if (!isset($release->nativeSize)) {
683 $release->nativeSize = 0;
686 $releaseTimestamp = strtotime($release->date);
687 if ($releaseTimestamp > $latestReleaseTimestamp) {
688 $game->latestRelease = $release;
689 $latestReleaseTimestamp = $releaseTimestamp;
692 if ($game->latestRelease === null) {
693 error('No latest release for ' . $game->packageName);
696 if (!isset($game->media)) {
700 if (!isset($game->developer->uuid)) {
701 $game->developer->uuid = null;
703 if (!isset($game->developer->name)) {
704 $game->developer->name = 'unknown';
706 if (!isset($game->developer->supportEmail)) {
707 $game->developer->supportEmail = null;
709 if (!isset($game->developer->supportPhone)) {
710 $game->developer->supportPhone = null;
712 if (!isset($game->developer->founder)) {
713 $game->developer->founder = false;
717 function getFirstVideoUrl($media)
719 foreach ($media as $medium) {
720 if ($medium->type == 'video') {
727 function getAllImageUrls($media)
730 foreach ($media as $medium) {
731 if ($medium->type == 'image') {
732 $imageUrls[] = $medium->url;
738 function getPromotedProduct($game)
740 if (!isset($game->products) || !count($game->products)) {
743 foreach ($game->products as $gameProd) {
744 if ($gameProd->promoted) {
751 function writeJson($path, $data)
754 $fullPath = $wwwDir . $path;
755 $dir = dirname($fullPath);
757 mkdir($dir, 0777, true);
761 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
767 fwrite(STDERR, $msg . "\n");