FlickrBridge.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /* This is a mashup of FlickrExploreBridge by sebsauvage and FlickrTagBridge
  3. * by erwang, providing the functionality of both in one.
  4. */
  5. class FlickrBridge extends BridgeAbstract {
  6. const MAINTAINER = 'logmanoriginal';
  7. const NAME = 'Flickr Bridge';
  8. const URI = 'https://www.flickr.com/';
  9. const CACHE_TIMEOUT = 21600; // 6 hours
  10. const DESCRIPTION = 'Returns images from Flickr';
  11. const PARAMETERS = array(
  12. 'Explore' => array(),
  13. 'By keyword' => array(
  14. 'q' => array(
  15. 'name' => 'Keyword',
  16. 'type' => 'text',
  17. 'required' => true,
  18. 'title' => 'Insert keyword',
  19. 'exampleValue' => 'bird'
  20. )
  21. ),
  22. 'By username' => array(
  23. 'u' => array(
  24. 'name' => 'Username',
  25. 'type' => 'text',
  26. 'required' => true,
  27. 'title' => 'Insert username (as shown in the address bar)',
  28. 'exampleValue' => 'flickr'
  29. )
  30. ),
  31. );
  32. public function collectData(){
  33. switch($this->queriedContext){
  34. case 'Explore':
  35. $key = 'photos';
  36. $html = getSimpleHTMLDOM(self::URI . 'explore')
  37. or returnServerError('Could not request Flickr.');
  38. break;
  39. case 'By keyword':
  40. $key = 'photos';
  41. $html = getSimpleHTMLDOM(self::URI . 'search/?q=' . urlencode($this->getInput('q')) . '&s=rec')
  42. or returnServerError('No results for this query.');
  43. break;
  44. case 'By username':
  45. $key = 'photoPageList';
  46. $html = getSimpleHTMLDOM(self::URI . 'photos/' . urlencode($this->getInput('u')))
  47. or returnServerError('Requested username can\'t be found.');
  48. break;
  49. default:
  50. returnClientError('Invalid context: ' . $this->queriedContext);
  51. }
  52. // Find SCRIPT containing JSON data
  53. $model = $html->find('.modelExport', 0);
  54. $model_text = $model->innertext;
  55. // Find start and end of JSON data
  56. $start = strpos($model_text, 'modelExport:') + strlen('modelExport:');
  57. $end = strpos($model_text, 'auth:') - strlen('auth:');
  58. // Dissect JSON data and remove trailing comma
  59. $model_text = trim(substr($model_text, $start, $end - $start));
  60. $model_text = substr($model_text, 0, strlen($model_text) - 1);
  61. $model_json = json_decode($model_text, true);
  62. foreach($html->find('.photo-list-photo-view') as $element){
  63. // Get the styles
  64. $style = explode(';', $element->style);
  65. // Get the background-image style
  66. $backgroundImage = explode(':', end($style));
  67. // URI type : url(//cX.staticflickr.com/X/XXXXX/XXXXXXXXX.jpg)
  68. $imageURI = trim(str_replace(['url(', ')'], '', end($backgroundImage)));
  69. // Get the image ID
  70. $imageURIs = explode('_', basename($imageURI));
  71. $imageID = reset($imageURIs);
  72. // Use JSON data to build items
  73. foreach(reset($model_json)[0][$key]['_data'] as $element){
  74. if($element['id'] === $imageID){
  75. $item = array();
  76. /* Author name depends on scope. On a keyword search the
  77. * author is part of the picture data. On a username search
  78. * the author is part of the owner data.
  79. */
  80. if(array_key_exists('username', $element)){
  81. $item['author'] = $element['username'];
  82. } elseif (array_key_exists('owner', reset($model_json)[0])){
  83. $item['author'] = reset($model_json)[0]['owner']['username'];
  84. }
  85. $item['title'] = (array_key_exists('title', $element) ? $element['title'] : 'Untitled');
  86. $item['uri'] = self::URI . 'photo.gne?id=' . $imageID;
  87. $description = (array_key_exists('description', $element) ? $element['description'] : '');
  88. $item['content'] = '<a href="'
  89. . $item['uri']
  90. . '"><img src="'
  91. . $imageURI
  92. . '" /></a><br><p>'
  93. . $description
  94. . '</p>';
  95. $this->items[] = $item;
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. }