UnsplashBridge.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. class UnsplashBridge extends BridgeAbstract {
  3. public function loadMetadatas() {
  4. $this->maintainer = "nel50n";
  5. $this->name = "Unsplash Bridge";
  6. $this->uri = "http://unsplash.com/";
  7. $this->description = "Returns the latests photos from Unsplash";
  8. $this->update = "2015-03-02";
  9. $this->parameters[] =
  10. '[
  11. {
  12. "name" : "Max number of photos",
  13. "identifier" : "m",
  14. "type" : "number"
  15. },
  16. {
  17. "name" : "Width",
  18. "identifier" : "w",
  19. "exampleValue" : "1920, 1680, ..."
  20. },
  21. {
  22. "name" : "JPEG quality",
  23. "identifier" : "q",
  24. "type" : "number"
  25. }
  26. ]';
  27. }
  28. public function collectData(array $param){
  29. $html = '';
  30. $baseUri = 'http://unsplash.com';
  31. $width = $param['w'] ?: '1920'; // Default width
  32. $num = 0;
  33. $max = $param['m'] ?: 20;
  34. $quality = $param['q'] ?: 75;
  35. $lastpage = 1;
  36. for ($page = 1; $page <= $lastpage; $page++) {
  37. $link = $baseUri.'/grid?page='.$page;
  38. $html = $this->file_get_html($link) or $this->returnError('No results for this query.', 404);
  39. if ($page === 1) {
  40. preg_match('/=(\d+)$/', $html->find('.pagination > a[!class]', -1)->href, $matches);
  41. $lastpage = min($matches[1], ceil($max/40));
  42. }
  43. foreach($html->find('.photo') as $element) {
  44. $thumbnail = $element->find('img', 0);
  45. $thumbnail->src = str_replace('https://', 'http://', $thumbnail->src);
  46. $item = new \Item();
  47. $item->uri = str_replace(array('q=75', 'w=400'),
  48. array("q=$quality", "w=$width"),
  49. $thumbnail->src).'.jpg'; // '.jpg' only for format hint
  50. $item->timestamp = time();
  51. $item->title = $thumbnail->alt;
  52. $item->thumbnailUri = $thumbnail->src;
  53. $item->content = $item->title.'<br><a href="'.$item->uri.'"><img src="'.$item->thumbnailUri.'" /></a>';
  54. $this->items[] = $item;
  55. $num++;
  56. if ($num >= $max)
  57. break 2;
  58. }
  59. }
  60. }
  61. public function getName(){
  62. return 'Unsplash';
  63. }
  64. public function getURI(){
  65. return 'http://unsplash.com';
  66. }
  67. public function getCacheDuration(){
  68. return 43200; // 12 hours
  69. }
  70. }