100 lines
2.1 KiB
PHP
100 lines
2.1 KiB
PHP
<?php
|
|
namespace Source;
|
|
|
|
use \Symfony\Component\DomCrawler\Crawler;
|
|
use \Game;
|
|
use \GuzzleHttp\Client as GuzzleClient;
|
|
|
|
abstract class Source {
|
|
public $title;
|
|
protected $dom;
|
|
protected $period;
|
|
protected $output;
|
|
protected $cookies = '';
|
|
public function __construct() {
|
|
$this->period = strtotime("1 week ago");
|
|
$this->output = '';
|
|
}
|
|
|
|
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<spoiler title='".$this->title."'><h4>".$this->title."</h4>\n<ul>";
|
|
}
|
|
if ($return) {
|
|
return $text;
|
|
}
|
|
$this->output .= $text;
|
|
}
|
|
|
|
protected function endSection() {
|
|
if (FORMAT === 'HTML') {
|
|
$text = "</ul></spoiler>\n";
|
|
}
|
|
$this->output .= $text;
|
|
}
|
|
|
|
abstract protected function parse();
|
|
|
|
/**
|
|
* System function to download page HTML.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_text($url) {
|
|
$client = new GuzzleClient([
|
|
'timeout' => 30,
|
|
]);
|
|
$response = $client->request('GET', $url, [
|
|
'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;
|
|
}
|
|
}
|