. */ 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; class Anivisual extends Source { public $title = "Anivisual"; public $keyword = 'anivisual'; protected $months = [ 'Января' => 'January', 'Февраля' => 'February', 'Марта' => 'March', 'Апреля' => 'April', 'Мая' => 'May', 'Июня' => 'June', 'Июля' => 'July', 'Августа' => 'August', 'Сентября' => 'September', 'Октября' => 'October', 'Ноября' => 'November', 'Декабря' => 'December', ]; public function parse() { // Parsing first 5 pages for ($i = 1; $i <= 5; $i++) { $this->parseIndex($i); } } protected function parseIndex($i) { $text = $this->get_text('http://anivisual.net/stuff/1-'.$i); $this->loadStr($text); unset($text); $this->dom->filter('#allEntries .entryBlock')->each(function($gameBlock) { // Get the game link $link = $gameBlock->filter('.novel-tmb a')->first(); $link = 'http://anivisual.net'.$link->attr('href'); $this->page($link); }); } public function checkPage($url) { return (strpos($url,'://anivisual.net/stuff/') !== FALSE); } public function page($url) { $text = $this->get_text($url); $this->loadStr($text); unset($text); $game = new Game(); $game->url = $url; $game->source_id = str_replace('http://anivisual.net/stuff/', '', $url); $game = $this->findGame($game); if ($game->isClean()) { return; } $gameBlock = $this->dom->filter('#casing-box'); $dateBlock = $this->dom->filter('.post-dts .icon-calendar'); $date = ''; if ($dateBlock->count() > 0) { $date = trim($dateBlock->first()->text()); if (!empty($date)) { $release_date = \DateTime::createFromFormat('d.m.Y', $date); if (!empty($release_date)) { $game->release_date = $release_date; } unset($date); } } $title = $this->dom->filter('h1.logo')->first(); if ($title->count() > 0) { $game->title = trim(htmlspecialchars_decode($title->text())); } $game->description = $this->dom->filter('#content > section > span')->first()->text(); $game->description = str_replace('(adsbygoogle = window.adsbygoogle || []).push({});', '', $game->description); $game->description = str_replace('Доп. ссылки: Доступно только для пользователей', '', $game->description); $game->description = trim($game->description); $game->save(); $language = Language::findByCode('ru'); if (!$game->languages()->where('code', 'ru')->exists()) { $game->languages()->syncWithoutDetaching([$language->id]); } $sidebar = $gameBlock->filter('#sidebar')->first()->html(); [$author, $author_url] = $this->getValue($sidebar, 'Автор'); if (!empty($author)) { $author_model = Author::findByName($author); if (empty($author_model)) { $author_model = new Author(); $author_model->name = $author; $author_model->url = $author_url; } $author = $author_model; unset($author_model); } if ($author instanceof Author) { $author->save(); if (!$game->authors()->where('authors.id', $author->id)->exists()) { $game->authors()->attach($author); } } [$sidebar_search, $author_url] = $this->getValue($sidebar, 'Перевод'); if ($sidebar_search !== '') { $language = Language::findByCode('en'); if (!$game->languages()->where('languages.id', $language->id)->exists()) { $game->languages()->attach($language->id); } $author = Author::findByName($sidebar_search); if (!$author) { $author = new Author(); $author->name = $sidebar_search; $author->is_translator = true; $author->url = $author_url; $author->save(); } if (!$game->authors()->where('authors.id', $author->id)->exists()) { $game->authors()->attach($author); } } [$platforms, ] = $this->getValue($sidebar, 'Платформа'); $platforms = explode(',', $platforms); $platforms = array_map('trim', $platforms); $platform_ids = []; foreach ($platforms as $platform) { $model = Platform::where('title', $platform)->first(); if (!$model) { $model = new Platform(); $model->title = $platform; $model->save(); } $platform_ids[] = $model->id; } $game->platforms()->sync($platform_ids); [$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'); if (!$game->languages()->where('languages.id', $language->id)->exists()) { $game->languages()->attach($language); } $tag_ids = []; 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(); } $tag_ids[] = $model->id; } $game->tags()->sync($tag_ids); } protected function getLink($html) { if (empty($html)) { return ''; } $author_dom = new Crawler($html); $author_url = ''; if ($author_dom->filter('a')->count() === 0) { return ''; } $link = $author_dom->filter('a')->first(); if (!empty($link)) { $author_url = $link->attr('href'); if (!empty($author_url)) { $author_url = str_replace('/go?', '', $author_url); } } 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]; } }