From 014d016a51bd52c3625d9ed9e98b3b52bacdfac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20Mazi=C3=A8re?= Date: Tue, 30 Aug 2016 00:03:36 +0200 Subject: [PATCH] [WallpaperStopBridge] code simplification MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pierre Mazière --- bridges/WallpaperStopBridge.php | 81 +++++++++++++++------------------ 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/bridges/WallpaperStopBridge.php b/bridges/WallpaperStopBridge.php index e2c7e11..633642c 100644 --- a/bridges/WallpaperStopBridge.php +++ b/bridges/WallpaperStopBridge.php @@ -1,13 +1,9 @@ array('name'=>'subcategory'), 'm'=>array( 'name'=>'Max number of wallpapers', - 'type'=>'number' + 'type'=>'number', + 'defaultValue'=>20 ), 'r'=>array( 'name'=>'resolution', 'exampleValue'=>'1920x1200, 1680x1050,…', + 'defaultValue'=>'1920x1200' ) )); public function collectData(){ - $html = ''; - if (!$this->getInput('c')) { - $this->returnClientError('You must specify at least a category (?c=...).'); - } else { - $baseUri = 'http://www.wallpaperstop.com'; + $category = $this->getInput('c'); + $subcategory = $this->getInput('s'); + $resolution = $this->getInput('r'); - $this->category = $this->getInput('c'); - $this->subcategory = $this->getInput('s') ?: ''; - $this->resolution = $this->getInput('r') ?: '1920x1200'; // Wide wallpaper default + $num = 0; + $max = $this->getInput('m'); + $lastpage = 1; - $num = 0; - $max = $this->getInput('m') ?: 20; - $lastpage = 1; + for ($page = 1; $page <= $lastpage; $page++) { + $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.'); - for ($page = 1; $page <= $lastpage; $page++) { - $link = $baseUri.'/'.$this->category.'-wallpaper/'.(!empty($this->subcategory)?$this->subcategory.'-wallpaper/':'').'desktop-wallpaper-'.$page.'.html'; - $html = $this->getSimpleHTMLDOM($link) or $this->returnServerError('No results for this query.'); + if ($page === 1) { + preg_match('/-(\d+)\.html$/', $html->find('.pagination > .last', 0)->href, $matches); + $lastpage = min($matches[1], ceil($max/20)); + } - if ($page === 1) { - preg_match('/-(\d+)\.html$/', $html->find('.pagination > .last', 0)->href, $matches); - $lastpage = min($matches[1], ceil($max/20)); - } + foreach($html->find('article.item') as $element) { + $wplink = $element->getAttribute('data-permalink'); + if (preg_match('%^'.$this->uri.'/(.+)/([^/]+)-(\d+)\.html$%', $wplink, $matches)) { + $thumbnail = $element->find('img', 0); - foreach($html->find('article.item') as $element) { - $wplink = $element->getAttribute('data-permalink'); - if (preg_match('%^http://www\.wallpaperstop\.com/(.+)/([^/]+)-(\d+)\.html$%', $wplink, $matches)) { - $thumbnail = $element->find('img', 0); + $item = array(); + $item['uri'] = $this->uri.'/wallpapers/'.str_replace('wallpaper', 'wallpapers', $matches[1]).'/'.$matches[2].'-'.$resolution.'-'.$matches[3].'.jpg'; + $item['id'] = $matches[3]; + $item['timestamp'] = time(); + $item['title'] = $thumbnail->title; + $item['content'] = $item['title'].'
'; + $this->items[] = $item; - $item = array(); - $item['uri'] = $baseUri.'/wallpapers/'.str_replace('wallpaper', 'wallpapers', $matches[1]).'/'.$matches[2].'-'.$this->resolution.'-'.$matches[3].'.jpg'; - $item['id'] = $matches[3]; - $item['timestamp'] = time(); - $item['title'] = $thumbnail->title; - $item['content'] = $item['title'].'
'; - $this->items[] = $item; - - $num++; - if ($num >= $max) - break 2; - } - - } - } - } + $num++; + if ($num >= $max) + break 2; + } + } + } } 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(){