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'])
118 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
119 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
122 function buildDiscover(array $games)
125 'title' => 'DISCOVER',
131 $data, 'Last Updated',
132 filterLastUpdated($games, 10)
136 filterBestRated($games, 10)
139 $data, "cweiske's picks",
140 filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
149 addDiscoverRow($data, '# of players', $players);
150 foreach ($players as $num => $title) {
152 'api/v1/discover-data/' . categoryPath($title) . '.json',
153 buildDiscoverCategory($title, filterByPlayers($games, $num))
157 $ages = getAllAges($games);
159 addDiscoverRow($data, 'Content rating', $ages);
160 foreach ($ages as $num => $title) {
162 'api/v1/discover-data/' . categoryPath($title) . '.json',
163 buildDiscoverCategory($title, filterByAge($games, $title))
167 $genres = getAllGenres($games);
169 addChunkedDiscoverRows($data, $genres, 'Genres');
171 foreach ($genres as $genre) {
173 'api/v1/discover-data/' . categoryPath($genre) . '.json',
174 buildDiscoverCategory($genre, filterByGenre($games, $genre))
178 $abc = array_merge(range('A', 'Z'), ['Other']);
179 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
180 foreach ($abc as $letter) {
182 'api/v1/discover-data/' . categoryPath($letter) . '.json',
183 buildDiscoverCategory($letter, filterByLetter($games, $letter))
191 * A genre category page
193 function buildDiscoverCategory($name, $games)
201 $data, 'Last Updated',
202 filterLastUpdated($games, 10)
206 filterBestRated($games, 10)
211 function ($gameA, $gameB) {
212 return strcmp($gameA->title, $gameB->title);
215 $chunks = array_chunk($games, 4);
216 foreach ($chunks as $chunkGames) {
217 addDiscoverRow($data, '', $chunkGames);
223 function buildDiscoverHome(array $games)
225 //we do not want anything here for now
230 'title' => 'FEATURED',
231 'showPrice' => false,
242 * Build api/v1/apps/$packageName
244 function buildApps($game)
246 $latestRelease = $game->latestRelease;
249 $gamePromoted = getPromotedProduct($game);
251 $product = buildProduct($gamePromoted);
254 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
257 'uuid' => $latestRelease->uuid,
258 'title' => $game->title,
259 'overview' => $game->overview,
260 'description' => $game->description,
261 'gamerNumbers' => $game->players,
262 'genres' => $game->genres,
264 'website' => $game->website,
265 'contentRating' => $game->contentRating,
266 'premium' => $game->premium,
267 'firstPublishedAt' => $game->firstPublishedAt,
269 'likeCount' => $game->rating->likeCount,
270 'ratingAverage' => $game->rating->average,
271 'ratingCount' => $game->rating->count,
273 'versionNumber' => $latestRelease->name,
274 'latestVersion' => $latestRelease->uuid,
275 'md5sum' => $latestRelease->md5sum,
276 'apkFileSize' => $latestRelease->size,
277 'publishedAt' => $latestRelease->date,
278 'publicSize' => $latestRelease->publicSize,
279 'nativeSize' => $latestRelease->nativeSize,
281 'mainImageFullUrl' => $game->discover,
282 'videoUrl' => getFirstVideoUrl($game->media),
283 'filepickerScreenshots' => getAllImageUrls($game->media),
284 'mobileAppIcon' => null,
286 'developer' => $game->developer->name,
287 'supportEmailAddress' => $game->developer->supportEmail,
288 'supportPhone' => $game->developer->supportPhone,
289 'founder' => $game->developer->founder,
291 'promotedProduct' => $product,
296 function buildAppDownload($game, $release)
300 'fileSize' => $release->size,
301 'version' => $release->uuid,
302 'contentRating' => $game->contentRating,
303 'downloadLink' => $release->url,
308 function buildProduct($product)
310 if ($product === null) {
314 'type' => 'entitlement',
315 'identifier' => $product->identifier,
316 'name' => $product->name,
317 'description' => $product->description ?? '',
318 'localPrice' => $product->localPrice,
319 'originalPrice' => $product->originalPrice,
321 'currency' => $product->currency,
326 * Build /app/v1/details?app=org.example.game
328 function buildDetails($game)
330 $latestRelease = $game->latestRelease;
333 if ($game->discover) {
337 'thumbnail' => $game->discover,
338 'full' => $game->discover,
342 foreach ($game->media as $medium) {
343 if ($medium->type == 'image') {
347 'thumbnail' => $medium->thumb,
348 'full' => $medium->url,
354 'url' => $medium->url,
360 if (isset($game->links->unlocked)) {
362 'text' => 'Show unlocked',
363 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
369 $gamePromoted = getPromotedProduct($game);
371 $product = buildProduct($gamePromoted);
374 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
377 'title' => $game->title,
378 'description' => $game->description,
379 'gamerNumbers' => $game->players,
380 'genres' => $game->genres,
382 'suggestedAge' => $game->contentRating,
383 'premium' => $game->premium,
384 'inAppPurchases' => $game->inAppPurchases,
385 'firstPublishedAt' => strtotime($game->firstPublishedAt),
389 'count' => $game->rating->count,
390 'average' => $game->rating->average,
394 'fileSize' => $latestRelease->size,
395 'nativeSize' => $latestRelease->nativeSize,
396 'publicSize' => $latestRelease->publicSize,
397 'md5sum' => $latestRelease->md5sum,
398 'filename' => 'FIXME',
400 'package' => $game->packageName,
401 'versionCode' => $latestRelease->versionCode,
402 'state' => 'complete',
406 'number' => $latestRelease->name,
407 'publishedAt' => strtotime($latestRelease->date),
408 'uuid' => $latestRelease->uuid,
412 'name' => $game->developer->name,
413 'founder' => $game->developer->founder,
417 'key:rating.average',
418 'key:developer.name',
420 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
423 'tileImage' => $game->discover,
424 'mediaTiles' => $mediaTiles,
425 'mobileAppIcon' => null,
430 'promotedProduct' => $product,
431 'buttons' => $buttons,
436 * For /api/v1/developers/xxx/products/?only=yyy
438 function buildDeveloperProductOnly($product, $developer)
441 'developerName' => $developer->name,
442 'currency' => $product->currency,
444 buildProduct($product),
450 * For /api/v1/developers/xxx/products/
452 function buildDeveloperProducts($products, $developer)
455 foreach ($products as $product) {
456 $jsonProducts[] = buildProduct($product);
459 'developerName' => $developer->name,
460 'currency' => $products[0]->currency ?? 'EUR',
461 'products' => $jsonProducts,
465 function buildPurchases($game)
470 $promotedProduct = getPromotedProduct($game);
471 if ($promotedProduct) {
472 $purchasesData['purchases'][] = [
473 'purchaseDate' => time() * 1000,
474 'generateDate' => time() * 1000,
475 'identifier' => $promotedProduct->identifier,
476 'gamer' => 'stouyapi',
477 'uuid' => '00702342-0000-1111-2222-c3e1500cafe2',//gamer uuid
478 'priceInCents' => $promotedProduct->originalPrice * 100,
479 'localPrice' => $promotedProduct->localPrice,
480 'currency' => $promotedProduct->currency,
484 $encryptedOnce = dummyEncrypt($purchasesData);
485 $encryptedTwice = dummyEncrypt($encryptedOnce);
486 return $encryptedTwice;
489 function dummyEncrypt($data)
492 'key' => base64_encode('0123456789abcdef') . "\n",
493 'iv' => 't3jir1LHpICunvhlM76edQ==' . "\n",//random bytes
494 'blob' => base64_encode(
495 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
500 function addChunkedDiscoverRows(&$data, $games, $title)
502 $chunks = array_chunk($games, 4);
504 foreach ($chunks as $chunk) {
506 $data, $first ? $title : '',
513 function addDiscoverRow(&$data, $title, $games)
521 foreach ($games as $game) {
522 if (is_string($game)) {
524 $tilePos = count($data['tiles']);
525 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
529 if (isset($game->links->original)) {
530 //do not link unlocked games.
531 // people an access them via the original games
534 $tilePos = findTile($data['tiles'], $game->packageName);
535 if ($tilePos === null) {
536 $tilePos = count($data['tiles']);
537 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
540 $row['tiles'][] = $tilePos;
542 $data['rows'][] = $row;
545 function findTile($tiles, $packageName)
547 foreach ($tiles as $pos => $tile) {
548 if ($tile['package'] == $packageName) {
555 function buildDiscoverCategoryTile($title)
558 'url' => 'ouya://launcher/discover/' . categoryPath($title),
565 function buildDiscoverGameTile($game)
567 $latestRelease = $game->latestRelease;
569 'gamerNumbers' => $game->players,
570 'genres' => $game->genres,
571 'url' => 'ouya://launcher/details?app=' . $game->packageName,
574 'md5sum' => $latestRelease->md5sum,
576 'versionNumber' => $latestRelease->name,
577 'uuid' => $latestRelease->uuid,
579 'inAppPurchases' => $game->inAppPurchases,
580 'promotedProduct' => null,
581 'premium' => $game->premium,
583 'package' => $game->packageName,
584 'updated_at' => strtotime($latestRelease->date),
585 'updatedAt' => $latestRelease->date,
586 'title' => $game->title,
587 'image' => $game->discover,
588 'contentRating' => $game->contentRating,
590 'count' => $game->rating->count,
591 'average' => $game->rating->average,
593 'promotedProduct' => buildProduct(getPromotedProduct($game)),
597 function categoryPath($title)
599 return str_replace(['/', '\\', ' ', '+'], '_', $title);
602 function getAllAges($games)
605 foreach ($games as $game) {
606 $ages[] = $game->contentRating;
608 return array_unique($ages);
611 function getAllGenres($games)
614 foreach ($games as $game) {
615 $genres = array_merge($genres, $game->genres);
617 return array_unique($genres);
620 function addMissingGameProperties($game)
622 if (!isset($game->overview)) {
623 $game->overview = null;
625 if (!isset($game->description)) {
626 $game->description = '';
628 if (!isset($game->players)) {
629 $game->players = [1];
631 if (!isset($game->genres)) {
632 $game->genres = ['Unsorted'];
634 if (!isset($game->website)) {
635 $game->website = null;
637 if (!isset($game->contentRating)) {
638 $game->contentRating = 'Everyone';
640 if (!isset($game->premium)) {
641 $game->premium = false;
643 if (!isset($game->firstPublishedAt)) {
644 $game->firstPublishedAt = gmdate('c');
647 if (!isset($game->rating)) {
648 $game->rating = new stdClass();
650 if (!isset($game->rating->likeCount)) {
651 $game->rating->likeCount = 0;
653 if (!isset($game->rating->average)) {
654 $game->rating->average = 0;
656 if (!isset($game->rating->count)) {
657 $game->rating->count = 0;
660 $game->latestRelease = null;
661 $latestReleaseTimestamp = 0;
662 foreach ($game->releases as $release) {
663 if (!isset($release->publicSize)) {
664 $release->publicSize = 0;
666 if (!isset($release->nativeSize)) {
667 $release->nativeSize = 0;
670 $releaseTimestamp = strtotime($release->date);
671 if ($releaseTimestamp > $latestReleaseTimestamp) {
672 $game->latestRelease = $release;
673 $latestReleaseTimestamp = $releaseTimestamp;
676 if ($game->latestRelease === null) {
677 error('No latest release for ' . $game->packageName);
680 if (!isset($game->media)) {
684 if (!isset($game->developer->uuid)) {
685 $game->developer->uuid = null;
687 if (!isset($game->developer->name)) {
688 $game->developer->name = 'unknown';
690 if (!isset($game->developer->supportEmail)) {
691 $game->developer->supportEmail = null;
693 if (!isset($game->developer->supportPhone)) {
694 $game->developer->supportPhone = null;
696 if (!isset($game->developer->founder)) {
697 $game->developer->founder = false;
701 function getFirstVideoUrl($media)
703 foreach ($media as $medium) {
704 if ($medium->type == 'video') {
711 function getAllImageUrls($media)
714 foreach ($media as $medium) {
715 if ($medium->type == 'image') {
716 $imageUrls[] = $medium->url;
722 function getPromotedProduct($game)
724 if (!isset($game->products) || !count($game->products)) {
727 foreach ($game->products as $gameProd) {
728 if ($gameProd->promoted) {
735 function writeJson($path, $data)
738 $fullPath = $wwwDir . $path;
739 $dir = dirname($fullPath);
741 mkdir($dir, 0777, true);
745 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
751 fwrite(STDERR, $msg . "\n");