fix detail videos in new format
[stouyapi.git] / bin / import-game-data.php
index 2952aba852330f9dec87a4c601c0948c6b4755d4..75841b66b5be29c20979f7b2a013e39300e72e20 100755 (executable)
@@ -59,7 +59,7 @@ foreach ($gameFiles as $gameFile) {
         "{}\n"
     );
     */
-    
+
     writeJson(
         'api/v1/apps/' . $game->packageName . '.json',
         buildApps($game)
@@ -80,7 +80,7 @@ foreach ($gameFiles as $gameFile) {
     }
 }
 
-writeJson('api/v1/discover.json', buildDiscover($games));
+writeJson('api/v1/discover-data/discover.json', buildDiscover($games));
 writeJson('api/v1/discover-data/home.json', buildDiscoverHome($games));
 
 
@@ -104,9 +104,9 @@ function buildDiscover(array $games)
         $data, "cweiske's picks",
         filterByPackageNames($games, $GLOBALS['packagelists']['cweiskepicks'])
     );
-    
+
     $players = [
-        1 => '1 player',
+        //1 => '1 player',
         2 => '2 players',
         3 => '3 players',
         4 => '4 players',
@@ -119,18 +119,20 @@ function buildDiscover(array $games)
         );
     }
 
-    $genres = getAllGenres($games);
-    sort($genres);
-    $genreChunks = array_chunk($genres, 4);
-    $first = true;
-    foreach ($genreChunks as $chunk) {
-        addDiscoverRow(
-            $data, $first ? 'Genres' : '',
-            $chunk
+    $ages = getAllAges($games);
+    natsort($ages);
+    addDiscoverRow($data, 'Content rating', $ages);
+    foreach ($ages as $num => $title) {
+        writeJson(
+            'api/v1/discover-data/' . categoryPath($title) . '.json',
+            buildDiscoverCategory($title, filterByAge($games, $title))
         );
-        $first = false;
     }
 
+    $genres = getAllGenres($games);
+    sort($genres);
+    addChunkedDiscoverRows($data, $genres, 'Genres');
+
     foreach ($genres as $genre) {
         writeJson(
             'api/v1/discover-data/' . categoryPath($genre) . '.json',
@@ -138,6 +140,15 @@ function buildDiscover(array $games)
         );
     }
 
+    $abc = array_merge(range('A', 'Z'), ['Other']);
+    addChunkedDiscoverRows($data, $abc, 'Alphabetical');
+    foreach ($abc as $letter) {
+        writeJson(
+            'api/v1/discover-data/' . categoryPath($letter) . '.json',
+            buildDiscoverCategory($letter, filterByLetter($games, $letter))
+        );
+    }
+
     return $data;
 }
 
@@ -163,7 +174,7 @@ function buildDiscoverCategory($name, $games)
     usort(
         $games,
         function ($gameA, $gameB) {
-            return strcmp($gameB->title, $gameA->title);
+            return strcmp($gameA->title, $gameB->title);
         }
     );
     $chunks = array_chunk($games, 4);
@@ -226,9 +237,9 @@ function buildApps($game)
             'publicSize'    => $latestRelease->publicSize,
             'nativeSize'    => $latestRelease->nativeSize,
 
-            'mainImageFullUrl' => $game->media->discover,
-            'videoUrl'         => $game->media->video,
-            'filepickerScreenshots' => $game->media->screenshots,
+            'mainImageFullUrl' => $game->discover,
+            'videoUrl'         => getFirstVideoUrl($game->media),
+            'filepickerScreenshots' => getAllImageUrls($game->media),
             'mobileAppIcon'    => null,
 
             'developer'           => $game->developer->name,
@@ -261,31 +272,30 @@ function buildDetails($game)
     $latestRelease = $game->latestRelease;
 
     $mediaTiles = [];
-    if ($game->media->discover) {
+    if ($game->discover) {
         $mediaTiles[] = [
             'type' => 'image',
             'urls' => [
-                'thumbnail' => $game->media->discover,
-                'full'      => $game->media->discover,
+                'thumbnail' => $game->discover,
+                'full'      => $game->discover,
             ],
-            'fp_url' => $game->media->discover,
         ];
     }
-    if ($game->media->video) {
-        $mediaTiles[] = [
-            'type' => 'video',
-            'url'  => $game->media->video,
-        ];
-    }
-    foreach ($game->media->screenshots as $screenshot) {
-        $mediaTiles[] = [
-            'type' => 'image',
-            'urls' => [
-                'thumbnail' => $screenshot,
-                'full'      => $screenshot,
-            ],
-            'fp_url' => $screenshot,
-        ];
+    foreach ($game->media as $medium) {
+        if ($medium->type == 'image')  {
+            $mediaTiles[] = [
+                'type' => 'image',
+                'urls' => [
+                    'thumbnail' => $medium->thumb,
+                    'full'      => $medium->url,
+                ],
+            ];
+        } else {
+            $mediaTiles[] = [
+                'type' => 'video',
+                'url'  => $medium->url,
+            ];
+        }
     }
 
     // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
@@ -337,7 +347,7 @@ function buildDetails($game)
             number_format($latestRelease->size / 1024 / 1024, 2, '.', '') . ' MiB',
         ],
 
-        'tileImage'     => $game->media->discover,
+        'tileImage'     => $game->discover,
         'mediaTiles'    => $mediaTiles,
         'mobileAppIcon' => null,
         'heroImage'     => [
@@ -348,6 +358,19 @@ function buildDetails($game)
     ];
 }
 
+function addChunkedDiscoverRows(&$data, $games, $title)
+{
+    $chunks = array_chunk($games, 4);
+    $first = true;
+    foreach ($chunks as $chunk) {
+        addDiscoverRow(
+            $data, $first ? $title : '',
+            $chunk
+        );
+        $first = false;
+    }
+}
+
 function addDiscoverRow(&$data, $title, $games)
 {
     $row = [
@@ -417,7 +440,7 @@ function buildDiscoverGameTile($game)
         'updated_at' => strtotime($latestRelease->date),
         'updatedAt' => $latestRelease->date,
         'title' => $game->title,
-        'image' => $game->media->discover,
+        'image' => $game->discover,
         'contentRating' => $game->contentRating,
         'rating' => [
             'count' => $game->rating->count,
@@ -428,7 +451,16 @@ function buildDiscoverGameTile($game)
 
 function categoryPath($title)
 {
-    return str_replace(['/', '\\', ' '], '_', $title);
+    return str_replace(['/', '\\', ' ', '+'], '_', $title);
+}
+
+function getAllAges($games)
+{
+    $ages = [];
+    foreach ($games as $game) {
+        $ages[] = $game->contentRating;
+    }
+    return array_unique($ages);
 }
 
 function getAllGenres($games)
@@ -500,12 +532,10 @@ function addMissingGameProperties($game)
         error('No latest release for ' . $game->packageName);
     }
 
-    if (!isset($game->media->video)) {
-        $game->media->video = null;
-    }
-    if (!isset($game->media->screenshots)) {
-        $game->media->screenshots = [];
+    if (!isset($game->media)) {
+        $game->media = [];
     }
+
     if (!isset($game->developer->uuid)) {
         $game->developer->uuid = null;
     }
@@ -523,6 +553,27 @@ function addMissingGameProperties($game)
     }
 }
 
+function getFirstVideoUrl($media)
+{
+    foreach ($media as $medium) {
+        if ($medium->type == 'video') {
+            return $medium->url;
+        }
+    }
+    return null;
+}
+
+function getAllImageUrls($media)
+{
+    $imageUrls = [];
+    foreach ($media as $medium) {
+        if ($medium->type == 'image') {
+            $imageUrls[] = $medium->url;
+        }
+    }
+    return $imageUrls;
+}
+
 function writeJson($path, $data)
 {
     global $wwwDir;
@@ -533,7 +584,7 @@ function writeJson($path, $data)
     }
     file_put_contents(
         $fullPath,
-        json_encode($data, JSON_PRETTY_PRINT) . "\n"
+        json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"
     );
 }