"Best rated" and "Last updated" only for 20+ games categories
[stouyapi.git] / bin / import-game-data.php
index 782910f53d159e64e0d8763795968d09124fd22e..5ee582e9501c18220fa8913f437691ab6f06ea30 100755 (executable)
@@ -18,6 +18,7 @@ if (!is_file($foldersFile)) {
 }
 
 //default configuration values
+$GLOBALS['baseUrl']      = 'http://ouya.cweiske.de/';
 $GLOBALS['packagelists'] = [];
 $GLOBALS['urlRewrites']  = [];
 $cfgFile = __DIR__ . '/../config.php';
@@ -27,6 +28,11 @@ if (file_exists($cfgFile)) {
 
 $wwwDir = __DIR__ . '/../www/';
 
+$qrDir = $wwwDir . 'gen-qr/';
+if (!is_dir($qrDir)) {
+    mkdir($qrDir, 0775);
+}
+
 $baseDir   = dirname($foldersFile);
 $gameFiles = [];
 foreach (file($foldersFile) as $line) {
@@ -170,8 +176,8 @@ function buildDiscover(array $games)
         filterLastAdded($games, 10)
     );
     addDiscoverRow(
-        $data, 'Best rated',
-        filterBestRated($games, 10),
+        $data, 'Best rated games',
+        filterBestRatedGames($games, 10),
         true
     );
 
@@ -186,6 +192,7 @@ function buildDiscover(array $games)
         $data, 'Special',
         [
             'Best rated',
+            'Best rated games',
             'Most rated',
             'Random',
             'Last updated',
@@ -195,6 +202,10 @@ function buildDiscover(array $games)
         'api/v1/discover-data/' . categoryPath('Best rated') . '.json',
         buildSpecialCategory('Best rated', filterBestRated($games, 99))
     );
+    writeJson(
+        'api/v1/discover-data/' . categoryPath('Best rated games') . '.json',
+        buildSpecialCategory('Best rated games', filterBestRatedGames($games, 99))
+    );
     writeJson(
         'api/v1/discover-data/' . categoryPath('Most rated') . '.json',
         buildSpecialCategory('Most rated', filterMostDownloaded($games, 99))
@@ -279,15 +290,18 @@ function buildDiscoverCategory($name, $games)
         'rows'  => [],
         'tiles' => [],
     ];
-    addDiscoverRow(
-        $data, 'Last Updated',
-        filterLastUpdated($games, 10)
-    );
-    addDiscoverRow(
-        $data, 'Best rated',
-        filterBestRated($games, 10),
-        true
-    );
+
+    if (count($games) >= 20) {
+        addDiscoverRow(
+            $data, 'Last Updated',
+            filterLastUpdated($games, 10)
+        );
+        addDiscoverRow(
+            $data, 'Best rated',
+            filterBestRated($games, 10),
+            true
+        );
+    }
 
     $games = sortByTitle($games);
     $chunks = array_chunk($games, 4);
@@ -426,7 +440,7 @@ function buildAppDownload($game, $release)
             'fileSize'      => $release->size,
             'version'       => $release->uuid,
             'contentRating' => $game->contentRating,
-            'downloadLink'  => rewriteUrl($release->url),
+            'downloadLink'  => $release->url,
         ]
     ];
 }
@@ -880,6 +894,34 @@ function addMissingGameProperties($game)
     if (!isset($game->developer->founder)) {
         $game->developer->founder = false;
     }
+
+    if ($game->website) {
+        $qrfileName = preg_replace('#[^\\w\\d._-]#', '_', $game->website) . '.png';
+        $qrfilePath = $GLOBALS['qrDir'] . $qrfileName;
+        if (!file_exists($qrfilePath)) {
+            $cmd = __DIR__ . '/create-qr.sh'
+                 . ' ' . escapeshellarg($game->website)
+                 . ' ' . escapeshellarg($qrfilePath);
+            passthru($cmd, $retval);
+            if ($retval != 0) {
+                exit(20);
+            }
+        }
+        $qrUrlPath = $GLOBALS['baseUrl'] . 'gen-qr/' . $qrfileName;
+        $game->media[] = (object) [
+            'type' => 'image',
+            'url'  => $qrUrlPath,
+        ];
+    }
+
+    //rewrite urls from Internet Archive to our servers
+    $game->discover = rewriteUrl($game->discover);
+    foreach ($game->media as $medium) {
+        $medium->url = rewriteUrl($medium->url);
+    }
+    foreach ($game->releases as $release) {
+        $release->url = rewriteUrl($release->url);
+    }
 }
 
 /**