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