springer.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Springer Nature (science)
  4. """
  5. from datetime import datetime
  6. from json import loads
  7. from urllib.parse import urlencode
  8. from searx.exceptions import SearxEngineAPIException
  9. about = {
  10. "website": 'https://www.springernature.com/',
  11. "wikidata_id": 'Q21096327',
  12. "official_api_documentation": 'https://dev.springernature.com/',
  13. "use_official_api": True,
  14. "require_api_key": True,
  15. "results": 'JSON',
  16. }
  17. categories = ['science']
  18. paging = True
  19. nb_per_page = 10
  20. api_key = 'unset'
  21. base_url = 'https://api.springernature.com/metadata/json?'
  22. def request(query, params):
  23. if api_key == 'unset':
  24. raise SearxEngineAPIException('missing Springer-Nature API key')
  25. args = urlencode({
  26. 'q' : query,
  27. 's' : nb_per_page * (params['pageno'] - 1),
  28. 'p' : nb_per_page,
  29. 'api_key' : api_key
  30. })
  31. params['url'] = base_url + args
  32. logger.debug("query_url --> %s", params['url'])
  33. return params
  34. def response(resp):
  35. results = []
  36. json_data = loads(resp.text)
  37. for record in json_data['records']:
  38. content = record['abstract'][0:500]
  39. if len(record['abstract']) > len(content):
  40. content += "..."
  41. published = datetime.strptime(record['publicationDate'], '%Y-%m-%d')
  42. metadata = [record[x] for x in [
  43. 'publicationName',
  44. 'identifier',
  45. 'contentType',
  46. ] if record.get(x) is not None]
  47. metadata = ' / '.join(metadata)
  48. if record.get('startingPage') and record.get('endingPage') is not None:
  49. metadata += " (%(startingPage)s-%(endingPage)s)" % record
  50. results.append({
  51. 'title': record['title'],
  52. 'url': record['url'][0]['value'].replace('http://', 'https://', 1),
  53. 'content' : content,
  54. 'publishedDate' : published,
  55. 'metadata' : metadata
  56. })
  57. return results