. */ namespace App\Sources; use \App\Models\Game; use \App\Models\Author; use \App\Models\Language; use \App\Source; use \Symfony\Component\DomCrawler\Crawler; class Textadventures extends Source { public $title = "Textadventures.co.uk"; public $keyword = 'textadventures'; public $baseUrl = 'https://textadventures.co.uk'; public function parse() { $text = $this->get_text($this->baseUrl.'/games/latest'); $this->loadStr($text); $language = Language::findByCode('en'); unset($text); $this->dom->filter('.games-item')->each(function($gameBlock) use($language){ $game = new Game; $url = (string) $gameBlock->filter('.games-title a')->attr('href'); $game->url = $this->baseUrl.$url; $temp = str_replace('/games/view/', '', $url); $temp = explode('/', $temp); $id = $temp[0]; unset($temp); $game->source_id = $id; $game->url_discussion = $game->url; $game->url_download = $this->baseUrl.'/games/download/'.$id; $game->url_play_online = $this->baseUrl.'/games/play/'.$id; $game->title = $gameBlock->filter('.games-title a')->text(); $game = $this->findGame($game); $date = $gameBlock->filter('.games-date')->text(); $game->release_date = new \DateTime($date); $text = $this->get_text($game->url); $game_page = new Crawler($text); $model = NULL; if ($game_page->filter('h1 > small')->count() > 0) { $author = $game_page->filter('h1 > small')->text(); $author = str_replace('by ', '', $author); $model = Author::findByName($author); if (!$model) { $model = new Author; $model->name = $author; $model->is_person = true; $author_url = ''; $game_page->filter('.col-md-3 a')->each(function($link) use(&$author_url) { $matches = []; if (preg_match('/\/user\/view\/(\S+)\/(\S+)/', $link->attr('href'), $matches)) { $author_url = $this->baseUrl.$link->attr('href'); // $author_id = $matches[1] ?? ''; } }); if (!empty($author_url)) { $model->url = $author_url; } $model->save(); } } unset($text); if ($game_page->filter('body > div:nth-child(5) > div:nth-child(1) > div > div.container > div > div.col-md-9 > div.row > div.col-md-7')->count()) { $game->description = trim($game_page->filter('body > div:nth-child(5) > div:nth-child(1) > div > div.container > div > div.col-md-9 > div.row > div.col-md-7')->first()->text()); $game->description = preg_replace('/ Play online$/', '', $game->description); } $image = $game_page->filter('img.cover-image')->first(); if ($image->count()) { $game->image_url = $image->attr('src'); } $game->save(); $game->languages()->attach($language); if (isset($author)) { $game->authors()->attach($model); } }); } }