unsplash.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Unsplash
  4. """
  5. from urllib.parse import urlencode, urlparse, urlunparse, parse_qsl
  6. from json import loads
  7. # about
  8. about = {
  9. "website": 'https://unsplash.com',
  10. "wikidata_id": 'Q28233552',
  11. "official_api_documentation": 'https://unsplash.com/developers',
  12. "use_official_api": False,
  13. "require_api_key": False,
  14. "results": 'JSON',
  15. }
  16. base_url = 'https://unsplash.com/'
  17. search_url = base_url + 'napi/search/photos?'
  18. categories = ['images']
  19. page_size = 20
  20. paging = True
  21. def clean_url(url):
  22. parsed = urlparse(url)
  23. query = [(k, v) for (k, v) in parse_qsl(parsed.query) if k not in ['ixid', 's']]
  24. return urlunparse((parsed.scheme, parsed.netloc, parsed.path, parsed.params, urlencode(query), parsed.fragment))
  25. def request(query, params):
  26. params['url'] = search_url + urlencode({'query': query, 'page': params['pageno'], 'per_page': page_size})
  27. logger.debug("query_url --> %s", params['url'])
  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(
  35. {
  36. 'template': 'images.html',
  37. 'url': clean_url(result['links']['html']),
  38. 'thumbnail_src': clean_url(result['urls']['thumb']),
  39. 'img_src': clean_url(result['urls']['raw']),
  40. 'title': result.get('alt_description') or 'unknown',
  41. 'content': result.get('description') or '',
  42. }
  43. )
  44. return results