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'));
44 foreach ($gameFiles as $gameFile) {
45 $game = json_decode(file_get_contents($gameFile));
47 error('JSON invalid at ' . $gameFile);
49 addMissingGameProperties($game);
50 $games[$game->packageName] = $game;
53 'api/v1/details-data/' . $game->packageName . '.json',
56 /* this crashes babylonian twins
58 'api/v1/games/' . $game->packageName . '/purchases',
64 'api/v1/apps/' . $game->packageName . '.json',
67 $latestRelease = $game->latestRelease;
69 'api/v1/apps/' . $latestRelease->uuid . '.json',
74 'api/v1/apps/' . $latestRelease->uuid . '-download.json',
75 buildAppDownload($game, $latestRelease)
83 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
84 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
87 function buildDiscover(array $games)
90 'title' => 'DISCOVER',
96 $data, 'Last Updated',
97 filterLastUpdated($games, 10)
101 filterBestRated($games, 10)
104 $data, "cweiske's picks",
105 filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
114 addDiscoverRow($data, '# of players', $players);
115 foreach ($players as $num => $title) {
117 'api/v1/discover-data/' . categoryPath($title) . '.json',
118 buildDiscoverCategory($title, filterByPlayers($games, $num))
122 $ages = getAllAges($games);
124 addDiscoverRow($data, 'Content rating', $ages);
125 foreach ($ages as $num => $title) {
127 'api/v1/discover-data/' . categoryPath($title) . '.json',
128 buildDiscoverCategory($title, filterByAge($games, $title))
132 $genres = getAllGenres($games);
134 addChunkedDiscoverRows($data, $genres, 'Genres');
136 foreach ($genres as $genre) {
138 'api/v1/discover-data/' . categoryPath($genre) . '.json',
139 buildDiscoverCategory($genre, filterByGenre($games, $genre))
143 $abc = array_merge(range('A', 'Z'), ['Other']);
144 addChunkedDiscoverRows($data, $abc, 'Alphabetical');
145 foreach ($abc as $letter) {
147 'api/v1/discover-data/' . categoryPath($letter) . '.json',
148 buildDiscoverCategory($letter, filterByLetter($games, $letter))
156 * A genre category page
158 function buildDiscoverCategory($name, $games)
166 $data, 'Last Updated',
167 filterLastUpdated($games, 10)
171 filterBestRated($games, 10)
176 function ($gameA, $gameB) {
177 return strcmp($gameA->title, $gameB->title);
180 $chunks = array_chunk($games, 4);
181 foreach ($chunks as $chunkGames) {
182 addDiscoverRow($data, '', $chunkGames);
188 function buildDiscoverHome(array $games)
190 //we do not want anything here for now
195 'title' => 'FEATURED',
196 'showPrice' => false,
207 * Build api/v1/apps/$packageName
209 function buildApps($game)
211 $latestRelease = $game->latestRelease;
213 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
216 'uuid' => $latestRelease->uuid,
217 'title' => $game->title,
218 'overview' => $game->overview,
219 'description' => $game->description,
220 'gamerNumbers' => $game->players,
221 'genres' => $game->genres,
223 'website' => $game->website,
224 'contentRating' => $game->contentRating,
225 'premium' => $game->premium,
226 'firstPublishedAt' => $game->firstPublishedAt,
228 'likeCount' => $game->rating->likeCount,
229 'ratingAverage' => $game->rating->average,
230 'ratingCount' => $game->rating->count,
232 'versionNumber' => $latestRelease->name,
233 'latestVersion' => $latestRelease->uuid,
234 'md5sum' => $latestRelease->md5sum,
235 'apkFileSize' => $latestRelease->size,
236 'publishedAt' => $latestRelease->date,
237 'publicSize' => $latestRelease->publicSize,
238 'nativeSize' => $latestRelease->nativeSize,
240 'mainImageFullUrl' => $game->discover,
241 'videoUrl' => getFirstVideoUrl($game->media),
242 'filepickerScreenshots' => getAllImageUrls($game->media),
243 'mobileAppIcon' => null,
245 'developer' => $game->developer->name,
246 'supportEmailAddress' => $game->developer->supportEmail,
247 'supportPhone' => $game->developer->supportPhone,
248 'founder' => $game->developer->founder,
250 'promotedProduct' => null,
255 function buildAppDownload($game, $release)
259 'fileSize' => $release->size,
260 'version' => $release->uuid,
261 'contentRating' => $game->contentRating,
262 'downloadLink' => $release->url,
268 * Build /app/v1/details?app=org.example.game
270 function buildDetails($game)
272 $latestRelease = $game->latestRelease;
275 if ($game->discover) {
279 'thumbnail' => $game->discover,
280 'full' => $game->discover,
284 foreach ($game->media as $medium) {
285 if ($medium->type == 'image') {
289 'thumbnail' => $medium->thumb,
290 'full' => $medium->url,
296 'url' => $medium->url,
302 if (isset($game->links->unlocked)) {
304 'text' => 'Show unlocked',
305 'url' => 'ouya://launcher/details?app=' . $game->links->unlocked,
310 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
313 'title' => $game->title,
314 'description' => $game->description,
315 'gamerNumbers' => $game->players,
316 'genres' => $game->genres,
318 'suggestedAge' => $game->contentRating,
319 'premium' => $game->premium,
320 'inAppPurchases' => $game->inAppPurchases,
321 'firstPublishedAt' => strtotime($game->firstPublishedAt),
325 'count' => $game->rating->count,
326 'average' => $game->rating->average,
330 'fileSize' => $latestRelease->size,
331 'nativeSize' => $latestRelease->nativeSize,
332 'publicSize' => $latestRelease->publicSize,
333 'md5sum' => $latestRelease->md5sum,
334 'filename' => 'FIXME',
336 'package' => $game->packageName,
337 'versionCode' => $latestRelease->versionCode,
338 'state' => 'complete',
342 'number' => $latestRelease->name,
343 'publishedAt' => strtotime($latestRelease->date),
344 'uuid' => $latestRelease->uuid,
348 'name' => $game->developer->name,
349 'founder' => $game->developer->founder,
353 'key:rating.average',
354 'key:developer.name',
356 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
359 'tileImage' => $game->discover,
360 'mediaTiles' => $mediaTiles,
361 'mobileAppIcon' => null,
366 'promotedProduct' => null,
367 'buttons' => $buttons,
371 function addChunkedDiscoverRows(&$data, $games, $title)
373 $chunks = array_chunk($games, 4);
375 foreach ($chunks as $chunk) {
377 $data, $first ? $title : '',
384 function addDiscoverRow(&$data, $title, $games)
388 'showPrice' => false,
392 foreach ($games as $game) {
393 if (is_string($game)) {
395 $tilePos = count($data['tiles']);
396 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
400 if (isset($game->links->original)) {
401 //do not link unlocked games.
402 // people an access them via the original games
405 $tilePos = findTile($data['tiles'], $game->packageName);
406 if ($tilePos === null) {
407 $tilePos = count($data['tiles']);
408 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
411 $row['tiles'][] = $tilePos;
413 $data['rows'][] = $row;
416 function findTile($tiles, $packageName)
418 foreach ($tiles as $pos => $tile) {
419 if ($tile['package'] == $packageName) {
426 function buildDiscoverCategoryTile($title)
429 'url' => 'ouya://launcher/discover/' . categoryPath($title),
436 function buildDiscoverGameTile($game)
438 $latestRelease = $game->latestRelease;
440 'gamerNumbers' => $game->players,
441 'genres' => $game->genres,
442 'url' => 'ouya://launcher/details?app=' . $game->packageName,
445 'md5sum' => $latestRelease->md5sum,
447 'versionNumber' => $latestRelease->name,
448 'uuid' => $latestRelease->uuid,
450 'inAppPurchases' => $game->inAppPurchases,
451 'promotedProduct' => null,
452 'premium' => $game->premium,
454 'package' => $game->packageName,
455 'updated_at' => strtotime($latestRelease->date),
456 'updatedAt' => $latestRelease->date,
457 'title' => $game->title,
458 'image' => $game->discover,
459 'contentRating' => $game->contentRating,
461 'count' => $game->rating->count,
462 'average' => $game->rating->average,
467 function categoryPath($title)
469 return str_replace(['/', '\\', ' ', '+'], '_', $title);
472 function getAllAges($games)
475 foreach ($games as $game) {
476 $ages[] = $game->contentRating;
478 return array_unique($ages);
481 function getAllGenres($games)
484 foreach ($games as $game) {
485 $genres = array_merge($genres, $game->genres);
487 return array_unique($genres);
490 function addMissingGameProperties($game)
492 if (!isset($game->overview)) {
493 $game->overview = null;
495 if (!isset($game->description)) {
496 $game->description = '';
498 if (!isset($game->players)) {
499 $game->players = [1];
501 if (!isset($game->genres)) {
502 $game->genres = ['Unsorted'];
504 if (!isset($game->website)) {
505 $game->website = null;
507 if (!isset($game->contentRating)) {
508 $game->contentRating = 'Everyone';
510 if (!isset($game->premium)) {
511 $game->premium = false;
513 if (!isset($game->firstPublishedAt)) {
514 $game->firstPublishedAt = gmdate('c');
517 if (!isset($game->rating)) {
518 $game->rating = new stdClass();
520 if (!isset($game->rating->likeCount)) {
521 $game->rating->likeCount = 0;
523 if (!isset($game->rating->average)) {
524 $game->rating->average = 0;
526 if (!isset($game->rating->count)) {
527 $game->rating->count = 0;
530 $game->latestRelease = null;
531 $latestReleaseTimestamp = 0;
532 foreach ($game->releases as $release) {
533 if (!isset($release->publicSize)) {
534 $release->publicSize = 0;
536 if (!isset($release->nativeSize)) {
537 $release->nativeSize = 0;
540 $releaseTimestamp = strtotime($release->date);
541 if ($releaseTimestamp > $latestReleaseTimestamp) {
542 $game->latestRelease = $release;
543 $latestReleaseTimestamp = $releaseTimestamp;
546 if ($game->latestRelease === null) {
547 error('No latest release for ' . $game->packageName);
550 if (!isset($game->media)) {
554 if (!isset($game->developer->uuid)) {
555 $game->developer->uuid = null;
557 if (!isset($game->developer->name)) {
558 $game->developer->name = 'unknown';
560 if (!isset($game->developer->supportEmail)) {
561 $game->developer->supportEmail = null;
563 if (!isset($game->developer->supportPhone)) {
564 $game->developer->supportPhone = null;
566 if (!isset($game->developer->founder)) {
567 $game->developer->founder = false;
571 function getFirstVideoUrl($media)
573 foreach ($media as $medium) {
574 if ($medium->type == 'video') {
581 function getAllImageUrls($media)
584 foreach ($media as $medium) {
585 if ($medium->type == 'image') {
586 $imageUrls[] = $medium->url;
592 function writeJson($path, $data)
595 $fullPath = $wwwDir . $path;
596 $dir = dirname($fullPath);
598 mkdir($dir, 0777, true);
602 json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
608 fwrite(STDERR, $msg . "\n");