springer.py 2.4 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', 'scientific publications']
  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. published = datetime.strptime(record['publicationDate'], '%Y-%m-%d')
  34. authors = [" ".join(author['creator'].split(', ')[::-1]) for author in record['creators']]
  35. tags = record.get('genre')
  36. if isinstance(tags, str):
  37. tags = [tags]
  38. results.append(
  39. {
  40. 'template': 'paper.html',
  41. 'url': record['url'][0]['value'].replace('http://', 'https://', 1),
  42. 'title': record['title'],
  43. 'content': record['abstract'],
  44. 'comments': record['publicationName'],
  45. 'tags': tags,
  46. 'publishedDate': published,
  47. 'type': record.get('contentType'),
  48. 'authors': authors,
  49. # 'editor': '',
  50. 'publisher': record.get('publisher'),
  51. 'journal': record.get('publicationName'),
  52. 'volume': record.get('volume') or None,
  53. 'pages': '-'.join([x for x in [record.get('startingPage'), record.get('endingPage')] if x]),
  54. 'number': record.get('number') or None,
  55. 'doi': record.get('doi'),
  56. 'issn': [x for x in [record.get('issn')] if x],
  57. 'isbn': [x for x in [record.get('isbn')] if x],
  58. # 'pdf_url' : ''
  59. }
  60. )
  61. return results