summaryrefslogtreecommitdiff
path: root/bridges/UnsplashBridge.php
diff options
context:
space:
mode:
authorJohannes Schauer <josch@debian.org>2017-08-04 22:06:01 +0200
committerJohannes Schauer <josch@debian.org>2017-08-04 22:06:01 +0200
commitb005331cd910c0cc7dee2ddf82491b8248f431cf (patch)
treeff8b5cbfe81d570b878cb8d60ee51d07c3b1d059 /bridges/UnsplashBridge.php
Import rss-bridge_2017-08-03.orig.tar.gz
[dgit import orig rss-bridge_2017-08-03.orig.tar.gz]
Diffstat (limited to 'bridges/UnsplashBridge.php')
-rw-r--r--bridges/UnsplashBridge.php77
1 files changed, 77 insertions, 0 deletions
diff --git a/bridges/UnsplashBridge.php b/bridges/UnsplashBridge.php
new file mode 100644
index 0000000..ee1040a
--- /dev/null
+++ b/bridges/UnsplashBridge.php
@@ -0,0 +1,77 @@
+<?php
+class UnsplashBridge extends BridgeAbstract {
+
+ const MAINTAINER = 'nel50n';
+ const NAME = 'Unsplash Bridge';
+ const URI = 'http://unsplash.com/';
+ const CACHE_TIMEOUT = 43200; // 12h
+ const DESCRIPTION = 'Returns the latests photos from Unsplash';
+
+ const PARAMETERS = array( array(
+ 'm' => array(
+ 'name' => 'Max number of photos',
+ 'type' => 'number',
+ 'defaultValue' => 20
+ ),
+ 'w' => array(
+ 'name' => 'Width',
+ 'exampleValue' => '1920, 1680, …',
+ 'defaultValue' => '1920'
+ ),
+ 'q' => array(
+ 'name' => 'JPEG quality',
+ 'type' => 'number',
+ 'defaultValue' => 75
+ )
+ ));
+
+ 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.');
+
+ 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 = 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['title'] = $thumbnail->alt;
+ $item['content'] = $item['title']
+ . '<br><a href="'
+ . $item['uri']
+ . '"><img src="'
+ . $thumbnail->src
+ . '" /></a>';
+
+ $this->items[] = $item;
+
+ $num++;
+ if ($num >= $max)
+ break 2;
+ }
+ }
+ }
+}