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-09 15:50:25 +02:00
|
|
|
$this->update = "2016-08-09";
|
2015-11-05 16:50:18 +01:00
|
|
|
|
|
|
|
$this->parameters[] =
|
|
|
|
'[
|
|
|
|
{
|
|
|
|
"name" : "Max number of photos",
|
|
|
|
"identifier" : "m",
|
|
|
|
"type" : "number"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name" : "Width",
|
|
|
|
"identifier" : "w",
|
|
|
|
"exampleValue" : "1920, 1680, ..."
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name" : "JPEG quality",
|
|
|
|
"identifier" : "q",
|
|
|
|
"type" : "number"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
]';
|
|
|
|
}
|
|
|
|
|
2015-03-02 22:42:54 +01:00
|
|
|
public function collectData(array $param){
|
|
|
|
$html = '';
|
|
|
|
$baseUri = 'http://unsplash.com';
|
|
|
|
|
|
|
|
$width = $param['w'] ?: '1920'; // Default width
|
|
|
|
|
|
|
|
$num = 0;
|
|
|
|
$max = $param['m'] ?: 20;
|
|
|
|
$quality = $param['q'] ?: 75;
|
|
|
|
$lastpage = 1;
|
|
|
|
|
|
|
|
for ($page = 1; $page <= $lastpage; $page++) {
|
|
|
|
$link = $baseUri.'/grid?page='.$page;
|
2016-06-25 23:17:42 +02:00
|
|
|
$html = $this->file_get_html($link) or $this->returnError('No results for this query.', 404);
|
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);
|
|
|
|
|
|
|
|
$item = new \Item();
|
|
|
|
$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;
|
2016-08-09 15:50:25 +02:00
|
|
|
$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 getName(){
|
|
|
|
return 'Unsplash';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getURI(){
|
|
|
|
return 'http://unsplash.com';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheDuration(){
|
|
|
|
return 43200; // 12 hours
|
|
|
|
}
|
|
|
|
}
|