1
0
Fork 0
forked from blallo/rss-bridge
rss-bridge/bridges/UnsplashBridge.php
Pierre Mazière 1b3c8a8aeb [core + bridges] add BridgeAbstract::$inputs and BridgeAbstract::getInput()
Inputs are not stored in BridgeAbstract::$parameters anymore to separate
static data from dynamic data.
The getInput method allows for more readable code.

Also fix an "undefined index 'global'" notice

Probability of breaking bridges: high !

Signed-off-by: Pierre Mazière <pierre.maziere@gmx.com>
2016-08-28 13:05:03 +02:00

67 lines
2.2 KiB
PHP

<?php
class UnsplashBridge extends BridgeAbstract {
public $maintainer = "nel50n";
public $name = "Unsplash Bridge";
public $uri = "http://unsplash.com/";
public $description = "Returns the latests photos from Unsplash";
public $parameters = array( array(
'm'=>array(
'name'=>'Max number of photos',
'type'=>'number'
),
'w'=>array(
'name'=>'Width',
'exampleValue'=>'1920, 1680, …'
),
'q'=>array(
'name'=>'JPEG quality',
'type'=>'number'
)
));
public function collectData(){
$html = '';
$baseUri = 'http://unsplash.com';
$width = $this->getInput('w') ?: '1920'; // Default width
$num = 0;
$max = $this->getInput('m') ?: 20;
$quality = $this->getInput('q') ?: 75;
$lastpage = 1;
for ($page = 1; $page <= $lastpage; $page++) {
$link = $baseUri.'/grid?page='.$page;
$html = $this->getSimpleHTMLDOM($link) or $this->returnServerError('No results for this query.');
if ($page === 1) {
preg_match('/=(\d+)$/', $html->find('.pagination > a[!class]', -1)->href, $matches);
$lastpage = min($matches[1], ceil($max/40));
}
foreach($html->find('.photo') as $element) {
$thumbnail = $element->find('img', 0);
$thumbnail->src = str_replace('https://', 'http://', $thumbnail->src);
$item = array();
$item['uri'] = str_replace(array('q=75', 'w=400'),
array("q=$quality", "w=$width"),
$thumbnail->src).'.jpg'; // '.jpg' only for format hint
$item['timestamp'] = time();
$item['title'] = $thumbnail->alt;
$item['content'] = $item['title'].'<br><a href="'.$item['uri'].'"><img src="'.$thumbnail->src.'" /></a>';
$this->items[] = $item;
$num++;
if ($num >= $max)
break 2;
}
}
}
public function getCacheDuration(){
return 43200; // 12 hours
}
}