. */ namespace App; use \Symfony\Component\DomCrawler\Crawler; use App\Models\Game; use Log; use App\Downloader; abstract class Source { // Title public $title; // Optional warning or note public $warning = FALSE; protected $dom; protected $downloader; /** * Should be load the page before the parsing or during * * @var boolean */ public $delayedLoad = false; public function loadStr($html) { $this->dom = new Crawler($html); } abstract public function parse(); /** * System function to download page HTML. * * @return string */ public function get_text($url, $post = []) { if (empty($this->downloader)) { $this->downloader = new Downloader(); } return $this->downloader->get_text($url, $post); } /** * GET JSON data. */ public function get_json($url) { if (empty($this->downloader)) { $this->downloader = new Downloader(); } return $this->downloader->get_json($url); } public function set_cookies($cookies) { return $this->downloader->setCookies($cookies); } /** * Check if URL corresponds to this source. * * @return boolean */ public function checkPage($url) { return false; } /** * Find if we already have the game model. */ public function findGame(Game $game): Game { $game->source = (new \ReflectionClass($this))->getShortName(); $dbmodel = NULL; if (isset($game->source_id)) { $dbmodel = Game::where('source', $game->source) ->where('source_id', $game->source_id) ->first(); } if (is_null($dbmodel) && isset($game->url)) { $dbmodel = Game::where('source', $game->source) ->where('url', $game->url) ->first(); } if (is_null($dbmodel) && isset($game->title)) { $dbmodel = Game::where('source', $game->source) ->where('title', $game->title) ->first(); } if ($dbmodel) { return $dbmodel; } return $game; } /** * Get the date of the last scraped game for this source. */ protected function getLastDate() { $date = Game::where('source', self::class) ->orderBy('created_at', 'desc') ->limit(1) ->value('release_date'); if (!$date) { return NULL; } return (new \DateTime($date)); } }