unsplash.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """
  2. Unsplash
  3. @website https://unsplash.com
  4. @provide-api yes (https://unsplash.com/developers)
  5. @using-api no
  6. @results JSON (using search portal's infiniscroll API)
  7. @stable no (JSON format could change any time)
  8. @parse url, title, img_src, thumbnail_src
  9. """
  10. from searx.url_utils import urlencode
  11. from json import loads
  12. url = 'https://unsplash.com/'
  13. search_url = url + 'napi/search/photos?'
  14. categories = ['images']
  15. page_size = 20
  16. paging = True
  17. def request(query, params):
  18. params['url'] = search_url + urlencode({'query': query, 'page': params['pageno'], 'per_page': page_size})
  19. return params
  20. def response(resp):
  21. results = []
  22. json_data = loads(resp.text)
  23. if 'results' in json_data:
  24. for result in json_data['results']:
  25. results.append({'template': 'images.html',
  26. 'url': result['links']['html'],
  27. 'thumbnail_src': result['urls']['thumb'],
  28. 'img_src': result['urls']['raw'],
  29. 'title': result['description'],
  30. 'content': ''})
  31. return results