From 4dba8c022374f8fa5e409bb221fc5fa7b963c7ba Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Sat, 30 Nov 2019 14:03:43 +0700 Subject: [PATCH] Tags and platforms --- app/Models/Game.php | 4 +- app/Models/Language.php | 4 -- app/Models/Platform.php | 4 +- app/Models/Tag.php | 1 + app/Sources/Anivisual.php | 70 ++++++++++++++----- .../2019_11_30_062149_platforms.php | 42 +++++++++++ 6 files changed, 99 insertions(+), 26 deletions(-) create mode 100644 database/migrations/2019_11_30_062149_platforms.php diff --git a/app/Models/Game.php b/app/Models/Game.php index dd8bcdd..a94b45d 100644 --- a/app/Models/Game.php +++ b/app/Models/Game.php @@ -25,7 +25,7 @@ class Game extends Model return $this->belongsToMany(Tag::class, 'tags_games'); } - public function platform() { - return $this->hasOne(Platform::class); + public function platforms() { + return $this->belongsToMany(Platform::class, 'games_platforms', 'game_id', 'platform_id'); } } diff --git a/app/Models/Language.php b/app/Models/Language.php index da0c791..7fd1b67 100644 --- a/app/Models/Language.php +++ b/app/Models/Language.php @@ -15,10 +15,6 @@ class Language extends Model return $this->belongsToMany(Game::class, 'languages_games'); } - public function tags() { - return $this->hasMany(Tag::class); - } - public static function findByCode($code) { return self::where('code', $code)->first(); } diff --git a/app/Models/Platform.php b/app/Models/Platform.php index 8752b7f..2df40e2 100644 --- a/app/Models/Platform.php +++ b/app/Models/Platform.php @@ -7,7 +7,9 @@ use App\Models\Game; class Platform extends Model { + public $timestamps = false; + public function games() { - return $this->hasMany(Game::class); + return $this->belongsToMany(Game::class, 'games_platforms', 'platform_id'); } } diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 990446e..b05e7c2 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -8,6 +8,7 @@ use App\Models\Game; class Tag extends Model { + public $timestamps = false; public function language() { return $this->belongsTo(Language::class); } diff --git a/app/Sources/Anivisual.php b/app/Sources/Anivisual.php index c663ee8..0fe05e4 100644 --- a/app/Sources/Anivisual.php +++ b/app/Sources/Anivisual.php @@ -21,6 +21,8 @@ namespace App\Sources; use \App\Models\Game; use \App\Models\Language; use \App\Models\Author; +use \App\Models\Platform; +use \App\Models\Tag; use \App\Source; use Symfony\Component\DomCrawler\Crawler; use Log; @@ -106,19 +108,10 @@ class Anivisual extends Source { $language = Language::findByCode('ru'); if (!$game->languages()->where('code', 'ru')->exists()) { $game->languages()->attach($language); - $game->save(); } $sidebar = $gameBlock->filter('#sidebar')->first()->html(); - $pos_start = mb_strpos($sidebar, 'Автор:'); - $sidebar_search = trim(mb_substr($sidebar, $pos_start)); - $pos_end = mb_strpos($sidebar_search, '
'); - $sidebar_search = trim(mb_substr($sidebar_search, 0, $pos_end)); - $sidebar_search = str_replace('Автор:', '', $sidebar_search); - - $author = trim(strip_tags($sidebar_search)); - $author_url = $this->getLink($sidebar_search); - + [$author, $author_url] = $this->getValue($sidebar, 'Автор'); if (!empty($author)) { $author_model = Author::findByName($author); if (empty($author_model)) { @@ -133,17 +126,10 @@ class Anivisual extends Source { $author->save(); if (!$game->authors()->where('name', $author->name)->exists()) { $game->authors()->attach($author); - $game->save(); } } - $pos_start = mb_strpos($sidebar, 'Перевод:'); - $sidebar_search = trim(mb_substr($sidebar, $pos_start)); - $pos_end = mb_strpos($sidebar_search, '
'); - $sidebar_search = trim(mb_substr($sidebar_search, 0, $pos_end)); - $sidebar_search = str_replace('Перевод:', '', $sidebar_search); - $sidebar_search = trim(strip_tags($sidebar_search)); - $author_url = $this->getLink($sidebar_search); + [$sidebar_search, $author_url] = $this->getValue($sidebar, 'Перевод'); if ($sidebar_search !== '') { $language = Language::findByCode('en'); if (!$game->languages()->where('code', 'en')->exists()) { @@ -159,9 +145,44 @@ class Anivisual extends Source { } if (!$game->authors()->where('name', $author->name)->exists()) { $game->authors()->attach($author); - $game->save(); } } + + [$platforms, ] = $this->getValue($sidebar, 'Платформа'); + $platforms = explode(',', $platforms); + $platforms = array_map('trim', $platforms); + foreach ($platforms as $platform) { + $model = Platform::where('title', $platform)->first(); + if (!$model) { + $model = new Platform(); + $model->title = $platform; + $model->save(); + } + $game->platforms()->attach($model); + } + + [$genres, ] = $this->getValue($sidebar, 'Жанры'); + [$tags, ] = $this->getValue($sidebar, 'Теги'); + $tags = explode(',', $tags); + $genres = explode(',', $genres); + $tags = array_merge($tags, $genres); + unset($genres); + $tags = array_map('trim', $tags); + $tags = array_filter($tags); + $language_ru = Language::findByCode('ru'); + foreach ($tags as $tag) { + $model = Tag::where('language_id', $language_ru->id) + ->where('title', $tag) + ->first(); + if (!$model) { + $model = new Tag(); + $model->language_id = $language_ru->id; + $model->title = $tag; + $model->save(); + } + $game->tags()->attach($model); + } + Log::info($game->title); } @@ -183,4 +204,15 @@ class Anivisual extends Source { } return (string) $author_url; } + + protected function getValue($sidebar, $value) { + $pos_start = mb_strpos($sidebar, ''.$value.':'); + $sidebar_search = trim(mb_substr($sidebar, $pos_start)); + $pos_end = mb_strpos($sidebar_search, '
'); + $sidebar_search = trim(mb_substr($sidebar_search, 0, $pos_end)); + $sidebar_search = str_replace(''.$value.':', '', $sidebar_search); + $text = trim(strip_tags($sidebar_search)); + $url = $this->getLink($sidebar_search); + return [$text, $url]; + } } diff --git a/database/migrations/2019_11_30_062149_platforms.php b/database/migrations/2019_11_30_062149_platforms.php new file mode 100644 index 0000000..d695aa0 --- /dev/null +++ b/database/migrations/2019_11_30_062149_platforms.php @@ -0,0 +1,42 @@ +increments('id'); + $table->integer('game_id')->unsigned(); + $table->foreign('game_id')->references('id')->on('games'); + $table->integer('platform_id')->unsigned(); + $table->foreign('platform_id')->references('id')->on('platforms'); + }); + Schema::table('games', function (Blueprint $table) { + $table->dropForeign(['platform_id']); + $table->dropColumn('platform_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('games_platforms'); + Schema::table('games', function (Blueprint $table) { + $table->unsignedInteger('platform_id')->nullable(); + $table->foreign('platform_id')->references('id')->on('platforms'); + }); + } +}