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