logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

flickr_noapi.py (3258B)


  1. #!/usr/bin/env python
  2. """
  3. Flickr (Images)
  4. @website https://www.flickr.com
  5. @provide-api yes (https://secure.flickr.com/services/api/flickr.photos.search.html)
  6. @using-api no
  7. @results HTML
  8. @stable no
  9. @parse url, title, thumbnail, img_src
  10. """
  11. from json import loads
  12. from time import time
  13. import re
  14. from searx.engines import logger
  15. from searx.url_utils import urlencode
  16. logger = logger.getChild('flickr-noapi')
  17. categories = ['images']
  18. url = 'https://www.flickr.com/'
  19. search_url = url + 'search?{query}&page={page}'
  20. time_range_url = '&min_upload_date={start}&max_upload_date={end}'
  21. photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
  22. regex = re.compile(r"\"search-photos-lite-models\",\"photos\":(.*}),\"totalItems\":", re.DOTALL)
  23. image_sizes = ('o', 'k', 'h', 'b', 'c', 'z', 'n', 'm', 't', 'q', 's')
  24. paging = True
  25. time_range_support = True
  26. time_range_dict = {'day': 60 * 60 * 24,
  27. 'week': 60 * 60 * 24 * 7,
  28. 'month': 60 * 60 * 24 * 7 * 4,
  29. 'year': 60 * 60 * 24 * 7 * 52}
  30. def build_flickr_url(user_id, photo_id):
  31. return photo_url.format(userid=user_id, photoid=photo_id)
  32. def _get_time_range_url(time_range):
  33. if time_range in time_range_dict:
  34. return time_range_url.format(start=time(), end=str(int(time()) - time_range_dict[time_range]))
  35. return ''
  36. def request(query, params):
  37. params['url'] = (search_url.format(query=urlencode({'text': query}), page=params['pageno'])
  38. + _get_time_range_url(params['time_range']))
  39. return params
  40. def response(resp):
  41. results = []
  42. matches = regex.search(resp.text)
  43. if matches is None:
  44. return results
  45. match = matches.group(1)
  46. search_results = loads(match)
  47. if '_data' not in search_results:
  48. return []
  49. photos = search_results['_data']
  50. for photo in photos:
  51. # In paged configuration, the first pages' photos
  52. # are represented by a None object
  53. if photo is None:
  54. continue
  55. img_src = None
  56. # From the biggest to the lowest format
  57. for image_size in image_sizes:
  58. if image_size in photo['sizes']:
  59. img_src = photo['sizes'][image_size]['url']
  60. break
  61. if not img_src:
  62. logger.debug('cannot find valid image size: {0}'.format(repr(photo)))
  63. continue
  64. if 'ownerNsid' not in photo:
  65. continue
  66. # For a bigger thumbnail, keep only the url_z, not the url_n
  67. if 'n' in photo['sizes']:
  68. thumbnail_src = photo['sizes']['n']['url']
  69. elif 'z' in photo['sizes']:
  70. thumbnail_src = photo['sizes']['z']['url']
  71. else:
  72. thumbnail_src = img_src
  73. url = build_flickr_url(photo['ownerNsid'], photo['id'])
  74. title = photo.get('title', '')
  75. author = photo['username']
  76. # append result
  77. results.append({'url': url,
  78. 'title': title,
  79. 'img_src': img_src,
  80. 'thumbnail_src': thumbnail_src,
  81. 'content': '',
  82. 'author': author,
  83. 'template': 'images.html'})
  84. return results