[WorldOfTanksBridge] code factorization

Signed-off-by: Pierre Mazière <pierre.maziere@gmx.com>
This commit is contained in:
Pierre Mazière 2016-08-25 17:15:52 +02:00
parent b6fe424ddb
commit 23a7edebd6

View file

@ -1,11 +1,6 @@
<?php <?php
define('WORLD_OF_TANKS', 'http://worldoftanks.eu/');
define('NEWS', '/news/');
class WorldOfTanksBridge extends HttpCachingBridgeAbstract{ class WorldOfTanksBridge extends HttpCachingBridgeAbstract{
private $lang = "fr";
public $uri = WORLD_OF_TANKS;
public function loadMetadatas() { public function loadMetadatas() {
$this->maintainer = "mitsukarenai"; $this->maintainer = "mitsukarenai";
@ -14,10 +9,10 @@ class WorldOfTanksBridge extends HttpCachingBridgeAbstract{
$this->description = "News about the tank slaughter game."; $this->description = "News about the tank slaughter game.";
$this->parameters[] = array( $this->parameters[] = array(
'category'=>array( 'category'=>array(
'name'=>'ID de la catégorie', // TODO: should be a list
'type'=>'number' 'name'=>'nom de la catégorie'
), ),
'lang'=>array( 'lang'=>array(
'name'=>'Langue', 'name'=>'Langue',
'type'=>'list', 'type'=>'list',
@ -34,17 +29,21 @@ class WorldOfTanksBridge extends HttpCachingBridgeAbstract{
); );
} }
function getURI(){
$param=$this->parameters[$this->queriedContext];
$lang='fr';
if (!empty($param['lang']['value'])) {
$lang = $param['lang']['value'];
}
$uri = $this->uri.$lang.'/news/';
if(!empty($param['category']['value'])) {
$uri .= 'pc-browser/'.$param['category']['value']."/";
}
return $uri;
}
public function collectData(){ public function collectData(){
$param=$this->parameters[$this->queriedContext];
if (!empty($param['lang']['value'])) {
$this->lang = $param['lang']['value'];
}
if(empty($param['category']['value'])) {
$this->uri = WORLD_OF_TANKS.$this->lang.NEWS;
} else {
$this->uri = WORLD_OF_TANKS.$this->lang.NEWS.'pc-browser/'.$param['category']['value']."/";
}
$html = $this->getSimpleHTMLDOM($this->getURI()) or $this->returnServerError('Could not request '.$this->getURI()); $html = $this->getSimpleHTMLDOM($this->getURI()) or $this->returnServerError('Could not request '.$this->getURI());
$this->debugMessage("loaded HTML from ".$this->getURI()); $this->debugMessage("loaded HTML from ".$this->getURI());
// customize name // customize name
@ -56,12 +55,12 @@ class WorldOfTanksBridge extends HttpCachingBridgeAbstract{
private function parseLine($infoLink) { private function parseLine($infoLink) {
$item = array(); $item = array();
$item['uri'] = WORLD_OF_TANKS.$infoLink->href; $item['uri'] = $this->uri.$infoLink->href;
// now load that uri from cache // now load that uri from cache
$this->debugMessage("loading page ".$item['uri']); $this->debugMessage("loading page ".$item['uri']);
$articlePage = str_get_html($this->get_cached($item['uri'])); $articlePage = str_get_html($this->get_cached($item['uri']));
$content = $articlePage->find('.l-content', 0); $content = $articlePage->find('.l-content', 0);
HTMLSanitizer::defaultImageSrcTo($content, WORLD_OF_TANKS); HTMLSanitizer::defaultImageSrcTo($content, $this->uri);
$item['title'] = $content->find('h1', 0)->innertext; $item['title'] = $content->find('h1', 0)->innertext;
$item['content'] = $content->find('.b-content', 0)->innertext; $item['content'] = $content->find('.b-content', 0)->innertext;
$item['timestamp'] = $content->find('.b-statistic_time', 0)->getAttribute("data-timestamp"); $item['timestamp'] = $content->find('.b-statistic_time', 0)->getAttribute("data-timestamp");