flickr_noapi.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 urllib import urlencode
  12. from json import loads
  13. from time import time
  14. import re
  15. from searx.engines import logger
  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. def build_flickr_url(user_id, photo_id):
  30. return photo_url.format(userid=user_id, photoid=photo_id)
  31. def _get_time_range_url(time_range):
  32. if time_range in time_range_dict:
  33. return time_range_url.format(start=time(), end=str(int(time()) - time_range_dict[time_range]))
  34. return ''
  35. def request(query, params):
  36. params['url'] = (search_url.format(query=urlencode({'text': query}), page=params['pageno'])
  37. + _get_time_range_url(params['time_range']))
  38. return params
  39. def response(resp):
  40. results = []
  41. matches = regex.search(resp.text)
  42. if matches is None:
  43. return results
  44. match = matches.group(1)
  45. search_results = loads(match)
  46. if '_data' not in search_results:
  47. return []
  48. photos = search_results['_data']
  49. for photo in photos:
  50. # In paged configuration, the first pages' photos
  51. # are represented by a None object
  52. if photo is None:
  53. continue
  54. img_src = None
  55. # From the biggest to the lowest format
  56. for image_size in image_sizes:
  57. if image_size in photo['sizes']:
  58. img_src = photo['sizes'][image_size]['url']
  59. break
  60. if not img_src:
  61. logger.debug('cannot find valid image size: {0}'.format(repr(photo)))
  62. continue
  63. if 'ownerNsid' not in photo:
  64. continue
  65. # For a bigger thumbnail, keep only the url_z, not the url_n
  66. if 'n' in photo['sizes']:
  67. thumbnail_src = photo['sizes']['n']['url']
  68. elif 'z' in photo['sizes']:
  69. thumbnail_src = photo['sizes']['z']['url']
  70. else:
  71. thumbnail_src = img_src
  72. url = build_flickr_url(photo['ownerNsid'], photo['id'])
  73. title = photo.get('title', '')
  74. content = '<span class="photo-author">' +\
  75. photo['username'] +\
  76. '</span><br />'
  77. # append result
  78. results.append({'url': url,
  79. 'title': title,
  80. 'img_src': img_src,
  81. 'thumbnail_src': thumbnail_src,
  82. 'content': content,
  83. 'template': 'images.html'})
  84. return results