unsplash.py 1.6 KB

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