[WallpaperStopBridge] code simplification

Signed-off-by: Pierre Mazière <pierre.maziere@gmx.com>
This commit is contained in:
Pierre Mazière 2016-08-30 00:03:36 +02:00
parent ada5903d68
commit 014d016a51

View file

@ -1,13 +1,9 @@
<?php <?php
class WallpaperStopBridge extends BridgeAbstract { class WallpaperStopBridge extends BridgeAbstract {
private $category;
private $subcategory;
private $resolution;
public $maintainer = "nel50n"; public $maintainer = "nel50n";
public $name = "WallpaperStop Bridge"; public $name = "WallpaperStop Bridge";
public $uri = "http://www.wallpaperstop.com/"; public $uri = "http://www.wallpaperstop.com";
public $description = "Returns the latests wallpapers from WallpaperStop"; public $description = "Returns the latests wallpapers from WallpaperStop";
public $parameters = array( array( public $parameters = array( array(
@ -15,33 +11,30 @@ class WallpaperStopBridge extends BridgeAbstract {
's'=>array('name'=>'subcategory'), 's'=>array('name'=>'subcategory'),
'm'=>array( 'm'=>array(
'name'=>'Max number of wallpapers', 'name'=>'Max number of wallpapers',
'type'=>'number' 'type'=>'number',
'defaultValue'=>20
), ),
'r'=>array( 'r'=>array(
'name'=>'resolution', 'name'=>'resolution',
'exampleValue'=>'1920x1200, 1680x1050,…', 'exampleValue'=>'1920x1200, 1680x1050,…',
'defaultValue'=>'1920x1200'
) )
)); ));
public function collectData(){ public function collectData(){
$html = ''; $category = $this->getInput('c');
if (!$this->getInput('c')) { $subcategory = $this->getInput('s');
$this->returnClientError('You must specify at least a category (?c=...).'); $resolution = $this->getInput('r');
} else {
$baseUri = 'http://www.wallpaperstop.com';
$this->category = $this->getInput('c');
$this->subcategory = $this->getInput('s') ?: '';
$this->resolution = $this->getInput('r') ?: '1920x1200'; // Wide wallpaper default
$num = 0; $num = 0;
$max = $this->getInput('m') ?: 20; $max = $this->getInput('m');
$lastpage = 1; $lastpage = 1;
for ($page = 1; $page <= $lastpage; $page++) { for ($page = 1; $page <= $lastpage; $page++) {
$link = $baseUri.'/'.$this->category.'-wallpaper/'.(!empty($this->subcategory)?$this->subcategory.'-wallpaper/':'').'desktop-wallpaper-'.$page.'.html'; $link = $this->uri.'/'.$category.'-wallpaper/'.(!empty($subcategory)?$subcategory.'-wallpaper/':'').'desktop-wallpaper-'.$page.'.html';
$html = $this->getSimpleHTMLDOM($link) or $this->returnServerError('No results for this query.'); $html = $this->getSimpleHTMLDOM($link)
or $this->returnServerError('No results for this query.');
if ($page === 1) { if ($page === 1) {
preg_match('/-(\d+)\.html$/', $html->find('.pagination > .last', 0)->href, $matches); preg_match('/-(\d+)\.html$/', $html->find('.pagination > .last', 0)->href, $matches);
@ -50,29 +43,27 @@ class WallpaperStopBridge extends BridgeAbstract {
foreach($html->find('article.item') as $element) { foreach($html->find('article.item') as $element) {
$wplink = $element->getAttribute('data-permalink'); $wplink = $element->getAttribute('data-permalink');
if (preg_match('%^http://www\.wallpaperstop\.com/(.+)/([^/]+)-(\d+)\.html$%', $wplink, $matches)) { if (preg_match('%^'.$this->uri.'/(.+)/([^/]+)-(\d+)\.html$%', $wplink, $matches)) {
$thumbnail = $element->find('img', 0); $thumbnail = $element->find('img', 0);
$item = array(); $item = array();
$item['uri'] = $baseUri.'/wallpapers/'.str_replace('wallpaper', 'wallpapers', $matches[1]).'/'.$matches[2].'-'.$this->resolution.'-'.$matches[3].'.jpg'; $item['uri'] = $this->uri.'/wallpapers/'.str_replace('wallpaper', 'wallpapers', $matches[1]).'/'.$matches[2].'-'.$resolution.'-'.$matches[3].'.jpg';
$item['id'] = $matches[3]; $item['id'] = $matches[3];
$item['timestamp'] = time(); $item['timestamp'] = time();
$item['title'] = $thumbnail->title; $item['title'] = $thumbnail->title;
$item['content'] = $item['title'].'<br><a href="'.$wplink.'"><img src="'.$baseUri.$thumbnail->src.'" /></a>'; $item['content'] = $item['title'].'<br><a href="'.$wplink.'"><img src="'.$this->uri.$thumbnail->src.'" /></a>';
$this->items[] = $item; $this->items[] = $item;
$num++; $num++;
if ($num >= $max) if ($num >= $max)
break 2; break 2;
} }
}
} }
} }
} }
public function getName(){ public function getName(){
return 'WallpaperStop - '.$this->category.(!empty($this->subcategory) ? ' > '.$this->subcategory : '').' ['.$this->resolution.']'; return 'WallpaperStop - '.$this->getInput('c').(!empty($this->getInput('s')) ? ' > '.$this->getInput('s') : '').' ['.$this->getInput('r').']';
} }
public function getCacheDuration(){ public function getCacheDuration(){