[UnsplashBridge] Fix bridge (fix issue #965) (#1208)

This commit is contained in:
Corentin Garcia 2019-07-16 16:50:14 +02:00 committed by Teromene
parent 5a0ea423c4
commit eb942bc498

View file

@ -3,7 +3,7 @@ class UnsplashBridge extends BridgeAbstract {
const MAINTAINER = 'nel50n'; const MAINTAINER = 'nel50n';
const NAME = 'Unsplash Bridge'; const NAME = 'Unsplash Bridge';
const URI = 'http://unsplash.com/'; const URI = 'https://unsplash.com/';
const CACHE_TIMEOUT = 43200; // 12h const CACHE_TIMEOUT = 43200; // 12h
const DESCRIPTION = 'Returns the latests photos from Unsplash'; const DESCRIPTION = 'Returns the latests photos from Unsplash';
@ -27,51 +27,42 @@ class UnsplashBridge extends BridgeAbstract {
public function collectData(){ public function collectData(){
$width = $this->getInput('w'); $width = $this->getInput('w');
$num = 0;
$max = $this->getInput('m'); $max = $this->getInput('m');
$quality = $this->getInput('q'); $quality = $this->getInput('q');
$lastpage = 1;
for($page = 1; $page <= $lastpage; $page++) { $api_response = getContents('https://unsplash.com/napi/photos?page=1&per_page=' . $max)
$link = self::URI . '/grid?page=' . $page; or returnServerError('Could not request Unsplash API.');
$html = getSimpleHTMLDOM($link) $json = json_decode($api_response, true);
or returnServerError('No results for this query.');
if($page === 1) { foreach ($json as $json_item) {
preg_match( $item = array();
'/=(\d+)$/',
$html->find('.pagination > a[!class]', -1)->href,
$matches
);
$lastpage = min($matches[1], ceil($max / 40)); // Get image URI
$uri = $json_item['urls']['regular'] . '.jpg'; // '.jpg' only for format hint
$uri = str_replace('q=80', 'q=' . $quality, $uri);
$uri = str_replace('w=1080', 'w=' . $width, $uri);
$item['uri'] = $uri;
// Get title from description
if (is_null($json_item['alt_description'])) {
if (is_null($json_item['description'])) {
$item['title'] = 'Unsplash picture from ' . $json_item['user']['name'];
} else {
$item['title'] = $json_item['description'];
}
} else {
$item['title'] = $json_item['alt_description'];
} }
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['timestamp'] = time();
$item['title'] = $thumbnail->alt;
$item['content'] = $item['title'] $item['content'] = $item['title']
. '<br><a href="' . '<br><a href="'
. $item['uri'] . $item['uri']
. '"><img src="' . '"><img src="'
. $thumbnail->src . $json_item['urls']['thumb']
. '" /></a>'; . '" /></a>';
$this->items[] = $item; $this->items[] = $item;
$num++;
if ($num >= $max)
break 2;
}
} }
} }
} }