. */ 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. */ public function saveGame(Game $game) { $game->source = self::class; $dbmodel = NULL; if (isset($game->source_id)) { $dbmodel = Game::where('source', $game->source) ->where('source_id', $game->source_id) ->first(); } if ($dbmodel) { $dbmodel->fill($game); $dbmodel->save(); } else { $game->save(); } } /** * Get the date of the last scraped game for this source. */ protected function getLastDate() { Game::where('source', self::class) ->orderBy('created_at', 'desc') ->limit(1) ->value('created_at'); } }