. */ namespace Source; use \Symfony\Component\DomCrawler\Crawler; use \Game; use \GuzzleHttp\Client as GuzzleClient; abstract class Source { // Title public $title; // Optional warning or note public $warning = FALSE; protected $dom; protected $period; protected $output; protected $cookies = ''; public function __construct() { $this->period = strtotime("1 week ago"); $this->output = ''; } /** * Should be load the page before the parsing or during * * @var boolean */ public $delayedLoad = false; public function loadStr($html) { $this->dom = new Crawler($html); } /** * Function to start the section. * @param whether to return or print the text * * @return string|void */ protected function startSection($return = false) { if (FORMAT === 'MARKDOWN') { $text = "\n#### ".$this->title."\n"; } if (FORMAT === 'HTML') { $text = "\n

".$this->title."

"; if (!empty($this->warning)) { $text .= ''.$this->warning.''; } $text .= "
\n"; } $this->output .= $text; } abstract protected 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(); } /** * Print the game line and catch all exceptions. * * @return void */ public function check() { try { $this->startSection(); try { $this->parse(); } catch (\Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; echo $e->getTraceAsString(); return; } $this->endSection(); if ($this->output === $this->startSection(true)) // nothing to print return; echo $this->output; } catch (Exception $e) { echo $e->getMessage(); echo $e->getTraceAsString(); } } /** * Check if URL corresponds to this source. * * @return boolean */ function checkPage($url) { return false; } }