From 027ab478ec8ac10db323323e7b1e9ccc557a2066 Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Fri, 10 Apr 2020 10:35:55 +0700 Subject: [PATCH] Print the list of games --- app/Commands/Collect.php | 18 +++++------ app/Commands/Freshlist.php | 62 ++++++++++++++++++++++++++++++++++++++ app/Downloader.php | 44 +++++++++++++++------------ app/Models/Game.php | 41 +++++++++++++++++-------- 4 files changed, 125 insertions(+), 40 deletions(-) create mode 100644 app/Commands/Freshlist.php diff --git a/app/Commands/Collect.php b/app/Commands/Collect.php index e33061d..33e8926 100644 --- a/app/Commands/Collect.php +++ b/app/Commands/Collect.php @@ -28,15 +28,15 @@ class Collect extends Command * @var array */ protected $parsers = [ - //'Anivisual', - //'Hyperbook', - //'HyperbookEn', - //'Apero', - //'Questbook', - //'Axma', - //'IFDB', - //'Itch', - //'Instead', + 'Anivisual', + 'Hyperbook', + 'HyperbookEn', + 'Apero', + 'Questbook', + 'Axma', + 'IFDB', + 'Itch', + 'Instead', 'Steam', /* 'Urq', diff --git a/app/Commands/Freshlist.php b/app/Commands/Freshlist.php new file mode 100644 index 0000000..e94f7c7 --- /dev/null +++ b/app/Commands/Freshlist.php @@ -0,0 +1,62 @@ +where('release_date', '<=', $now) + ->where('release_date', '>', $week_ago) + ->orderBy('source') + ->get(); + echo ''; + } +} diff --git a/app/Downloader.php b/app/Downloader.php index 9481ecb..e18df5d 100644 --- a/app/Downloader.php +++ b/app/Downloader.php @@ -20,6 +20,7 @@ namespace App; use Illuminate\Support\Facades\Cache; use \GuzzleHttp\Client as GuzzleClient; +use Log; class Downloader { /** @@ -37,32 +38,37 @@ class Downloader { if (env('DEBUG') && Cache::has($url)) { return Cache::get($url); } - if ($post === []) { - $response = $this->client->request('GET', $url, [ - 'cookies' => $this->cookies, - ]); - } else { - $response = $this->client->request('POST', $url, [ - 'form_params' => $post, - 'cookies' => $this->cookies, - ]); + try { + if ($post === []) { + $response = $this->client->request('GET', $url, [ + 'cookies' => $this->cookies, + ]); + } else { + $response = $this->client->request('POST', $url, [ + 'form_params' => $post, + 'cookies' => $this->cookies, + ]); + } + } catch (\Exception $e) { + Log::warning($e->getMessage()); + return ''; } $resp = (string) $response->getBody(); Cache::put($url, $resp); return $resp; } - public function download($url, $outFile) { - $options = array( - CURLOPT_FILE => fopen($outFile, 'w'), - CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files - CURLOPT_URL => $url - ); + public function download($url, $outFile) { + $options = array( + CURLOPT_FILE => fopen($outFile, 'w'), + CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files + CURLOPT_URL => $url + ); - $ch = curl_init(); - curl_setopt_array($ch, $options); - curl_exec($ch); - curl_close($ch); + $ch = curl_init(); + curl_setopt_array($ch, $options); + curl_exec($ch); + curl_close($ch); } public function setCookies($cookies): void { diff --git a/app/Models/Game.php b/app/Models/Game.php index a94b45d..ec18dc1 100644 --- a/app/Models/Game.php +++ b/app/Models/Game.php @@ -7,25 +7,42 @@ use App\Models\Author; use App\Models\Language; use App\Models\Tag; use App\Models\Platform; +use App\Events\GameSaving; class Game extends Model { protected $table = 'games'; public $guarded = ['id']; - public function authors() { - return $this->belongsToMany(Author::class, 'authors_games'); - } + /** + * The "booted" method of the model. + * + * @return void + */ + protected static function booted() + { + static::saving(function ($game) { + // Replace non-breaking space with regular one + $game->title = str_replace(' ', ' ', $game->title); + $game->title = trim($game->title); + $game->description = str_replace(' ', ' ', $game->description); + $game->description = trim($game->description); + }); + } - public function languages() { - return $this->belongsToMany(Language::class, 'languages_games'); - } + public function authors() { + return $this->belongsToMany(Author::class, 'authors_games'); + } - public function tags() { - return $this->belongsToMany(Tag::class, 'tags_games'); - } + public function languages() { + return $this->belongsToMany(Language::class, 'languages_games'); + } - public function platforms() { - return $this->belongsToMany(Platform::class, 'games_platforms', 'game_id', 'platform_id'); - } + public function tags() { + return $this->belongsToMany(Tag::class, 'tags_games'); + } + + public function platforms() { + return $this->belongsToMany(Platform::class, 'games_platforms', 'game_id', 'platform_id'); + } }