123 lines
2.9 KiB
PHP
123 lines
2.9 KiB
PHP
<?php
|
|
/*
|
|
A set of utilities for tracking text-based game releases
|
|
Copyright (C) 2017-2018 Alexander Yakovlev
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
namespace App;
|
|
|
|
use \Symfony\Component\DomCrawler\Crawler;
|
|
use \GuzzleHttp\Client as GuzzleClient;
|
|
use App\Models\Game;
|
|
use Log;
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 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 ($dbmodel) {
|
|
$dbmodel->fill($game->toArray());
|
|
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));
|
|
}
|
|
}
|