logo

searx

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

flickr.py (2464B)


  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 yes
  7. @results JSON
  8. @stable yes
  9. @parse url, title, thumbnail, img_src
  10. More info on api-key : https://www.flickr.com/services/apps/create/
  11. """
  12. from json import loads
  13. from searx.url_utils import urlencode
  14. categories = ['images']
  15. nb_per_page = 15
  16. paging = True
  17. api_key = None
  18. url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search' +\
  19. '&api_key={api_key}&{text}&sort=relevance' +\
  20. '&extras=description%2C+owner_name%2C+url_o%2C+url_n%2C+url_z' +\
  21. '&per_page={nb_per_page}&format=json&nojsoncallback=1&page={page}'
  22. photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
  23. paging = True
  24. def build_flickr_url(user_id, photo_id):
  25. return photo_url.format(userid=user_id, photoid=photo_id)
  26. def request(query, params):
  27. params['url'] = url.format(text=urlencode({'text': query}),
  28. api_key=api_key,
  29. nb_per_page=nb_per_page,
  30. page=params['pageno'])
  31. return params
  32. def response(resp):
  33. results = []
  34. search_results = loads(resp.text)
  35. # return empty array if there are no results
  36. if 'photos' not in search_results:
  37. return []
  38. if 'photo' not in search_results['photos']:
  39. return []
  40. photos = search_results['photos']['photo']
  41. # parse results
  42. for photo in photos:
  43. if 'url_o' in photo:
  44. img_src = photo['url_o']
  45. elif 'url_z' in photo:
  46. img_src = photo['url_z']
  47. else:
  48. continue
  49. # For a bigger thumbnail, keep only the url_z, not the url_n
  50. if 'url_n' in photo:
  51. thumbnail_src = photo['url_n']
  52. elif 'url_z' in photo:
  53. thumbnail_src = photo['url_z']
  54. else:
  55. thumbnail_src = img_src
  56. url = build_flickr_url(photo['owner'], photo['id'])
  57. # append result
  58. results.append({'url': url,
  59. 'title': photo['title'],
  60. 'img_src': img_src,
  61. 'thumbnail_src': thumbnail_src,
  62. 'content': photo['description']['_content'],
  63. 'author': photo['ownername'],
  64. 'template': 'images.html'})
  65. # return results
  66. return results