Archived
1
0
Fork 0
This repository has been archived on 2021-07-30. You can view files and clone it, but cannot push or open issues or pull requests.
ifnews/app/Source.php
2019-09-13 02:30:03 +07:00

97 lines
2.3 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;
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.
*/
protected function saveGame(Game $game) {
$game->save();
}
}