From afade9386df517d5fdc8279630713f92cad12a03 Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Wed, 21 Oct 2020 13:45:37 +0700 Subject: [PATCH] DashingDon parser --- app/Commands/Collect.php | 2 +- app/Models/Author.php | 2 +- app/Models/Game.php | 10 ++++++++++ app/Models/Platform.php | 2 +- app/Models/Tag.php | 4 +++- app/Sources/Dashingdon.php | 27 ++++++++++++++++++++++++++- app/Sources/Gamejolt.php | 1 + 7 files changed, 43 insertions(+), 5 deletions(-) diff --git a/app/Commands/Collect.php b/app/Commands/Collect.php index 9b65ac0..cc0c0dd 100644 --- a/app/Commands/Collect.php +++ b/app/Commands/Collect.php @@ -44,9 +44,9 @@ class Collect extends Command 'Ifiction', 'Gamejolt', 'Textadventures', + 'Dashingdon', /* 'vndb', - 'Dashingdon', */ ]; diff --git a/app/Models/Author.php b/app/Models/Author.php index ac88068..cfcefd6 100644 --- a/app/Models/Author.php +++ b/app/Models/Author.php @@ -15,6 +15,6 @@ class Author extends Model } public static function findByName($name) { - return self::where('name', $name)->first(); + return self::whereRaw('LOWER(name) = ?', mb_strtolower($name))->first(); } } diff --git a/app/Models/Game.php b/app/Models/Game.php index 8a9fabe..fccff6a 100644 --- a/app/Models/Game.php +++ b/app/Models/Game.php @@ -91,4 +91,14 @@ class Game extends Model $out = implode("\n", $lines); return $out; } + + public function guessPlatform() { + foreach($this->tags as $tag) { + $platform = Platform::findByName($tag->title, false); + if ($platform) { + $this->platforms()->attach($platform); + break; + } + } + } } diff --git a/app/Models/Platform.php b/app/Models/Platform.php index 33ae2ed..95add3c 100644 --- a/app/Models/Platform.php +++ b/app/Models/Platform.php @@ -14,7 +14,7 @@ class Platform extends Model } public static function findByName($name, $autocreate = true) { - $model = self::where('title', $name)->first(); + $model = self::whereRaw('LOWER(title) = ?', mb_strtolower($name))->first(); if (empty($model) && $autocreate) { $model = new self; $model->title = $name; diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 8b35a07..5c69dde 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -17,6 +17,8 @@ class Tag extends Model return $this->belongsToMany(Game::class, 'tags_games'); } public static function findByName($name, $language) { - return self::where('title', $name)->where('language_id', $language->id)->first(); + return self::whereRaw('LOWER(title) = ?', mb_strtolower($name)) + ->where('language_id', $language->id) + ->first(); } } diff --git a/app/Sources/Dashingdon.php b/app/Sources/Dashingdon.php index 47989d2..b2cd184 100644 --- a/app/Sources/Dashingdon.php +++ b/app/Sources/Dashingdon.php @@ -18,7 +18,10 @@ */ namespace App\Sources; +use App\Models\Author; use \App\Models\Game; +use App\Models\Language; +use App\Models\Platform; use \App\Source; /** @@ -26,9 +29,31 @@ use \App\Source; */ class Dashingdon extends Source { public $title = "DashingDon"; + public $keyword = 'dashingdon'; public function parse() { - $text = $this->get_text("https://dashingdon.com/screenreader/"); + $platform = Platform::findByName('ChoiceScript'); + $language = Language::findByCode('en'); + $text = $this->get_text("https://dashingdon.com/"); $this->loadStr($text); unset($text); + $this->dom->filter('#allgames > article > fieldset > div > section.col.col-8')->each(function($gameBlock) use($platform, $language) { + $game = new Game(); + $game->title = $gameBlock->filter('div.value-self > a')->text(); + $game = $this->findGame($game); + $game->url = $gameBlock->filter('div.value-self > a')->attr('href'); + $game->url_play_online = $game->url; + $game->description = trim($gameBlock->filter('.blurbbox')->text()); + $author_name = $gameBlock->filter('p > strong')->first()->text(); + $game->save(); + $game->languages()->attach($language); + $game->platforms()->attach($platform); + $author = Author::findByName($author_name); + if (!$author) { + $author = new Author(); + $author->name = $author_name; + $author->save(); + } + $game->authors()->attach($author); + }); } } diff --git a/app/Sources/Gamejolt.php b/app/Sources/Gamejolt.php index dc6469b..d8af402 100644 --- a/app/Sources/Gamejolt.php +++ b/app/Sources/Gamejolt.php @@ -97,6 +97,7 @@ class Gamejolt extends Source { foreach ($tags as $t) { $game->tags()->attach($t); } + $game->guessPlatform(); } } }