unsplash.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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, urlparse, urlunparse, parse_qsl
  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 clean_url(url):
  18. parsed = urlparse(url)
  19. query = [(k, v) for (k, v) in parse_qsl(parsed.query) if k not in ['ixid', 's']]
  20. return urlunparse((parsed.scheme,
  21. parsed.netloc,
  22. parsed.path,
  23. parsed.params,
  24. urlencode(query),
  25. parsed.fragment))
  26. def request(query, params):
  27. params['url'] = search_url + urlencode({'query': query, 'page': params['pageno'], 'per_page': page_size})
  28. return params
  29. def response(resp):
  30. results = []
  31. json_data = loads(resp.text)
  32. if 'results' in json_data:
  33. for result in json_data['results']:
  34. results.append({'template': 'images.html',
  35. 'url': clean_url(result['links']['html']),
  36. 'thumbnail_src': clean_url(result['urls']['thumb']),
  37. 'img_src': clean_url(result['urls']['raw']),
  38. 'title': result['description'],
  39. 'content': ''})
  40. return results