ccengine.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Creative Commons search engine (Images)
  4. """
  5. from json import loads
  6. from urllib.parse import urlencode
  7. about = {
  8. "website": 'https://search.creativecommons.org/',
  9. "wikidata_id": None,
  10. "official_api_documentation": 'https://api.creativecommons.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.creativecommons.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(
  22. query=urlencode({'q': query}),
  23. nb_per_page=nb_per_page,
  24. page=params['pageno'])
  25. params['url'] = base_url + search_path
  26. return params
  27. def response(resp):
  28. results = []
  29. json_data = loads(resp.text)
  30. for result in json_data['results']:
  31. results.append({'url': result['foreign_landing_url'],
  32. 'title': result['title'],
  33. 'img_src': result['url'],
  34. 'template': 'images.html'})
  35. return results