springer.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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({'q': query, 's': nb_per_page * (params['pageno'] - 1), 'p': nb_per_page, 'api_key': api_key})
  26. params['url'] = base_url + args
  27. logger.debug("query_url --> %s", params['url'])
  28. return params
  29. def response(resp):
  30. results = []
  31. json_data = loads(resp.text)
  32. for record in json_data['records']:
  33. content = record['abstract'][0:500]
  34. if len(record['abstract']) > len(content):
  35. content += "..."
  36. published = datetime.strptime(record['publicationDate'], '%Y-%m-%d')
  37. metadata = [
  38. record[x]
  39. for x in [
  40. 'publicationName',
  41. 'identifier',
  42. 'contentType',
  43. ]
  44. if record.get(x) is not None
  45. ]
  46. metadata = ' / '.join(metadata)
  47. if record.get('startingPage') and record.get('endingPage') is not None:
  48. metadata += " (%(startingPage)s-%(endingPage)s)" % record
  49. results.append(
  50. {
  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. )
  58. return results