springer.py 2.0 KB

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