Skip to content

Commit

Permalink
[UnsplashBridge] Fix bridge (fix issue RSS-Bridge#965) (RSS-Bridge#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
corenting authored and teromene committed Jul 16, 2019
1 parent 44ced11 commit 916cb3d
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions bridges/UnsplashBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class UnsplashBridge extends BridgeAbstract {

const MAINTAINER = 'nel50n';
const NAME = 'Unsplash Bridge';
const URI = 'http://unsplash.com/';
const URI = 'https://unsplash.com/';
const CACHE_TIMEOUT = 43200; // 12h
const DESCRIPTION = 'Returns the latests photos from Unsplash';

Expand All @@ -27,51 +27,42 @@ class UnsplashBridge extends BridgeAbstract {

public function collectData(){
$width = $this->getInput('w');
$num = 0;
$max = $this->getInput('m');
$quality = $this->getInput('q');
$lastpage = 1;

for($page = 1; $page <= $lastpage; $page++) {
$link = self::URI . '/grid?page=' . $page;
$html = getSimpleHTMLDOM($link)
or returnServerError('No results for this query.');
$api_response = getContents('https://unsplash.com/napi/photos?page=1&per_page=' . $max)
or returnServerError('Could not request Unsplash API.');
$json = json_decode($api_response, true);

if($page === 1) {
preg_match(
'/=(\d+)$/',
$html->find('.pagination > a[!class]', -1)->href,
$matches
);
foreach ($json as $json_item) {
$item = array();

$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);
// 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;

$item = array();
$item['uri'] = str_replace(
array('q=75', 'w=400'),
array("q=$quality", "w=$width"),
$thumbnail->src) . '.jpg'; // '.jpg' only for format hint
// 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'];
}

$item['timestamp'] = time();
$item['title'] = $thumbnail->alt;
$item['content'] = $item['title']
$item['timestamp'] = time();
$item['content'] = $item['title']
. '<br><a href="'
. $item['uri']
. '"><img src="'
. $thumbnail->src
. $json_item['urls']['thumb']
. '" /></a>';

$this->items[] = $item;

$num++;
if ($num >= $max)
break 2;
}
$this->items[] = $item;
}
}
}

0 comments on commit 916cb3d

Please sign in to comment.