. */ namespace App; use \Symfony\Component\DomCrawler\Crawler; use \GuzzleHttp\Client as GuzzleClient; use App\Models\Game; abstract class Source { // Title public $title; // Optional warning or note public $warning = FALSE; protected $dom; protected $cookies = ''; /** * 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 = []) { $client = new GuzzleClient([ 'timeout' => 30, ]); if ($post === []) { $response = $client->request('GET', $url, [ 'cookies' => $this->cookies, ]); } else { $response = $client->request('POST', $url, [ 'form_params' => $post, 'cookies' => $this->cookies, ]); } return (string) $response->getBody(); } /** * GET JSON data. */ public function get_json($url) { $client = new GuzzleClient([ 'timeout' => 30, ]); $response = $client->request('GET', $url, [ 'cookies' => $this->cookies, ]); $text = (string) $response->getBody(); return json_decode($text); } /** * Check if URL corresponds to this source. * * @return boolean */ public function checkPage($url) { return false; } /** * Save the game if not a duplicate. */ protected function saveGame(Game $game) { $game->save(); } }