2015-03-02 22:42:54 +01:00
|
|
|
<?php
|
|
|
|
class UnsplashBridge extends BridgeAbstract {
|
|
|
|
|
2015-11-05 16:50:18 +01:00
|
|
|
public function loadMetadatas() {
|
|
|
|
|
|
|
|
$this->maintainer = "nel50n";
|
|
|
|
$this->name = "Unsplash Bridge";
|
|
|
|
$this->uri = "http://unsplash.com/";
|
|
|
|
$this->description = "Returns the latests photos from Unsplash";
|
|
|
|
|
2016-08-22 01:25:56 +02:00
|
|
|
$this->parameters[] = array(
|
|
|
|
'm'=>array(
|
|
|
|
'name'=>'Max number of photos',
|
|
|
|
'type'=>'number'
|
|
|
|
),
|
|
|
|
'w'=>array(
|
|
|
|
'name'=>'Width',
|
|
|
|
'exampleValue'=>'1920, 1680, …'
|
|
|
|
),
|
|
|
|
'q'=>array(
|
|
|
|
'name'=>'JPEG quality',
|
|
|
|
'type'=>'number'
|
|
|
|
)
|
|
|
|
);
|
2015-11-05 16:50:18 +01:00
|
|
|
}
|
|
|
|
|
2016-08-25 01:24:53 +02:00
|
|
|
public function collectData(){
|
|
|
|
$param=$this->parameters[$this->queriedContext];
|
2015-03-02 22:42:54 +01:00
|
|
|
$html = '';
|
|
|
|
$baseUri = 'http://unsplash.com';
|
|
|
|
|
2016-08-25 01:24:53 +02:00
|
|
|
$width = $param['w']['value'] ?: '1920'; // Default width
|
2015-03-02 22:42:54 +01:00
|
|
|
|
|
|
|
$num = 0;
|
2016-08-25 01:24:53 +02:00
|
|
|
$max = $param['m']['value'] ?: 20;
|
|
|
|
$quality = $param['q']['value'] ?: 75;
|
2015-03-02 22:42:54 +01:00
|
|
|
$lastpage = 1;
|
|
|
|
|
|
|
|
for ($page = 1; $page <= $lastpage; $page++) {
|
|
|
|
$link = $baseUri.'/grid?page='.$page;
|
2016-07-08 19:06:35 +02:00
|
|
|
$html = $this->getSimpleHTMLDOM($link) or $this->returnServerError('No results for this query.');
|
2015-03-02 22:42:54 +01: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 18:55:59 +02:00
|
|
|
$item = array();
|
|
|
|
$item['uri'] = str_replace(array('q=75', 'w=400'),
|
2015-03-02 22:42:54 +01:00
|
|
|
array("q=$quality", "w=$width"),
|
|
|
|
$thumbnail->src).'.jpg'; // '.jpg' only for format hint
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['timestamp'] = time();
|
|
|
|
$item['title'] = $thumbnail->alt;
|
|
|
|
$item['content'] = $item['title'].'<br><a href="'.$item['uri'].'"><img src="'.$thumbnail->src.'" /></a>';
|
2015-03-02 22:42:54 +01:00
|
|
|
$this->items[] = $item;
|
|
|
|
|
|
|
|
$num++;
|
|
|
|
if ($num >= $max)
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheDuration(){
|
|
|
|
return 43200; // 12 hours
|
|
|
|
}
|
|
|
|
}
|