flickr_noapi.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, unquote
  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. modelexport_re = re.compile(r"^\s*modelExport:\s*({.*}),$", re.M)
  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 = modelexport_re.search(resp.text)
  43. if matches is None:
  44. return results
  45. match = matches.group(1)
  46. model_export = loads(match)
  47. if 'legend' not in model_export:
  48. return results
  49. legend = model_export['legend']
  50. # handle empty page
  51. if not legend or not legend[0]:
  52. return results
  53. for index in legend:
  54. photo = model_export['main'][index[0]][int(index[1])][index[2]][index[3]][int(index[4])]
  55. author = unquote(photo.get('realname', ''))
  56. source = unquote(photo.get('username', '')) + ' @ Flickr'
  57. title = unquote(photo.get('title', ''))
  58. content = unquote(photo.get('description', ''))
  59. img_src = None
  60. # From the biggest to the lowest format
  61. for image_size in image_sizes:
  62. if image_size in photo['sizes']:
  63. img_src = photo['sizes'][image_size]['url']
  64. img_format = 'jpg ' \
  65. + str(photo['sizes'][image_size]['width']) \
  66. + 'x' \
  67. + str(photo['sizes'][image_size]['height'])
  68. break
  69. if not img_src:
  70. logger.debug('cannot find valid image size: {0}'.format(repr(photo)))
  71. continue
  72. # For a bigger thumbnail, keep only the url_z, not the url_n
  73. if 'n' in photo['sizes']:
  74. thumbnail_src = photo['sizes']['n']['url']
  75. elif 'z' in photo['sizes']:
  76. thumbnail_src = photo['sizes']['z']['url']
  77. else:
  78. thumbnail_src = img_src
  79. if 'ownerNsid' not in photo:
  80. # should not happen, disowned photo? Show it anyway
  81. url = img_src
  82. else:
  83. url = build_flickr_url(photo['ownerNsid'], photo['id'])
  84. results.append({'url': url,
  85. 'title': title,
  86. 'img_src': img_src,
  87. 'thumbnail_src': thumbnail_src,
  88. 'content': content,
  89. 'author': author,
  90. 'source': source,
  91. 'img_format': img_format,
  92. 'template': 'images.html'})
  93. return results