From a18fa4833932c5e734eed2d751ede8203fe69237 Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Sun, 5 Jan 2020 15:10:04 +0700 Subject: [PATCH] Hyperbook + HyperbookEn --- app/Commands/Collect.php | 2 +- app/Sources/Hyperbook.php | 30 ++++++++++++++++++++++-------- app/Sources/HyperbookEn.php | 4 ++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/Commands/Collect.php b/app/Commands/Collect.php index aa8766c..2ebee7f 100644 --- a/app/Commands/Collect.php +++ b/app/Commands/Collect.php @@ -30,6 +30,7 @@ class Collect extends Command protected $parsers = [ //'Anivisual', 'Hyperbook', + 'HyperbookEn', /* 'Apero', 'Questbook', @@ -42,7 +43,6 @@ class Collect extends Command 'vndb', 'Instory', 'instead', - 'HyperbookEn', 'Dashingdon', 'Gamejolt', */ diff --git a/app/Sources/Hyperbook.php b/app/Sources/Hyperbook.php index 29d3909..71a99a5 100644 --- a/app/Sources/Hyperbook.php +++ b/app/Sources/Hyperbook.php @@ -32,6 +32,9 @@ class Hyperbook extends Source { protected $platform_model; protected $language_model; protected $rootUrl = 'http://hyperbook.ru'; + protected function set_language(): void { + $this->language_model = Language::findByCode('ru'); + } public function parse() { $text = $this->get_text($this->rootUrl.'/lib.php?sort=time'); $this->loadStr($text); @@ -43,15 +46,18 @@ class Hyperbook extends Source { $model->save(); } $this->platform_model = $model; + $this->set_language(); - $this->language_model = Language::findByCode('ru'); - - $this->dom->filter("#listPubs h3 a")->each(function($link) { + $urls = []; + $this->dom->filter("#listPubs h3 a")->each(function($link) use (&$urls) { $id = $link->attr('href'); $id = (int) str_replace('file', '', $id); $url = $this->rootUrl.'/comments.php?id='.$id; - $this->page($url, $id); + $urls[$id] = $url; }); + foreach ($urls as $id => $url) { + $this->page($url, $id); + } } public function checkPage($url) { return (strpos($url,$this->rootUrl.'/comments.php') !== FALSE); @@ -60,14 +66,22 @@ class Hyperbook extends Source { $game = new Game; $game->url = $url; $game->source_id = $id; + if (empty($game->source_id)) { + throw new \Exception('no id'); + return; + } $game = $this->findGame($game); if ($game->isClean()) { return; } - $game->title = $this->dom->filter(".content h1")->first()->text(); - $game->title = trim(str_replace($this->dom->filter("h1 span")->first()->text(), '', $game->title)); + $this->loadStr($this->get_text($url)); + $title = $this->dom->filter(".content h1")->first()->text(); + $this->dom->filter(".content h1 span")->each(function($span) use(&$title) { + $title = trim(str_replace($span->text(), '', $title)); + }); + $game->title = $title; $date = $this->dom->filter(".content div.small")->reduce(function($node) { if ($node->attr('style') === 'float: left; width: 20%; text-align:right;') @@ -77,7 +91,7 @@ class Hyperbook extends Source { if ($date->count() > 0){ $date = $date->first()->text(); if (!empty($date)) { - $game->date = \DateTime::createFromFormat('d.m.y', $date); + $game->release_date = \DateTime::createFromFormat('d.m.y', $date); } } @@ -116,7 +130,7 @@ class Hyperbook extends Source { } $game->platforms()->attach($this->platform_model); - if (!$game->languages()->where('code', 'ru')->exists()) { + if (!$game->languages()->where('code', $this->language_model->code)->exists()) { $game->languages()->attach($this->language_model); } diff --git a/app/Sources/HyperbookEn.php b/app/Sources/HyperbookEn.php index c2400c8..5c4ffbc 100644 --- a/app/Sources/HyperbookEn.php +++ b/app/Sources/HyperbookEn.php @@ -19,10 +19,14 @@ namespace App\Sources; use \App\Models\Game; +use \App\Models\Language; use \App\Sources\Hyperbook; use \App\Source; class HyperbookEn extends Hyperbook { public $title = "Гиперкнига"; protected $rootUrl = 'http://ifiction.net'; + protected function set_language(): void { + $this->language_model = Language::findByCode('en'); + } }