. */ namespace App\Sources; use \App\Models\Game; use \App\Models\Platform; use \App\Models\Language; use \App\Models\Author; use \App\Models\Tag; use \App\Source; use Log; class Hyperbook extends Source { public $title = "Гиперкнига"; public $keyword = 'hyperbook'; protected $platform = 'AXMA Story Maker'; 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); unset($text); $model = Platform::where('title', $this->platform)->first(); if (!$model) { $model = new Platform(); $model->title = $this->platform; $model->save(); } $this->platform_model = $model; $this->set_language(); $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; $urls[$id] = $url; }); foreach ($urls as $id => $url) { $this->page($url, $id); } } public function checkPage($url) { return (strpos($url,$this->rootUrl.'/comments.php') !== FALSE); } public function page($url, $id) { $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; } $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;') return true; return false; }); if ($date->count() > 0){ $date = $date->first()->text(); if (!empty($date)) { $game->release_date = \DateTime::createFromFormat('d.m.y', $date); } } $description = $this->dom->filter(".content div.small")->reduce(function($node) { if ($node->attr('style') === NULL) return true; return false; }); if ($description->count() > 0) { $game->description = $description->first()->text(); } $game->save(); $author = $this->dom->filter(".content > div")->reduce(function($node) { if ($node->attr('style') === 'float: left; width: 50%; margin-bottom:14px; text-align: left;') { return true; } return false; }); if ($author->count() > 0) { $author = $author->first()->text(); } else { $author = ''; } if (!empty($author)) { $author_model = Author::findByName($author); if (empty($author_model)) { $author_model = new Author(); $author_model->name = $author; $author_model->save(); } $game->authors()->sync([$author_model->id]); } $game->platforms()->sync([$this->platform_model->id]); $game->languages()->sync([$this->language_model->id]); } }