. */ namespace App\Sources; use \App\Models\Game; use \App\Models\Platform; use \App\Models\Language; use \App\Models\Author; use \App\Source; use Log; class Ifiction extends Source { public $title = "ifiction.ru"; public $keyword = 'ifiction'; protected $baseUrl = 'https://forum.ifiction.ru/'; protected $platform; protected $language_model; public function parse() { $this->language_model = Language::findByCode('ru'); $this->page('https://forum.ifiction.ru/viewtopic.php?id=2424&lid=2'); $text = $this->get_text('https://forum.ifiction.ru/viewforum.php?id=36'); $this->loadStr($text); unset($text); $this->dom->filter('.box > .inbox > .GameCatalog > tbody > tr')->each(function($gameBlock) { $href = $gameBlock->filter('.intd .tclcon a:first-child')->first(); if ($href) { $href = $href->attr('href'); $href = str_replace('./', '', $href); } else { return; } $url = $this->baseUrl.trim($href); if (!$this->checkPage($url)) { return; } $date = NULL; if ($gameBlock->filter('td:nth-child(3) > small.dark')->count() > 0) { $datestring = $gameBlock->filter('td:nth-child(3) > small.dark')->text(); if ($datestring === 'Сегодня') { $datestring = date('d.m.Y'); } if ($datestring === 'Вчера') { $datestring = date('d.m.Y', strtotime('-1 day')); } $date = new \DateTime($datestring); } if ($this->checkPage($url)) { $this->page($url, $date); } }); } public function checkPage($url) { $is_ifiction = (strpos($url,'https://forum.ifiction.ru/viewtopic.php?id=') !== FALSE); $is_list = (strpos($url, '&list=') !== FALSE); return $is_ifiction && !$is_list; } public function page($url, $date = NULL) { $text = $this->get_text($url); $this->loadStr($text); unset($text); $game = new Game; $game->url = $url; if (empty($date)) { $game->release_date = new \DateTime(); } else { $game->release_date = $date; } $game->url_discussion = $url; $temp = $this->dom->filter('div.gamecontainer div div.postmsg h1 a b span.light'); if ($temp->count() > 0) { $game->title = trim($temp->first()->text()); } $game = $this->findGame($game); $temp = $this->dom->filter('div.gamecontainer > div > div.postmsg > div'); if ($temp->count() > 0) { $game->description = trim($temp->eq(1)->html()); } $this->dom->filter('div.gamecontainer div div.postmsg table tbody tr td ul li a')->each(function($link) use(&$game) { if ($link->text() === 'Играть онлайн') { $game->url_play_online = $this->baseUrl.$link->attr('href'); $game->url_online_description = 'Играть онлайн'; } if ($link->text() === 'Файл игры') { $game->url_download = $this->baseUrl.$link->attr('href'); } }); $temp = $this->dom->filter('div.gamecontainer > div > div.postmsg > div#game_authors > a'); $author = NULL; $platform = NULL; if ($temp->count() > 0) { $author = trim($temp->first()->text()); if ($temp->count() > 1) { $platform = trim($temp->eq(2)->text()); $platform = Platform::findByName($platform); } } $image = $this->dom->filter('div.gamecontainer > div > div.postmsg > table tr > td > div > a')->first(); if ($image->count() > 0) { $game->image_url = $this->baseUrl.$image->attr('href'); } $game->save(); if (isset($author) && !empty($author)) { $author_model = Author::findByName($author); if (empty($author_model)) { $author_model = new Author(); $author_model->name = $author; $author_model->save(); } $game->authors()->syncWithoutDetaching([$author_model->id]); } $game->languages()->sync([$this->language_model->id]); if ($platform) { $game->platforms()->sync([$platform->id]); } } }