springer.py 2.0 KB

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