springer.py 2.4 KB

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