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($gameB->title, $gameA->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->media->discover,
241 'videoUrl' => $game->media->video,
242 'filepickerScreenshots' => $game->media->screenshots,
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->media->discover) {
279 'thumbnail' => $game->media->discover,
280 'full' => $game->media->discover,
282 'fp_url' => $game->media->discover,
285 if ($game->media->video) {
288 'url' => $game->media->video,
291 foreach ($game->media->screenshots as $screenshot) {
295 'thumbnail' => $screenshot,
296 'full' => $screenshot,
298 'fp_url' => $screenshot,
302 // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
305 'title' => $game->title,
306 'description' => $game->description,
307 'gamerNumbers' => $game->players,
308 'genres' => $game->genres,
310 'suggestedAge' => $game->contentRating,
311 'premium' => $game->premium,
312 'inAppPurchases' => $game->inAppPurchases,
313 'firstPublishedAt' => strtotime($game->firstPublishedAt),
317 'count' => $game->rating->count,
318 'average' => $game->rating->average,
322 'fileSize' => $latestRelease->size,
323 'nativeSize' => $latestRelease->nativeSize,
324 'publicSize' => $latestRelease->publicSize,
325 'md5sum' => $latestRelease->md5sum,
326 'filename' => 'FIXME',
328 'package' => $game->packageName,
329 'versionCode' => $latestRelease->versionCode,
330 'state' => 'complete',
334 'number' => $latestRelease->name,
335 'publishedAt' => strtotime($latestRelease->date),
336 'uuid' => $latestRelease->uuid,
340 'name' => $game->developer->name,
341 'founder' => $game->developer->founder,
345 'key:rating.average',
346 'key:developer.name',
348 number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
351 'tileImage' => $game->media->discover,
352 'mediaTiles' => $mediaTiles,
353 'mobileAppIcon' => null,
358 'promotedProduct' => null,
362 function addChunkedDiscoverRows(&$data, $games, $title)
364 $chunks = array_chunk($games, 4);
366 foreach ($chunks as $chunk) {
368 $data, $first ? $title : '',
375 function addDiscoverRow(&$data, $title, $games)
379 'showPrice' => false,
383 foreach ($games as $game) {
384 if (is_string($game)) {
386 $tilePos = count($data['tiles']);
387 $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
391 $tilePos = findTile($data['tiles'], $game->packageName);
392 if ($tilePos === null) {
393 $tilePos = count($data['tiles']);
394 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
397 $row['tiles'][] = $tilePos;
399 $data['rows'][] = $row;
402 function findTile($tiles, $packageName)
404 foreach ($tiles as $pos => $tile) {
405 if ($tile['package'] == $packageName) {
412 function buildDiscoverCategoryTile($title)
415 'url' => 'ouya://launcher/discover/' . categoryPath($title),
422 function buildDiscoverGameTile($game)
424 $latestRelease = $game->latestRelease;
426 'gamerNumbers' => $game->players,
427 'genres' => $game->genres,
428 'url' => 'ouya://launcher/details?app=' . $game->packageName,
431 'md5sum' => $latestRelease->md5sum,
433 'versionNumber' => $latestRelease->name,
434 'uuid' => $latestRelease->uuid,
436 'inAppPurchases' => $game->inAppPurchases,
437 'promotedProduct' => null,
438 'premium' => $game->premium,
440 'package' => $game->packageName,
441 'updated_at' => strtotime($latestRelease->date),
442 'updatedAt' => $latestRelease->date,
443 'title' => $game->title,
444 'image' => $game->media->discover,
445 'contentRating' => $game->contentRating,
447 'count' => $game->rating->count,
448 'average' => $game->rating->average,
453 function categoryPath($title)
455 return str_replace(['/', '\\', ' ', '+'], '_', $title);
458 function getAllAges($games)
461 foreach ($games as $game) {
462 $ages[] = $game->contentRating;
464 return array_unique($ages);
467 function getAllGenres($games)
470 foreach ($games as $game) {
471 $genres = array_merge($genres, $game->genres);
473 return array_unique($genres);
476 function addMissingGameProperties($game)
478 if (!isset($game->overview)) {
479 $game->overview = null;
481 if (!isset($game->description)) {
482 $game->description = '';
484 if (!isset($game->players)) {
485 $game->players = [1];
487 if (!isset($game->genres)) {
488 $game->genres = ['Unsorted'];
490 if (!isset($game->website)) {
491 $game->website = null;
493 if (!isset($game->contentRating)) {
494 $game->contentRating = 'Everyone';
496 if (!isset($game->premium)) {
497 $game->premium = false;
499 if (!isset($game->firstPublishedAt)) {
500 $game->firstPublishedAt = gmdate('c');
503 if (!isset($game->rating)) {
504 $game->rating = new stdClass();
506 if (!isset($game->rating->likeCount)) {
507 $game->rating->likeCount = 0;
509 if (!isset($game->rating->average)) {
510 $game->rating->average = 0;
512 if (!isset($game->rating->count)) {
513 $game->rating->count = 0;
516 $game->latestRelease = null;
517 $latestReleaseTimestamp = 0;
518 foreach ($game->releases as $release) {
519 if (!isset($release->publicSize)) {
520 $release->publicSize = 0;
522 if (!isset($release->nativeSize)) {
523 $release->nativeSize = 0;
526 $releaseTimestamp = strtotime($release->date);
527 if ($releaseTimestamp > $latestReleaseTimestamp) {
528 $game->latestRelease = $release;
529 $latestReleaseTimestamp = $releaseTimestamp;
532 if ($game->latestRelease === null) {
533 error('No latest release for ' . $game->packageName);
536 if (!isset($game->media->video)) {
537 $game->media->video = null;
539 if (!isset($game->media->screenshots)) {
540 $game->media->screenshots = [];
542 if (!isset($game->developer->uuid)) {
543 $game->developer->uuid = null;
545 if (!isset($game->developer->name)) {
546 $game->developer->name = 'unknown';
548 if (!isset($game->developer->supportEmail)) {
549 $game->developer->supportEmail = null;
551 if (!isset($game->developer->supportPhone)) {
552 $game->developer->supportPhone = null;
554 if (!isset($game->developer->founder)) {
555 $game->developer->founder = false;
559 function writeJson($path, $data)
562 $fullPath = $wwwDir . $path;
563 $dir = dirname($fullPath);
565 mkdir($dir, 0777, true);
569 json_encode($data, JSON_PRETTY_PRINT) . "\n"
575 fwrite(STDERR, $msg . "\n");