openverse.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Openverse (formerly known as: Creative Commons search engine) [Images]
  4. """
  5. from json import loads
  6. from urllib.parse import urlencode
  7. about = {
  8. "website": 'https://wordpress.org/openverse/',
  9. "wikidata_id": None,
  10. "official_api_documentation": 'https://api.openverse.engineering/v1/',
  11. "use_official_api": True,
  12. "require_api_key": False,
  13. "results": 'JSON',
  14. }
  15. categories = ['images']
  16. paging = True
  17. nb_per_page = 20
  18. base_url = 'https://api.openverse.engineering/v1/images/'
  19. search_string = '?page={page}&page_size={nb_per_page}&format=json&{query}'
  20. def request(query, params):
  21. search_path = search_string.format(query=urlencode({'q': query}), nb_per_page=nb_per_page, page=params['pageno'])
  22. params['url'] = base_url + search_path
  23. return params
  24. def response(resp):
  25. results = []
  26. json_data = loads(resp.text)
  27. for result in json_data['results']:
  28. results.append(
  29. {
  30. 'url': result['foreign_landing_url'],
  31. 'title': result['title'],
  32. 'img_src': result['url'],
  33. 'template': 'images.html',
  34. }
  35. )
  36. return results