From b3d8bc750ea4a799ee5bea04e1203af0544c577e Mon Sep 17 00:00:00 2001 From: DariusIII Date: Thu, 23 Mar 2023 14:07:28 +0100 Subject: [PATCH] Add artisan command to populate steam apps table --- Blacklight/ColorCLI.php | 69 ++++++++++++ Blacklight/Steam.php | 64 ++++++----- app/Console/Commands/NntmuxPopulateAniDB.php | 41 ++++++++ app/Console/Commands/NntmuxPopulateSteam.php | 34 ++++++ cli/data/populate_anidb.php | 28 ----- composer.json | 1 + composer.lock | 105 ++++++++++++++++++- 7 files changed, 280 insertions(+), 62 deletions(-) create mode 100644 app/Console/Commands/NntmuxPopulateAniDB.php create mode 100644 app/Console/Commands/NntmuxPopulateSteam.php delete mode 100755 cli/data/populate_anidb.php diff --git a/Blacklight/ColorCLI.php b/Blacklight/ColorCLI.php index 5b9b823f0..cfaa578f7 100755 --- a/Blacklight/ColorCLI.php +++ b/Blacklight/ColorCLI.php @@ -19,6 +19,11 @@ class ColorCLI $this->climate = new CLImate(); } + /** + * @param string $str + * @param bool $newline + * @return void + */ public function debug(string $str, bool $newline = false): void { if ($newline) { @@ -27,6 +32,11 @@ class ColorCLI $this->climate->lightGray()->out($str); } + /** + * @param string $str + * @param bool $newline + * @return void + */ public function info(string $str, bool $newline = false): void { if ($newline) { @@ -35,6 +45,11 @@ class ColorCLI $this->climate->magenta()->out($str); } + /** + * @param string $str + * @param bool $newline + * @return void + */ public function notice(string $str, bool $newline = false): void { if ($newline) { @@ -43,6 +58,11 @@ class ColorCLI $this->climate->blue()->out($str); } + /** + * @param string $str + * @param bool $newline + * @return void + */ public function warning(string $str, bool $newline = false): void { if ($newline) { @@ -51,6 +71,11 @@ class ColorCLI $this->climate->yellow()->out($str); } + /** + * @param string $str + * @param bool $newline + * @return void + */ public function error(string $str, bool $newline = false): void { if ($newline) { @@ -59,6 +84,11 @@ class ColorCLI $this->climate->red()->out($str); } + /** + * @param string $str + * @param bool $newline + * @return void + */ public function primary(string $str, bool $newline = false): void { if ($newline) { @@ -67,6 +97,11 @@ class ColorCLI $this->climate->green()->out($str); } + /** + * @param string $str + * @param bool $newline + * @return void + */ public function header(string $str, bool $newline = false): void { if ($newline) { @@ -75,6 +110,11 @@ class ColorCLI $this->climate->yellow()->out($str); } + /** + * @param string $str + * @param bool $newline + * @return void + */ public function alternate(string $str, bool $newline = false): void { if ($newline) { @@ -83,6 +123,11 @@ class ColorCLI $this->climate->magenta()->bold()->out($str); } + /** + * @param string $str + * @param bool $newline + * @return void + */ public function tmuxOrange(string $str, bool $newline = false): void { if ($newline) { @@ -91,23 +136,47 @@ class ColorCLI $this->climate->yellow()->bold()->out($str); } + /** + * @param string $str + * @return void + */ public function primaryOver(string $str): void { $this->climate->green()->inline($str); } + /** + * @param string $str + * @return void + */ public function headerOver(string $str): void { $this->climate->yellow()->inline($str); } + /** + * @param string $str + * @return void + */ public function alternateOver(string $str): void { $this->climate->magenta()->bold()->inline($str); } + /** + * @param string $str + * @return void + */ public function warningOver(string $str): void { $this->climate->red()->inline($str); } + + /** + * @return mixed + */ + public function progress() + { + return $this->climate->progress(); + } } diff --git a/Blacklight/Steam.php b/Blacklight/Steam.php index a413797fe..26a06d356 100755 --- a/Blacklight/Steam.php +++ b/Blacklight/Steam.php @@ -5,6 +5,8 @@ namespace Blacklight; use App\Models\Settings; use App\Models\SteamApp; use b3rs3rk\steamfront\Main; +use DivineOmega\CliProgressBar\ProgressBar; +use Illuminate\Support\Arr; /** * Class Steam. @@ -16,24 +18,24 @@ class Steam /** * @var string The parsed game name from searchname */ - public $searchTerm; + public string $searchTerm; /** * @var int The ID of the Steam Game matched */ - protected $steamGameID; + protected int $steamGameID; protected $lastUpdate; /** - * @var \b3rs3rk\steamfront\Main + * @var Main */ - protected $steamFront; + protected Main $steamFront; /** - * @var \Blacklight\ColorCLI + * @var ColorCLI */ - protected $colorCli; + protected ColorCLI $colorCli; /** * Steam constructor. @@ -59,15 +61,15 @@ class Steam /** * Gets all Information for the game. * - * @param int $appID + * @param int $appID * @return array|false */ - public function getAll($appID) + public function getAll(int $appID): bool|array { $res = $this->steamFront->getAppDetails($appID); if ($res !== false) { - $result = [ + return [ 'title' => $res->name, 'description' => $res->description['short'] ?? null, 'cover' => $res->images['header'] ?? null, @@ -79,13 +81,9 @@ class Steam 'releasedate' => $res->releasedate['date'] ?? null, 'genres' => $res->genres !== null ? implode(',', array_column($res->genres, 'description')) : '', ]; - - return $result; } - if ($res === false) { - $this->colorCli->notice('Steam did not return game data'); - } + $this->colorCli->notice('Steam did not return game data'); return false; } @@ -93,19 +91,19 @@ class Steam /** * Searches Steam Apps table for best title match -- prefers 100% match but returns highest over 90%. * - * @param string $searchTerm The parsed game name from the release searchname + * @param string $searchTerm The parsed game name from the release searchname * @return false|int $bestMatch The Best match from the given search term * * @throws \Exception */ - public function search($searchTerm) + public function search(string $searchTerm): bool|int { $bestMatch = false; if (empty($searchTerm)) { $this->colorCli->notice('Search term cannot be empty'); - return $bestMatch; + return false; } $this->populateSteamAppsTable(); @@ -147,6 +145,7 @@ class Steam */ public function populateSteamAppsTable(): void { + $bar = new ProgressBar(); $lastUpdate = Settings::settingValue('APIs.Steam.last_update'); $this->lastUpdate = $lastUpdate > 0 ? $lastUpdate : 0; if ((time() - (int) $this->lastUpdate) > 86400) { @@ -155,25 +154,24 @@ class Steam $fullAppArray = $this->steamFront->getFullAppList(); $inserted = $dupe = 0; $this->colorCli->info('Populating steam apps table'); - foreach ($fullAppArray as $appsArray) { - foreach ($appsArray as $appArray) { - foreach ($appArray as $app) { - $dupeCheck = SteamApp::query()->where('appid', '=', $app['appid'])->first(['appid']); - - if ($dupeCheck === null) { - SteamApp::query()->insert(['name' => $app['name'], 'appid' => $app['appid']]); - $inserted++; - if ($inserted % 500 === 0) { - echo PHP_EOL.number_format($inserted).' apps inserted.'.PHP_EOL; - } else { - echo '.'; - } - } else { - $dupe++; - } + $appsArray = Arr::pluck($fullAppArray, 'apps'); + $max = count($appsArray[0]); + $bar->setMaxProgress($max); + foreach ($appsArray as $appArray) { + foreach ($appArray as $app) { + $dupeCheck = SteamApp::query()->where('appid', '=', $app['appid'])->first(['appid']); + if ($dupeCheck === null) { + SteamApp::query()->insert(['name' => $app['name'], 'appid' => $app['appid']]); + $inserted++; + } else { + $dupe++; } + $bar->advance()->display(); } } + + $bar->complete(); + echo PHP_EOL.'Added '.$inserted.' new steam app(s), '.$dupe.' duplicates skipped'.PHP_EOL; } } diff --git a/app/Console/Commands/NntmuxPopulateAniDB.php b/app/Console/Commands/NntmuxPopulateAniDB.php new file mode 100644 index 000000000..a76ab5ab6 --- /dev/null +++ b/app/Console/Commands/NntmuxPopulateAniDB.php @@ -0,0 +1,41 @@ +option('full')) { + (new AniDB(['Echo' => true]))->populateTable('full'); + } elseif ($this->option('info') && is_numeric($this->option('anidbid'))) { + (new AniDB(['Echo' => true]))->populateTable('info', $this->option('anidbid')); + } elseif ($this->option('info')){ + (new AniDB(['Echo' => true]))->populateTable('info'); + } + $this->info('AniDB tables populated with requested data'); + } +} diff --git a/app/Console/Commands/NntmuxPopulateSteam.php b/app/Console/Commands/NntmuxPopulateSteam.php new file mode 100644 index 000000000..c6232deff --- /dev/null +++ b/app/Console/Commands/NntmuxPopulateSteam.php @@ -0,0 +1,34 @@ +populateSteamAppsTable(); + + } +} diff --git a/cli/data/populate_anidb.php b/cli/data/populate_anidb.php deleted file mode 100755 index 3e15070f9..000000000 --- a/cli/data/populate_anidb.php +++ /dev/null @@ -1,28 +0,0 @@ - 1 && $argv[1] === 'true' && isset($argv[2])) { - if ($argv[2] === 'full') { - (new AniDB(['Echo' => true]))->populateTable('full'); - } elseif ($argv[2] === 'info') { - if ($argv[3] !== null && is_numeric($argv[3])) { - (new AniDB(['Echo' => true]))->populateTable('info', $argv[3]); - } else { - (new AniDB(['Echo' => true]))->populateTable('info'); - } - } -} else { - (new ColorCLI())->error( - 'To execute this script you must provide a boolean argument.'.PHP_EOL. - 'Argument1: true|false to run this script or not'.PHP_EOL. - 'Argument2: full|info for what type of data to populate.'.PHP_EOL. - 'Argument3 (optional) anidbid to fetch info for'.PHP_EOL. - 'WARNING: Argument "info" without third argument will get you banned from AniDB almost instantly' - ); -} diff --git a/composer.json b/composer.json index 1ff1f4e8c..e8ee26483 100644 --- a/composer.json +++ b/composer.json @@ -56,6 +56,7 @@ "dariusiii/tmdb-laravel": "^6.0", "dariusiii/token": "^5.0", "dborsatto/php-giantbomb": "^2.0", + "divineomega/php-cli-progress-bar": "^2.1", "doctrine/dbal": "^3.0", "fakerphp/faker": "^1.12", "genealabs/laravel-caffeine": "^10.0", diff --git a/composer.lock b/composer.lock index e862ba1e1..7af716251 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f0cbb646147483a8138272a6f050f9d6", + "content-hash": "a5896c29d62f20052346eff6a7a3ffdd", "packages": [ { "name": "aharen/omdbapi", @@ -1323,6 +1323,47 @@ }, "time": "2022-10-27T11:44:00+00:00" }, + { + "name": "divineomega/php-cli-progress-bar", + "version": "v2.1.0", + "source": { + "type": "git", + "url": "https://github.com/DivineOmega/php-cli-progress-bar.git", + "reference": "0c2d8d64d27dce8c2f214260c1e28f9fb4821bdc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/DivineOmega/php-cli-progress-bar/zipball/0c2d8d64d27dce8c2f214260c1e28f9fb4821bdc", + "reference": "0c2d8d64d27dce8c2f214260c1e28f9fb4821bdc", + "shasum": "" + }, + "require": { + "khill/php-duration": "^1.0", + "php": ">=5.6" + }, + "type": "library", + "autoload": { + "psr-4": { + "DivineOmega\\CliProgressBar\\": "./src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-only" + ], + "authors": [ + { + "name": "Jordan Hall", + "email": "jordan@hall05.co.uk" + } + ], + "description": "Progress bar for command line PHP scripts", + "support": { + "issues": "https://github.com/DivineOmega/php-cli-progress-bar/issues", + "source": "https://github.com/DivineOmega/php-cli-progress-bar/tree/master" + }, + "time": "2020-02-06T21:58:50+00:00" + }, { "name": "doctrine/cache", "version": "2.2.0", @@ -3677,6 +3718,68 @@ }, "time": "2018-05-30T08:34:23+00:00" }, + { + "name": "khill/php-duration", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/kevinkhill/php-duration.git", + "reference": "dcb222fd0c9eb459676c9eef6fb9f9d2baf749b1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/kevinkhill/php-duration/zipball/dcb222fd0c9eb459676c9eef6fb9f9d2baf749b1", + "reference": "dcb222fd0c9eb459676c9eef6fb9f9d2baf749b1", + "shasum": "" + }, + "require": { + "php": ">=5.4" + }, + "require-dev": { + "doctrine/instantiator": "1.0.4", + "phpstan/phpstan": "^0.12.81", + "phpunit/phpunit": "~4.8|~5.0", + "satooshi/php-coveralls": "~1.0", + "symfony/config": "~2.0", + "symfony/console": "~2.0", + "symfony/filesystem": "~2.0", + "symfony/stopwatch": "~2.0", + "symfony/yaml": "~2.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Khill\\Duration\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kevin Hill", + "email": "kevinkhill@gmail.com", + "role": "Creator" + }, + { + "name": "Duration Community", + "homepage": "https://github.com/kevinkhill/php-duration/contributors" + } + ], + "description": "Converts between colon formatted time, human-readable time and seconds", + "keywords": [ + "convert", + "duration", + "seconds", + "time" + ], + "support": { + "issues": "https://github.com/kevinkhill/php-duration/issues", + "source": "https://github.com/kevinkhill/php-duration/tree/1.1.0" + }, + "time": "2021-03-17T11:34:55+00:00" + }, { "name": "laravel/framework", "version": "v10.4.1",