122 lines
2.9 KiB
PHP
122 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 App\Models\Game;
|
|
use Log;
|
|
use App\Downloader;
|
|
|
|
abstract class Source {
|
|
// Title
|
|
public $title;
|
|
// Optional warning or note
|
|
public $warning = FALSE;
|
|
protected $dom;
|
|
protected $downloader;
|
|
/**
|
|
* 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 = []) {
|
|
if (empty($this->downloader)) {
|
|
$this->downloader = new Downloader();
|
|
}
|
|
return $this->downloader->get_text($url, $post);
|
|
}
|
|
|
|
/**
|
|
* GET JSON data.
|
|
*/
|
|
public function get_json($url) {
|
|
if (empty($this->downloader)) {
|
|
$this->downloader = new Downloader();
|
|
}
|
|
return $this->downloader->get_json($url);
|
|
}
|
|
|
|
public function set_cookies($cookies) {
|
|
return $this->downloader->setCookies($cookies);
|
|
}
|
|
|
|
/**
|
|
* 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 (is_null($dbmodel) && isset($game->url)) {
|
|
$dbmodel = Game::where('source', $game->source)
|
|
->where('url', $game->url)
|
|
->first();
|
|
}
|
|
if (is_null($dbmodel) && isset($game->title)) {
|
|
$dbmodel = Game::where('source', $game->source)
|
|
->where('title', $game->title)
|
|
->first();
|
|
}
|
|
if ($dbmodel) {
|
|
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));
|
|
}
|
|
}
|