2015-03-03 00:42:54 +03:00
|
|
|
<?php
|
|
|
|
class UnsplashBridge extends BridgeAbstract {
|
|
|
|
|
2016-08-30 12:23:55 +03:00
|
|
|
const MAINTAINER = "nel50n";
|
|
|
|
const NAME = "Unsplash Bridge";
|
|
|
|
const URI = "http://unsplash.com/";
|
2016-09-25 18:04:28 +03:00
|
|
|
const CACHE_TIMEOUT = 43200; // 12h
|
2016-08-30 12:23:55 +03:00
|
|
|
const DESCRIPTION = "Returns the latests photos from Unsplash";
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2016-08-30 12:23:55 +03:00
|
|
|
const PARAMETERS = array( array(
|
2016-08-22 02:25:56 +03:00
|
|
|
'm'=>array(
|
|
|
|
'name'=>'Max number of photos',
|
2016-08-30 00:39:29 +03:00
|
|
|
'type'=>'number',
|
|
|
|
'defaultValue'=>20
|
2016-08-22 02:25:56 +03:00
|
|
|
),
|
|
|
|
'w'=>array(
|
|
|
|
'name'=>'Width',
|
2016-08-30 00:39:29 +03:00
|
|
|
'exampleValue'=>'1920, 1680, …',
|
|
|
|
'defaultValue'=>'1920'
|
2016-08-22 02:25:56 +03:00
|
|
|
),
|
|
|
|
'q'=>array(
|
|
|
|
'name'=>'JPEG quality',
|
2016-08-30 00:39:29 +03:00
|
|
|
'type'=>'number',
|
|
|
|
'defaultValue'=>75
|
2016-08-22 02:25:56 +03:00
|
|
|
)
|
2016-08-27 22:03:26 +03:00
|
|
|
));
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2016-08-25 02:24:53 +03:00
|
|
|
public function collectData(){
|
2016-08-30 00:39:29 +03:00
|
|
|
$width = $this->getInput('w') ;
|
2015-03-03 00:42:54 +03:00
|
|
|
$num = 0;
|
2016-08-30 00:39:29 +03:00
|
|
|
$max = $this->getInput('m');
|
|
|
|
$quality = $this->getInput('q');
|
2015-03-03 00:42:54 +03:00
|
|
|
$lastpage = 1;
|
|
|
|
|
|
|
|
for ($page = 1; $page <= $lastpage; $page++) {
|
2016-08-30 12:23:55 +03:00
|
|
|
$link = self::URI.'/grid?page='.$page;
|
2016-09-26 00:22:33 +03:00
|
|
|
$html = getSimpleHTMLDOM($link)
|
|
|
|
or returnServerError('No results for this query.');
|
2015-03-03 00:42:54 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-08-22 19:55:59 +03:00
|
|
|
$item = array();
|
|
|
|
$item['uri'] = str_replace(array('q=75', 'w=400'),
|
2015-03-03 00:42:54 +03:00
|
|
|
array("q=$quality", "w=$width"),
|
|
|
|
$thumbnail->src).'.jpg'; // '.jpg' only for format hint
|
2016-08-22 19:55:59 +03:00
|
|
|
$item['timestamp'] = time();
|
|
|
|
$item['title'] = $thumbnail->alt;
|
|
|
|
$item['content'] = $item['title'].'<br><a href="'.$item['uri'].'"><img src="'.$thumbnail->src.'" /></a>';
|
2015-03-03 00:42:54 +03:00
|
|
|
$this->items[] = $item;
|
|
|
|
|
|
|
|
$num++;
|
|
|
|
if ($num >= $max)
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|