Tags and platforms
This commit is contained in:
parent
8031fb239f
commit
4dba8c0223
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ use App\Models\Game;
|
|||
|
||||
class Tag extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
public function language() {
|
||||
return $this->belongsTo(Language::class);
|
||||
}
|
||||
|
|
|
@ -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, '<b>Автор:');
|
||||
$sidebar_search = trim(mb_substr($sidebar, $pos_start));
|
||||
$pos_end = mb_strpos($sidebar_search, '<br>');
|
||||
$sidebar_search = trim(mb_substr($sidebar_search, 0, $pos_end));
|
||||
$sidebar_search = str_replace('<b>Автор:</b>', '', $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, '<b>Перевод:');
|
||||
$sidebar_search = trim(mb_substr($sidebar, $pos_start));
|
||||
$pos_end = mb_strpos($sidebar_search, '<br>');
|
||||
$sidebar_search = trim(mb_substr($sidebar_search, 0, $pos_end));
|
||||
$sidebar_search = str_replace('<b>Перевод:</b>', '', $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, '<b>'.$value.':');
|
||||
$sidebar_search = trim(mb_substr($sidebar, $pos_start));
|
||||
$pos_end = mb_strpos($sidebar_search, '<br>');
|
||||
$sidebar_search = trim(mb_substr($sidebar_search, 0, $pos_end));
|
||||
$sidebar_search = str_replace('<b>'.$value.':</b>', '', $sidebar_search);
|
||||
$text = trim(strip_tags($sidebar_search));
|
||||
$url = $this->getLink($sidebar_search);
|
||||
return [$text, $url];
|
||||
}
|
||||
}
|
||||
|
|
42
database/migrations/2019_11_30_062149_platforms.php
Normal file
42
database/migrations/2019_11_30_062149_platforms.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class Platforms extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('games_platforms', function (Blueprint $table) {
|
||||
$table->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');
|
||||
});
|
||||
}
|
||||
}
|
Reference in a new issue