FlickrBridge.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. $filter = 'photo-lite-models';
  36. $html = getSimpleHTMLDOM(self::URI . 'explore')
  37. or returnServerError('Could not request Flickr.');
  38. break;
  39. case 'By keyword':
  40. $filter = 'photo-lite-models';
  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. $filter = 'photo-models';
  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. $model_json = $this->extractJsonModel($html);
  53. $photo_models = $this->getPhotoModels($model_json, $filter);
  54. foreach($photo_models as $model) {
  55. $item = array();
  56. /* Author name depends on scope. On a keyword search the
  57. * author is part of the picture data. On a username search
  58. * the author is part of the owner data.
  59. */
  60. if(array_key_exists('username', $model)) {
  61. $item['author'] = $model['username'];
  62. } elseif (array_key_exists('owner', reset($model_json)[0])) {
  63. $item['author'] = reset($model_json)[0]['owner']['username'];
  64. }
  65. $item['title'] = (array_key_exists('title', $model) ? $model['title'] : 'Untitled');
  66. $item['uri'] = self::URI . 'photo.gne?id=' . $model['id'];
  67. $description = (array_key_exists('description', $model) ? $model['description'] : '');
  68. $item['content'] = '<a href="'
  69. . $item['uri']
  70. . '"><img src="'
  71. . $this->extractContentImage($model)
  72. . '" style="max-width: 640px; max-height: 480px;"/></a><br><p>'
  73. . $description
  74. . '</p>';
  75. $item['enclosures'] = $this->extractEnclosures($model);
  76. $this->items[] = $item;
  77. }
  78. }
  79. private function extractJsonModel($html) {
  80. // Find SCRIPT containing JSON data
  81. $model = $html->find('.modelExport', 0);
  82. $model_text = $model->innertext;
  83. // Find start and end of JSON data
  84. $start = strpos($model_text, 'modelExport:') + strlen('modelExport:');
  85. $end = strpos($model_text, 'auth:') - strlen('auth:');
  86. // Extract JSON data, remove trailing comma
  87. $model_text = trim(substr($model_text, $start, $end - $start));
  88. $model_text = substr($model_text, 0, strlen($model_text) - 1);
  89. return json_decode($model_text, true);
  90. }
  91. private function getPhotoModels($json, $filter) {
  92. // The JSON model contains a "legend" array, where each element contains
  93. // the path to an element in the "main" object
  94. $photo_models = array();
  95. foreach($json['legend'] as $legend) {
  96. $photo_model = $json['main'];
  97. foreach($legend as $element) { // Traverse tree
  98. $photo_model = $photo_model[$element];
  99. }
  100. // We are only interested in content
  101. if($photo_model['_flickrModelRegistry'] === $filter) {
  102. $photo_models[] = $photo_model;
  103. }
  104. }
  105. return $photo_models;
  106. }
  107. private function extractEnclosures($model) {
  108. $areas = array();
  109. foreach($model['sizes'] as $size) {
  110. $areas[$size['width'] * $size['height']] = $size['url'];
  111. }
  112. return array($this->fixURL(max($areas)));
  113. }
  114. private function extractContentImage($model) {
  115. $areas = array();
  116. $limit = 320 * 240;
  117. foreach($model['sizes'] as $size) {
  118. $image_area = $size['width'] * $size['height'];
  119. if($image_area >= $limit) {
  120. $areas[$image_area] = $size['url'];
  121. }
  122. }
  123. return $this->fixURL(min($areas));
  124. }
  125. private function fixURL($url) {
  126. // For some reason the image URLs don't include the protocol (https)
  127. if(strpos($url, '//') === 0) {
  128. $url = 'https:' . $url;
  129. }
  130. return $url;
  131. }
  132. }