springer.py 2.0 KB

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