Generate QR codes for game websites as last game image
[stouyapi.git] / bin / import-game-data.php
index c0b500c18198d86d22826a6344c319f122453b60..232b11fd3426b0712e074c5d171afb992d5ce849 100755 (executable)
@@ -27,6 +27,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) {
@@ -166,8 +171,8 @@ function buildDiscover(array $games)
     ];
 
     addDiscoverRow(
-        $data, 'Last Updated',
-        filterLastUpdated($games, 10)
+        $data, 'New games',
+        filterLastAdded($games, 10)
     );
     addDiscoverRow(
         $data, 'Best rated',
@@ -188,6 +193,7 @@ function buildDiscover(array $games)
             'Best rated',
             'Most rated',
             'Random',
+            'Last updated',
         ]
     );
     writeJson(
@@ -205,6 +211,10 @@ function buildDiscover(array $games)
             filterRandom($games, 99)
         )
     );
+    writeJson(
+        'api/v1/discover-data/' . categoryPath('Last updated') . '.json',
+        buildSpecialCategory('Last updated', filterLastUpdated($games, 99))
+    );
 
     $players = [
         //1 => '1 player',
@@ -216,7 +226,18 @@ function buildDiscover(array $games)
     foreach ($players as $num => $title) {
         writeJson(
             'api/v1/discover-data/' . categoryPath($title) . '.json',
-            buildDiscoverCategory($title, filterByPlayers($games, $num))
+            buildDiscoverCategory(
+                $title,
+                //I do not want emulators here,
+                // and neither Streaming apps
+                filterByGenre(
+                    filterByGenre(
+                        filterByPlayers($games, $num),
+                        'Emulator', true
+                    ),
+                    'App', true
+                )
+            )
         );
     }
 
@@ -814,7 +835,9 @@ function addMissingGameProperties($game)
         $game->rating->count = 0;
     }
 
+    $game->firstRelease  = null;
     $game->latestRelease = null;
+    $firstReleaseTimestamp  = null;
     $latestReleaseTimestamp = 0;
     foreach ($game->releases as $release) {
         if (!isset($release->publicSize)) {
@@ -829,6 +852,15 @@ function addMissingGameProperties($game)
             $game->latestRelease    = $release;
             $latestReleaseTimestamp = $releaseTimestamp;
         }
+        if ($firstReleaseTimestamp === null
+            || $releaseTimestamp < $firstReleaseTimestamp
+        ) {
+            $game->firstRelease    = $release;
+            $firstReleaseTimestamp = $releaseTimestamp;
+        }
+    }
+    if ($game->firstRelease === null) {
+        error('No first release for ' . $game->packageName);
     }
     if ($game->latestRelease === null) {
         error('No latest release for ' . $game->packageName);
@@ -853,6 +885,25 @@ 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 = '/gen-qr/' . $qrfileName;
+        $game->media[] = (object) [
+            'type' => 'image',
+            'url'  => $qrUrlPath,
+        ];
+    }
 }
 
 /**