store main discover file in discover-data dir
[stouyapi.git] / bin / import-game-data.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * Import games from a OUYA game data repository
5  *
6  * @link https://github.com/cweiske/ouya-game-data/
7  * @author Christian Weiske <cweiske@cweiske.de>
8  */
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');
13 }
14 $foldersFile = $argv[1];
15 if (!is_file($foldersFile)) {
16     error('Given path is not a file: ' . $foldersFile);
17 }
18
19 $GLOBALS['packagelists']['cweiskepicks'] = [
20     'de.eiswuxe.blookid2',
21     'com.cosmos.babyloniantwins'
22 ];
23
24 $wwwDir = __DIR__ . '/../www/';
25
26 $baseDir   = dirname($foldersFile);
27 $gameFiles = [];
28 foreach (file($foldersFile) as $line) {
29     $line = trim($line);
30     if (strlen($line)) {
31         if (strpos($line, '..') !== false) {
32             error('Path attack in ' . $folder);
33         }
34         $folder = $baseDir . '/' . $line;
35         if (!is_dir($folder)) {
36             error('Folder does not exist: ' . $folder);
37         }
38         $gameFiles = array_merge($gameFiles, glob($folder . '/*.json'));
39     }
40 }
41
42 $games = [];
43 $count = 0;
44 foreach ($gameFiles as $gameFile) {
45     $game = json_decode(file_get_contents($gameFile));
46     if ($game === null) {
47         error('JSON invalid at ' . $gameFile);
48     }
49     addMissingGameProperties($game);
50     $games[$game->packageName] = $game;
51
52     writeJson(
53         'api/v1/details-data/' . $game->packageName . '.json',
54         buildDetails($game)
55     );
56     /* this crashes babylonian twins
57     writeJson(
58         'api/v1/games/' . $game->packageName . '/purchases',
59         "{}\n"
60     );
61     */
62     
63     writeJson(
64         'api/v1/apps/' . $game->packageName . '.json',
65         buildApps($game)
66     );
67     $latestRelease = $game->latestRelease;
68     writeJson(
69         'api/v1/apps/' . $latestRelease->uuid . '.json',
70         buildApps($game)
71     );
72
73     writeJson(
74         'api/v1/apps/' . $latestRelease->uuid . '-download.json',
75         buildAppDownload($game, $latestRelease)
76     );
77
78     if ($count++ > 20) {
79         //break;
80     }
81 }
82
83 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
84 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
85
86
87 function buildDiscover(array $games)
88 {
89     $data = [
90         'title' => 'DISCOVER',
91         'rows'  => [],
92         'tiles' => [],
93     ];
94
95     addDiscoverRow(
96         $data, 'Last Updated',
97         filterLastUpdated($games, 10)
98     );
99     addDiscoverRow(
100         $data, 'Best rated',
101         filterBestRated($games, 10)
102     );
103     addDiscoverRow(
104         $data, "cweiske's picks",
105         filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
106     );
107     
108     $players = [
109         //1 => '1 player',
110         2 => '2 players',
111         3 => '3 players',
112         4 => '4 players',
113     ];
114     addDiscoverRow($data, '# of players', $players);
115     foreach ($players as $num => $title) {
116         writeJson(
117             'api/v1/discover-data/' . categoryPath($title) . '.json',
118             buildDiscoverCategory($title, filterByPlayers($games, $num))
119         );
120     }
121
122     $ages = getAllAges($games);
123     natsort($ages);
124     addDiscoverRow($data, 'Content rating', $ages);
125     foreach ($ages as $num => $title) {
126         writeJson(
127             'api/v1/discover-data/' . categoryPath($title) . '.json',
128             buildDiscoverCategory($title, filterByAge($games, $title))
129         );
130     }
131
132     $genres = getAllGenres($games);
133     sort($genres);
134     addChunkedDiscoverRows($data, $genres, 'Genres');
135
136     foreach ($genres as $genre) {
137         writeJson(
138             'api/v1/discover-data/' . categoryPath($genre) . '.json',
139             buildDiscoverCategory($genre, filterByGenre($games, $genre))
140         );
141     }
142
143     $abc = array_merge(range('A', 'Z'), ['Other']);
144     addChunkedDiscoverRows($data, $abc, 'Alphabetical');
145     foreach ($abc as $letter) {
146         writeJson(
147             'api/v1/discover-data/' . categoryPath($letter) . '.json',
148             buildDiscoverCategory($letter, filterByLetter($games, $letter))
149         );
150     }
151
152     return $data;
153 }
154
155 /**
156  * A genre category page
157  */
158 function buildDiscoverCategory($name, $games)
159 {
160     $data = [
161         'title' => $name,
162         'rows'  => [],
163         'tiles' => [],
164     ];
165     addDiscoverRow(
166         $data, 'Last Updated',
167         filterLastUpdated($games, 10)
168     );
169     addDiscoverRow(
170         $data, 'Best rated',
171         filterBestRated($games, 10)
172     );
173
174     usort(
175         $games,
176         function ($gameA, $gameB) {
177             return strcmp($gameB->title, $gameA->title);
178         }
179     );
180     $chunks = array_chunk($games, 4);
181     foreach ($chunks as $chunkGames) {
182         addDiscoverRow($data, '', $chunkGames);
183     }
184
185     return $data;
186 }
187
188 function buildDiscoverHome(array $games)
189 {
190     //we do not want anything here for now
191     $data = [
192         'title' => 'home',
193         'rows'  => [
194             [
195                 'title' => 'FEATURED',
196                 'showPrice' => false,
197                 'ranked'    => false,
198                 'tiles'     => [],
199             ]
200         ],
201         'tiles' => [],
202     ];
203     return $data;
204 }
205
206 /**
207  * Build api/v1/apps/$packageName
208  */
209 function buildApps($game)
210 {
211     $latestRelease = $game->latestRelease;
212
213     // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
214     return [
215         'app' => [
216             'uuid'          => $latestRelease->uuid,
217             'title'         => $game->title,
218             'overview'      => $game->overview,
219             'description'   => $game->description,
220             'gamerNumbers'  => $game->players,
221             'genres'        => $game->genres,
222
223             'website'       => $game->website,
224             'contentRating' => $game->contentRating,
225             'premium'       => $game->premium,
226             'firstPublishedAt' => $game->firstPublishedAt,
227
228             'likeCount'     => $game->rating->likeCount,
229             'ratingAverage' => $game->rating->average,
230             'ratingCount'   => $game->rating->count,
231
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,
239
240             'mainImageFullUrl' => $game->media->discover,
241             'videoUrl'         => $game->media->video,
242             'filepickerScreenshots' => $game->media->screenshots,
243             'mobileAppIcon'    => null,
244
245             'developer'           => $game->developer->name,
246             'supportEmailAddress' => $game->developer->supportEmail,
247             'supportPhone'        => $game->developer->supportPhone,
248             'founder'             => $game->developer->founder,
249
250             'promotedProduct' => null,
251         ],
252     ];
253 }
254
255 function buildAppDownload($game, $release)
256 {
257     return [
258         'app' => [
259             'fileSize'      => $release->size,
260             'version'       => $release->uuid,
261             'contentRating' => $game->contentRating,
262             'downloadLink'  => $release->url,
263         ]
264     ];
265 }
266
267 /**
268  * Build /app/v1/details?app=org.example.game
269  */
270 function buildDetails($game)
271 {
272     $latestRelease = $game->latestRelease;
273
274     $mediaTiles = [];
275     if ($game->media->discover) {
276         $mediaTiles[] = [
277             'type' => 'image',
278             'urls' => [
279                 'thumbnail' => $game->media->discover,
280                 'full'      => $game->media->discover,
281             ],
282             'fp_url' => $game->media->discover,
283         ];
284     }
285     if ($game->media->video) {
286         $mediaTiles[] = [
287             'type' => 'video',
288             'url'  => $game->media->video,
289         ];
290     }
291     foreach ($game->media->screenshots as $screenshot) {
292         $mediaTiles[] = [
293             'type' => 'image',
294             'urls' => [
295                 'thumbnail' => $screenshot,
296                 'full'      => $screenshot,
297             ],
298             'fp_url' => $screenshot,
299         ];
300     }
301
302     // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
303     return [
304         'type'             => 'Game',
305         'title'            => $game->title,
306         'description'      => $game->description,
307         'gamerNumbers'     => $game->players,
308         'genres'           => $game->genres,
309
310         'suggestedAge'     => $game->contentRating,
311         'premium'          => $game->premium,
312         'inAppPurchases'   => $game->inAppPurchases,
313         'firstPublishedAt' => strtotime($game->firstPublishedAt),
314         'ccUrl'            => null,
315
316         'rating' => [
317             'count'   => $game->rating->count,
318             'average' => $game->rating->average,
319         ],
320
321         'apk' => [
322             'fileSize'    => $latestRelease->size,
323             'nativeSize'  => $latestRelease->nativeSize,
324             'publicSize'  => $latestRelease->publicSize,
325             'md5sum'      => $latestRelease->md5sum,
326             'filename'    => 'FIXME',
327             'errors'      => '',
328             'package'     => $game->packageName,
329             'versionCode' => $latestRelease->versionCode,
330             'state'       => 'complete',
331         ],
332
333         'version' => [
334             'number'      => $latestRelease->name,
335             'publishedAt' => strtotime($latestRelease->date),
336             'uuid'        => $latestRelease->uuid,
337         ],
338
339         'developer' => [
340             'name'    => $game->developer->name,
341             'founder' => $game->developer->founder,
342         ],
343
344         'metaData' => [
345             'key:rating.average',
346             'key:developer.name',
347             'key:suggestedAge',
348             number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
349         ],
350
351         'tileImage'     => $game->media->discover,
352         'mediaTiles'    => $mediaTiles,
353         'mobileAppIcon' => null,
354         'heroImage'     => [
355             'url' => null,
356         ],
357
358         'promotedProduct' => null,
359     ];
360 }
361
362 function addChunkedDiscoverRows(&$data, $games, $title)
363 {
364     $chunks = array_chunk($games, 4);
365     $first = true;
366     foreach ($chunks as $chunk) {
367         addDiscoverRow(
368             $data, $first ? $title : '',
369             $chunk
370         );
371         $first = false;
372     }
373 }
374
375 function addDiscoverRow(&$data, $title, $games)
376 {
377     $row = [
378         'title'     => $title,
379         'showPrice' => false,
380         'ranked'    => false,
381         'tiles'     => [],
382     ];
383     foreach ($games as $game) {
384         if (is_string($game)) {
385             //category link
386             $tilePos = count($data['tiles']);
387             $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
388
389         } else {
390             //game
391             $tilePos = findTile($data['tiles'], $game->packageName);
392             if ($tilePos === null) {
393                 $tilePos = count($data['tiles']);
394                 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
395             }
396         }
397         $row['tiles'][] = $tilePos;
398     }
399     $data['rows'][] = $row;
400 }
401
402 function findTile($tiles, $packageName)
403 {
404     foreach ($tiles as $pos => $tile) {
405         if ($tile['package'] == $packageName) {
406             return $pos;
407         }
408     }
409     return null;
410 }
411
412 function buildDiscoverCategoryTile($title)
413 {
414     return [
415         'url'   => 'ouya://launcher/discover/' . categoryPath($title),
416         'image' => '',
417         'title' => $title,
418         'type'  => 'discover'
419     ];
420 }
421
422 function buildDiscoverGameTile($game)
423 {
424     $latestRelease = $game->latestRelease;
425     return [
426         'gamerNumbers' => $game->players,
427         'genres' => $game->genres,
428         'url' => 'ouya://launcher/details?app=' . $game->packageName,
429         'latestVersion' => [
430             'apk' => [
431                 'md5sum' => $latestRelease->md5sum,
432             ],
433             'versionNumber' => $latestRelease->name,
434             'uuid' => $latestRelease->uuid,
435         ],
436         'inAppPurchases' => $game->inAppPurchases,
437         'promotedProduct' => null,
438         'premium' => $game->premium,
439         'type' => 'app',
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,
446         'rating' => [
447             'count' => $game->rating->count,
448             'average' => $game->rating->average,
449         ],
450     ];
451 }
452
453 function categoryPath($title)
454 {
455     return str_replace(['/', '\\', ' ', '+'], '_', $title);
456 }
457
458 function getAllAges($games)
459 {
460     $ages = [];
461     foreach ($games as $game) {
462         $ages[] = $game->contentRating;
463     }
464     return array_unique($ages);
465 }
466
467 function getAllGenres($games)
468 {
469     $genres = [];
470     foreach ($games as $game) {
471         $genres = array_merge($genres, $game->genres);
472     }
473     return array_unique($genres);
474 }
475
476 function addMissingGameProperties($game)
477 {
478     if (!isset($game->overview)) {
479         $game->overview = null;
480     }
481     if (!isset($game->description)) {
482         $game->description = '';
483     }
484     if (!isset($game->players)) {
485         $game->players = [1];
486     }
487     if (!isset($game->genres)) {
488         $game->genres = ['Unsorted'];
489     }
490     if (!isset($game->website)) {
491         $game->website = null;
492     }
493     if (!isset($game->contentRating)) {
494         $game->contentRating = 'Everyone';
495     }
496     if (!isset($game->premium)) {
497         $game->premium = false;
498     }
499     if (!isset($game->firstPublishedAt)) {
500         $game->firstPublishedAt = gmdate('c');
501     }
502
503     if (!isset($game->rating)) {
504         $game->rating = new stdClass();
505     }
506     if (!isset($game->rating->likeCount)) {
507         $game->rating->likeCount = 0;
508     }
509     if (!isset($game->rating->average)) {
510         $game->rating->average = 0;
511     }
512     if (!isset($game->rating->count)) {
513         $game->rating->count = 0;
514     }
515
516     $game->latestRelease = null;
517     $latestReleaseTimestamp = 0;
518     foreach ($game->releases as $release) {
519         if (!isset($release->publicSize)) {
520             $release->publicSize = 0;
521         }
522         if (!isset($release->nativeSize)) {
523             $release->nativeSize = 0;
524         }
525
526         $releaseTimestamp = strtotime($release->date);
527         if ($releaseTimestamp > $latestReleaseTimestamp) {
528             $game->latestRelease    = $release;
529             $latestReleaseTimestamp = $releaseTimestamp;
530         }
531     }
532     if ($game->latestRelease === null) {
533         error('No latest release for ' . $game->packageName);
534     }
535
536     if (!isset($game->media->video)) {
537         $game->media->video = null;
538     }
539     if (!isset($game->media->screenshots)) {
540         $game->media->screenshots = [];
541     }
542     if (!isset($game->developer->uuid)) {
543         $game->developer->uuid = null;
544     }
545     if (!isset($game->developer->name)) {
546         $game->developer->name = 'unknown';
547     }
548     if (!isset($game->developer->supportEmail)) {
549         $game->developer->supportEmail = null;
550     }
551     if (!isset($game->developer->supportPhone)) {
552         $game->developer->supportPhone = null;
553     }
554     if (!isset($game->developer->founder)) {
555         $game->developer->founder = false;
556     }
557 }
558
559 function writeJson($path, $data)
560 {
561     global $wwwDir;
562     $fullPath = $wwwDir . $path;
563     $dir = dirname($fullPath);
564     if (!is_dir($dir)) {
565         mkdir($dir, 0777, true);
566     }
567     file_put_contents(
568         $fullPath,
569         json_encode($data, JSON_PRETTY_PRINT) . "\n"
570     );
571 }
572
573 function error($msg)
574 {
575     fwrite(STDERR, $msg . "\n");
576     exit(1);
577 }
578 ?>