build current_gamer json
[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 $developers = [];
45 foreach ($gameFiles as $gameFile) {
46     $game = json_decode(file_get_contents($gameFile));
47     if ($game === null) {
48         error('JSON invalid at ' . $gameFile);
49     }
50     addMissingGameProperties($game);
51     $games[$game->packageName] = $game;
52
53     writeJson(
54         'api/v1/details-data/' . $game->packageName . '.json',
55         buildDetails($game)
56     );
57
58     if (!isset($developers[$game->developer->uuid])) {
59         $developers[$game->developer->uuid] = [
60             'info'     => $game->developer,
61             'products' => [],
62         ];
63     }
64
65     $products = $game->products ?? [];
66     foreach ($products as $product) {
67         writeJson(
68             'api/v1/developers/' . $game->developer->uuid
69             . '/products/' . $product->identifier . '.json',
70             buildDeveloperProductOnly($product, $game->developer)
71         );
72         $developers[$game->developer->uuid]['products'][] = $product;
73     }
74
75     /**/
76     writeJson(
77         'api/v1/games/' . $game->packageName . '/purchases',
78         buildPurchases($game)
79     );
80     /**/
81
82     /* this crashes babylonian twins
83     writeJson(
84         'api/v1/games/' . $game->packageName . '/purchases',
85         "{}\n"
86     );
87     */
88
89     writeJson(
90         'api/v1/apps/' . $game->packageName . '.json',
91         buildApps($game)
92     );
93     $latestRelease = $game->latestRelease;
94     writeJson(
95         'api/v1/apps/' . $latestRelease->uuid . '.json',
96         buildApps($game)
97     );
98
99     writeJson(
100         'api/v1/apps/' . $latestRelease->uuid . '-download.json',
101         buildAppDownload($game, $latestRelease)
102     );
103
104     if ($count++ > 20) {
105         //break;
106     }
107 }
108
109 foreach ($developers as $developer) {
110     writeJson(
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'])
115     );
116     writeJson(
117         //index.htm does not need a rewrite rule
118         'api/v1/developers/' . $developer['info']->uuid
119         . '/current_gamer',
120         buildDeveloperCurrentGamer()
121     );
122 }
123
124 writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
125 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
126
127
128 function buildDiscover(array $games)
129 {
130     $data = [
131         'title' => 'DISCOVER',
132         'rows'  => [],
133         'tiles' => [],
134     ];
135
136     addDiscoverRow(
137         $data, 'Last Updated',
138         filterLastUpdated($games, 10)
139     );
140     addDiscoverRow(
141         $data, 'Best rated',
142         filterBestRated($games, 10)
143     );
144     addDiscoverRow(
145         $data, "cweiske's picks",
146         filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
147     );
148
149     $players = [
150         //1 => '1 player',
151         2 => '2 players',
152         3 => '3 players',
153         4 => '4 players',
154     ];
155     addDiscoverRow($data, '# of players', $players);
156     foreach ($players as $num => $title) {
157         writeJson(
158             'api/v1/discover-data/' . categoryPath($title) . '.json',
159             buildDiscoverCategory($title, filterByPlayers($games, $num))
160         );
161     }
162
163     $ages = getAllAges($games);
164     natsort($ages);
165     addDiscoverRow($data, 'Content rating', $ages);
166     foreach ($ages as $num => $title) {
167         writeJson(
168             'api/v1/discover-data/' . categoryPath($title) . '.json',
169             buildDiscoverCategory($title, filterByAge($games, $title))
170         );
171     }
172
173     $genres = getAllGenres($games);
174     sort($genres);
175     addChunkedDiscoverRows($data, $genres, 'Genres');
176
177     foreach ($genres as $genre) {
178         writeJson(
179             'api/v1/discover-data/' . categoryPath($genre) . '.json',
180             buildDiscoverCategory($genre, filterByGenre($games, $genre))
181         );
182     }
183
184     $abc = array_merge(range('A', 'Z'), ['Other']);
185     addChunkedDiscoverRows($data, $abc, 'Alphabetical');
186     foreach ($abc as $letter) {
187         writeJson(
188             'api/v1/discover-data/' . categoryPath($letter) . '.json',
189             buildDiscoverCategory($letter, filterByLetter($games, $letter))
190         );
191     }
192
193     return $data;
194 }
195
196 /**
197  * A genre category page
198  */
199 function buildDiscoverCategory($name, $games)
200 {
201     $data = [
202         'title' => $name,
203         'rows'  => [],
204         'tiles' => [],
205     ];
206     addDiscoverRow(
207         $data, 'Last Updated',
208         filterLastUpdated($games, 10)
209     );
210     addDiscoverRow(
211         $data, 'Best rated',
212         filterBestRated($games, 10)
213     );
214
215     usort(
216         $games,
217         function ($gameA, $gameB) {
218             return strcmp($gameA->title, $gameB->title);
219         }
220     );
221     $chunks = array_chunk($games, 4);
222     foreach ($chunks as $chunkGames) {
223         addDiscoverRow($data, '', $chunkGames);
224     }
225
226     return $data;
227 }
228
229 function buildDiscoverHome(array $games)
230 {
231     //we do not want anything here for now
232     $data = [
233         'title' => 'home',
234         'rows'  => [
235             [
236                 'title' => 'FEATURED',
237                 'showPrice' => false,
238                 'ranked'    => false,
239                 'tiles'     => [],
240             ]
241         ],
242         'tiles' => [],
243     ];
244     return $data;
245 }
246
247 /**
248  * Build api/v1/apps/$packageName
249  */
250 function buildApps($game)
251 {
252     $latestRelease = $game->latestRelease;
253
254     $product      = null;
255     $gamePromoted = getPromotedProduct($game);
256     if ($gamePromoted) {
257         $product = buildProduct($gamePromoted);
258     }
259
260     // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-apps-xxx
261     return [
262         'app' => [
263             'uuid'          => $latestRelease->uuid,
264             'title'         => $game->title,
265             'overview'      => $game->overview,
266             'description'   => $game->description,
267             'gamerNumbers'  => $game->players,
268             'genres'        => $game->genres,
269
270             'website'       => $game->website,
271             'contentRating' => $game->contentRating,
272             'premium'       => $game->premium,
273             'firstPublishedAt' => $game->firstPublishedAt,
274
275             'likeCount'     => $game->rating->likeCount,
276             'ratingAverage' => $game->rating->average,
277             'ratingCount'   => $game->rating->count,
278
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,
286
287             'mainImageFullUrl' => $game->discover,
288             'videoUrl'         => getFirstVideoUrl($game->media),
289             'filepickerScreenshots' => getAllImageUrls($game->media),
290             'mobileAppIcon'    => null,
291
292             'developer'           => $game->developer->name,
293             'supportEmailAddress' => $game->developer->supportEmail,
294             'supportPhone'        => $game->developer->supportPhone,
295             'founder'             => $game->developer->founder,
296
297             'promotedProduct' => $product,
298         ],
299     ];
300 }
301
302 function buildAppDownload($game, $release)
303 {
304     return [
305         'app' => [
306             'fileSize'      => $release->size,
307             'version'       => $release->uuid,
308             'contentRating' => $game->contentRating,
309             'downloadLink'  => $release->url,
310         ]
311     ];
312 }
313
314 function buildProduct($product)
315 {
316     if ($product === null) {
317         return null;
318     }
319     return [
320         'type'          => 'entitlement',
321         'identifier'    => $product->identifier,
322         'name'          => $product->name,
323         'description'   => $product->description ?? '',
324         'localPrice'    => $product->localPrice,
325         'originalPrice' => $product->originalPrice,
326         'percentOff'    => 0,
327         'currency'      => $product->currency,
328     ];
329 }
330
331 /**
332  * Build /app/v1/details?app=org.example.game
333  */
334 function buildDetails($game)
335 {
336     $latestRelease = $game->latestRelease;
337
338     $mediaTiles = [];
339     if ($game->discover) {
340         $mediaTiles[] = [
341             'type' => 'image',
342             'urls' => [
343                 'thumbnail' => $game->discover,
344                 'full'      => $game->discover,
345             ],
346         ];
347     }
348     foreach ($game->media as $medium) {
349         if ($medium->type == 'image')  {
350             $mediaTiles[] = [
351                 'type' => 'image',
352                 'urls' => [
353                     'thumbnail' => $medium->thumb,
354                     'full'      => $medium->url,
355                 ],
356             ];
357         } else {
358             $mediaTiles[] = [
359                 'type' => 'video',
360                 'url'  => $medium->url,
361             ];
362         }
363     }
364
365     $buttons = [];
366     if (isset($game->links->unlocked)) {
367         $buttons[] = [
368             'text' => 'Show unlocked',
369             'url'  => 'ouya://launcher/details?app=' . $game->links->unlocked,
370             'bold' => true,
371         ];
372     }
373
374     $product      = null;
375     $gamePromoted = getPromotedProduct($game);
376     if ($gamePromoted) {
377         $product = buildProduct($gamePromoted);
378     }
379
380     // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
381     return [
382         'type'             => 'Game',
383         'title'            => $game->title,
384         'description'      => $game->description,
385         'gamerNumbers'     => $game->players,
386         'genres'           => $game->genres,
387
388         'suggestedAge'     => $game->contentRating,
389         'premium'          => $game->premium,
390         'inAppPurchases'   => $game->inAppPurchases,
391         'firstPublishedAt' => strtotime($game->firstPublishedAt),
392         'ccUrl'            => null,
393
394         'rating' => [
395             'count'   => $game->rating->count,
396             'average' => $game->rating->average,
397         ],
398
399         'apk' => [
400             'fileSize'    => $latestRelease->size,
401             'nativeSize'  => $latestRelease->nativeSize,
402             'publicSize'  => $latestRelease->publicSize,
403             'md5sum'      => $latestRelease->md5sum,
404             'filename'    => 'FIXME',
405             'errors'      => '',
406             'package'     => $game->packageName,
407             'versionCode' => $latestRelease->versionCode,
408             'state'       => 'complete',
409         ],
410
411         'version' => [
412             'number'      => $latestRelease->name,
413             'publishedAt' => strtotime($latestRelease->date),
414             'uuid'        => $latestRelease->uuid,
415         ],
416
417         'developer' => [
418             'name'    => $game->developer->name,
419             'founder' => $game->developer->founder,
420         ],
421
422         'metaData' => [
423             'key:rating.average',
424             'key:developer.name',
425             'key:suggestedAge',
426             number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
427         ],
428
429         'tileImage'     => $game->discover,
430         'mediaTiles'    => $mediaTiles,
431         'mobileAppIcon' => null,
432         'heroImage'     => [
433             'url' => null,
434         ],
435
436         'promotedProduct' => $product,
437         'buttons'         => $buttons,
438     ];
439 }
440
441 function buildDeveloperCurrentGamer()
442 {
443     return [
444         'gamer' => [
445             'uuid'     => '00702342-0000-1111-2222-c3e1500cafe2',
446             'username' => 'stouyapi',
447         ],
448     ];
449 }
450
451 /**
452  * For /api/v1/developers/xxx/products/?only=yyy
453  */
454 function buildDeveloperProductOnly($product, $developer)
455 {
456     return [
457         'developerName' => $developer->name,
458         'currency'      => $product->currency,
459         'products'      => [
460             buildProduct($product),
461         ],
462     ];
463 }
464
465 /**
466  * For /api/v1/developers/xxx/products/
467  */
468 function buildDeveloperProducts($products, $developer)
469 {
470     $jsonProducts = [];
471     foreach ($products as $product) {
472         $jsonProducts[] = buildProduct($product);
473     }
474     return [
475         'developerName' => $developer->name,
476         'currency'      => $products[0]->currency ?? 'EUR',
477         'products'      => $jsonProducts,
478     ];
479 }
480
481 function buildPurchases($game)
482 {
483     $purchasesData = [
484         'purchases' => [],
485     ];
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,
497         ];
498     }
499
500     $encryptedOnce  = dummyEncrypt($purchasesData);
501     $encryptedTwice = dummyEncrypt($encryptedOnce);
502     return $encryptedTwice;
503 }
504
505 function dummyEncrypt($data)
506 {
507     return [
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)
512         ) . "\n",
513     ];
514 }
515
516 function addChunkedDiscoverRows(&$data, $games, $title)
517 {
518     $chunks = array_chunk($games, 4);
519     $first = true;
520     foreach ($chunks as $chunk) {
521         addDiscoverRow(
522             $data, $first ? $title : '',
523             $chunk
524         );
525         $first = false;
526     }
527 }
528
529 function addDiscoverRow(&$data, $title, $games)
530 {
531     $row = [
532         'title'     => $title,
533         'showPrice' => true,
534         'ranked'    => false,
535         'tiles'     => [],
536     ];
537     foreach ($games as $game) {
538         if (is_string($game)) {
539             //category link
540             $tilePos = count($data['tiles']);
541             $data['tiles'][$tilePos] = buildDiscoverCategoryTile($game);
542
543         } else {
544             //game
545             if (isset($game->links->original)) {
546                 //do not link unlocked games.
547                 // people an access them via the original games
548                 continue;
549             }
550             $tilePos = findTile($data['tiles'], $game->packageName);
551             if ($tilePos === null) {
552                 $tilePos = count($data['tiles']);
553                 $data['tiles'][$tilePos] = buildDiscoverGameTile($game);
554             }
555         }
556         $row['tiles'][] = $tilePos;
557     }
558     $data['rows'][] = $row;
559 }
560
561 function findTile($tiles, $packageName)
562 {
563     foreach ($tiles as $pos => $tile) {
564         if ($tile['package'] == $packageName) {
565             return $pos;
566         }
567     }
568     return null;
569 }
570
571 function buildDiscoverCategoryTile($title)
572 {
573     return [
574         'url'   => 'ouya://launcher/discover/' . categoryPath($title),
575         'image' => '',
576         'title' => $title,
577         'type'  => 'discover'
578     ];
579 }
580
581 function buildDiscoverGameTile($game)
582 {
583     $latestRelease = $game->latestRelease;
584     return [
585         'gamerNumbers' => $game->players,
586         'genres' => $game->genres,
587         'url' => 'ouya://launcher/details?app=' . $game->packageName,
588         'latestVersion' => [
589             'apk' => [
590                 'md5sum' => $latestRelease->md5sum,
591             ],
592             'versionNumber' => $latestRelease->name,
593             'uuid' => $latestRelease->uuid,
594         ],
595         'inAppPurchases' => $game->inAppPurchases,
596         'promotedProduct' => null,
597         'premium' => $game->premium,
598         'type' => 'app',
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,
605         'rating' => [
606             'count' => $game->rating->count,
607             'average' => $game->rating->average,
608         ],
609         'promotedProduct' => buildProduct(getPromotedProduct($game)),
610     ];
611 }
612
613 function categoryPath($title)
614 {
615     return str_replace(['/', '\\', ' ', '+'], '_', $title);
616 }
617
618 function getAllAges($games)
619 {
620     $ages = [];
621     foreach ($games as $game) {
622         $ages[] = $game->contentRating;
623     }
624     return array_unique($ages);
625 }
626
627 function getAllGenres($games)
628 {
629     $genres = [];
630     foreach ($games as $game) {
631         $genres = array_merge($genres, $game->genres);
632     }
633     return array_unique($genres);
634 }
635
636 function addMissingGameProperties($game)
637 {
638     if (!isset($game->overview)) {
639         $game->overview = null;
640     }
641     if (!isset($game->description)) {
642         $game->description = '';
643     }
644     if (!isset($game->players)) {
645         $game->players = [1];
646     }
647     if (!isset($game->genres)) {
648         $game->genres = ['Unsorted'];
649     }
650     if (!isset($game->website)) {
651         $game->website = null;
652     }
653     if (!isset($game->contentRating)) {
654         $game->contentRating = 'Everyone';
655     }
656     if (!isset($game->premium)) {
657         $game->premium = false;
658     }
659     if (!isset($game->firstPublishedAt)) {
660         $game->firstPublishedAt = gmdate('c');
661     }
662
663     if (!isset($game->rating)) {
664         $game->rating = new stdClass();
665     }
666     if (!isset($game->rating->likeCount)) {
667         $game->rating->likeCount = 0;
668     }
669     if (!isset($game->rating->average)) {
670         $game->rating->average = 0;
671     }
672     if (!isset($game->rating->count)) {
673         $game->rating->count = 0;
674     }
675
676     $game->latestRelease = null;
677     $latestReleaseTimestamp = 0;
678     foreach ($game->releases as $release) {
679         if (!isset($release->publicSize)) {
680             $release->publicSize = 0;
681         }
682         if (!isset($release->nativeSize)) {
683             $release->nativeSize = 0;
684         }
685
686         $releaseTimestamp = strtotime($release->date);
687         if ($releaseTimestamp > $latestReleaseTimestamp) {
688             $game->latestRelease    = $release;
689             $latestReleaseTimestamp = $releaseTimestamp;
690         }
691     }
692     if ($game->latestRelease === null) {
693         error('No latest release for ' . $game->packageName);
694     }
695
696     if (!isset($game->media)) {
697         $game->media = [];
698     }
699
700     if (!isset($game->developer->uuid)) {
701         $game->developer->uuid = null;
702     }
703     if (!isset($game->developer->name)) {
704         $game->developer->name = 'unknown';
705     }
706     if (!isset($game->developer->supportEmail)) {
707         $game->developer->supportEmail = null;
708     }
709     if (!isset($game->developer->supportPhone)) {
710         $game->developer->supportPhone = null;
711     }
712     if (!isset($game->developer->founder)) {
713         $game->developer->founder = false;
714     }
715 }
716
717 function getFirstVideoUrl($media)
718 {
719     foreach ($media as $medium) {
720         if ($medium->type == 'video') {
721             return $medium->url;
722         }
723     }
724     return null;
725 }
726
727 function getAllImageUrls($media)
728 {
729     $imageUrls = [];
730     foreach ($media as $medium) {
731         if ($medium->type == 'image') {
732             $imageUrls[] = $medium->url;
733         }
734     }
735     return $imageUrls;
736 }
737
738 function getPromotedProduct($game)
739 {
740     if (!isset($game->products) || !count($game->products)) {
741         return null;
742     }
743     foreach ($game->products as $gameProd) {
744         if ($gameProd->promoted) {
745             return $gameProd;
746         }
747     }
748     return null;
749 }
750
751 function writeJson($path, $data)
752 {
753     global $wwwDir;
754     $fullPath = $wwwDir . $path;
755     $dir = dirname($fullPath);
756     if (!is_dir($dir)) {
757         mkdir($dir, 0777, true);
758     }
759     file_put_contents(
760         $fullPath,
761         json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
762     );
763 }
764
765 function error($msg)
766 {
767     fwrite(STDERR, $msg . "\n");
768     exit(1);
769 }
770 ?>