Do not crash when only few games exist
[stouyapi.git] / bin / import-game-data.php
index 283c06b60e1e081762a8d8d56086fc5e709913b2..182708a050e0337f3f3c5c06b4117efa7430ddbd 100755 (executable)
@@ -7,6 +7,7 @@
  * @author Christian Weiske <cweiske@cweiske.de>
  */
 ini_set('xdebug.halt_level', E_WARNING|E_NOTICE|E_USER_WARNING|E_USER_NOTICE);
+require_once __DIR__ . '/functions.php';
 require_once __DIR__ . '/filters.php';
 if (!isset($argv[1])) {
     error('Pass the path to a "folders" file with game data json files folder names');
@@ -419,7 +420,7 @@ function buildDetails($game)
             $mediaTiles[] = [
                 'type' => 'image',
                 'urls' => [
-                    'thumbnail' => $medium->thumb,
+                    'thumbnail' => $medium->thumb ?? $medium->url,
                     'full'      => $medium->url,
                 ],
             ];
@@ -448,6 +449,13 @@ function buildDetails($game)
         $product = buildProduct($gamePromoted);
     }
 
+    $iaUrl = null;
+    if (isset($game->latestRelease->url)
+        && substr($game->latestRelease->url, 0, 29) == 'https://archive.org/download/'
+    ) {
+        $iaUrl = dirname($game->latestRelease->url) . '/';
+    }
+
     // http://cweiske.de/ouya-store-api-docs.htm#get-https-devs-ouya-tv-api-v1-details
     return [
         'type'             => 'Game',
@@ -506,6 +514,11 @@ function buildDetails($game)
 
         'promotedProduct' => $product,
         'buttons'         => $buttons,
+
+        'stouyapi' => [
+            'internet-archive' => $iaUrl,
+            'developer-url'    => $game->developer->website ?? null,
+        ]
     ];
 }
 
@@ -538,6 +551,9 @@ function buildDeveloperProductOnly($product, $developer)
  */
 function buildDeveloperProducts($products, $developer)
 {
+    //remove duplicates
+    $products = array_values(array_column($products, null, 'identifier'));
+
     $jsonProducts = [];
     foreach ($products as $product) {
         $jsonProducts[] = buildProduct($product);
@@ -698,11 +714,6 @@ function buildDiscoverGameTile($game)
     ];
 }
 
-function categoryPath($title)
-{
-    return str_replace(['/', '\\', ' ', '+', '?'], '_', $title);
-}
-
 function getAllAges($games)
 {
     $ages = [];