[PickyWallpapersBridge] add getURI() + code simplification

Signed-off-by: Pierre Mazière <pierre.maziere@gmx.com>
This commit is contained in:
Pierre Mazière 2016-08-29 20:41:15 +02:00
parent 0b4854ccbf
commit 5f0b843453

View file

@ -1,73 +1,73 @@
<?php <?php
class PickyWallpapersBridge extends BridgeAbstract { class PickyWallpapersBridge extends BridgeAbstract {
private $category;
private $subcategory;
private $resolution;
public $maintainer = "nel50n"; public $maintainer = "nel50n";
public $name = "PickyWallpapers Bridge"; public $name = "PickyWallpapers Bridge";
public $uri = "http://www.pickywallpapers.com/"; public $uri = "http://www.pickywallpapers.com/";
public $description = "Returns the latests wallpapers from PickyWallpapers"; public $description = "Returns the latests wallpapers from PickyWallpapers";
public $parameters = array( array( public $parameters = array( array(
'c'=>array('name'=>'category'), 'c'=>array(
'name'=>'category',
'required'=>true
),
's'=>array('name'=>'subcategory'), 's'=>array('name'=>'subcategory'),
'm'=>array( 'm'=>array(
'name'=>'Max number of wallpapers', 'name'=>'Max number of wallpapers',
'defaultValue'=>12,
'type'=>'number' 'type'=>'number'
), ),
'r'=>array( 'r'=>array(
'name'=>'resolution', 'name'=>'resolution',
'exampleValue'=>'1920x1200, 1680x1050,…', 'exampleValue'=>'1920x1200, 1680x1050,…',
'defaultValue'=>'1920x1200',
'pattern'=>'[0-9]{3,4}x[0-9]{3,4}' 'pattern'=>'[0-9]{3,4}x[0-9]{3,4}'
) )
)); ));
public function collectData(){ public function collectData(){
$html = ''; $lastpage = 1;
if (!$this->getInput('c')) { $num = 0;
$this->returnClientError('You must specify at least a category (?c=...).'); $max = $this->getInput('m');
} else { $resolution = $this->getInput('r'); // Wide wallpaper default
$baseUri = 'http://www.pickywallpapers.com';
$this->category = $this->getInput('c'); for ($page = 1; $page <= $lastpage; $page++) {
$this->subcategory = $this->getInput('s') ?: ''; $html = $this->getSimpleHTMLDOM($this->getURI().'/page-'.$page.'/')
$this->resolution = $this->getInput('r') ?: '1920x1200'; // Wide wallpaper default or $this->returnServerError('No results for this query.');
$num = 0; if ($page === 1) {
$max = $this->getInput('m') ?: 12; preg_match('/page-(\d+)\/$/', $html->find('.pages li a', -2)->href, $matches);
$lastpage = 1; $lastpage = min($matches[1], ceil($max/12));
}
for ($page = 1; $page <= $lastpage; $page++) { foreach($html->find('.items li img') as $element) {
$link = $baseUri.'/'.$this->resolution.'/'.$this->category.'/'.(!empty($this->subcategory)?$this->subcategory.'/':'').'page-'.$page.'/';
$html = $this->getSimpleHTMLDOM($link) or $this->returnServerError('No results for this query.');
if ($page === 1) { $item = array();
preg_match('/page-(\d+)\/$/', $html->find('.pages li a', -2)->href, $matches); $item['uri'] = str_replace('www', 'wallpaper', $this->uri).'/'.$resolution.'/'.basename($element->src);
$lastpage = min($matches[1], ceil($max/12)); $item['timestamp'] = time();
} $item['title'] = $element->alt;
$item['content'] = $item['title'].'<br><a href="'.$item['uri'].'">'.$element.'</a>';
$this->items[] = $item;
foreach($html->find('.items li img') as $element) { $num++;
if ($num >= $max)
$item = array(); break 2;
$item['uri'] = str_replace('www', 'wallpaper', $baseUri).'/'.$this->resolution.'/'.basename($element->src);
$item['timestamp'] = time();
$item['title'] = $element->alt;
$item['content'] = $item['title'].'<br><a href="'.$item['uri'].'">'.$element.'</a>';
$this->items[] = $item;
$num++;
if ($num >= $max)
break 2;
}
} }
} }
} }
public function getURI(){
$subcategory = $this->getInput('s');
$link = $this->uri.$this->getInput('r').'/'.$this->getInput('c').'/'.$subcategory;
return $link;
}
public function getName(){ public function getName(){
return 'PickyWallpapers - '.$this->category.(!empty($this->subcategory) ? ' > '.$this->subcategory : '').' ['.$this->resolution.']'; $subcategory = $this->getInput('s');
return 'PickyWallpapers - '.$this->getInput('c')
.($subcategory? ' > '.$subcategory : '')
.' ['.$this->getInput('r').']';
} }
public function getCacheDuration(){ public function getCacheDuration(){