| 
									
										
										
										
											2021-03-17 16:43:09 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | """Springer Nature (science)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from datetime import datetime | 
					
						
							|  |  |  | from json import loads | 
					
						
							|  |  |  | from urllib.parse import urlencode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from searx.exceptions import SearxEngineAPIException | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://www.springernature.com/', | 
					
						
							|  |  |  |     "wikidata_id": 'Q21096327', | 
					
						
							|  |  |  |     "official_api_documentation": 'https://dev.springernature.com/', | 
					
						
							|  |  |  |     "use_official_api": True, | 
					
						
							|  |  |  |     "require_api_key": True, | 
					
						
							|  |  |  |     "results": 'JSON', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | categories = ['science', 'scientific publications'] | 
					
						
							| 
									
										
										
										
											2021-03-17 16:43:09 +01:00
										 |  |  | paging = True | 
					
						
							|  |  |  | nb_per_page = 10 | 
					
						
							|  |  |  | api_key = 'unset' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | base_url = 'https://api.springernature.com/metadata/json?' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 16:43:09 +01:00
										 |  |  | def request(query, params): | 
					
						
							|  |  |  |     if api_key == 'unset': | 
					
						
							|  |  |  |         raise SearxEngineAPIException('missing Springer-Nature API key') | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     args = urlencode({'q': query, 's': nb_per_page * (params['pageno'] - 1), 'p': nb_per_page, 'api_key': api_key}) | 
					
						
							| 
									
										
										
										
											2021-03-17 16:43:09 +01:00
										 |  |  |     params['url'] = base_url + args | 
					
						
							|  |  |  |     logger.debug("query_url --> %s", params['url']) | 
					
						
							|  |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  |     json_data = loads(resp.text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for record in json_data['records']: | 
					
						
							|  |  |  |         published = datetime.strptime(record['publicationDate'], '%Y-%m-%d') | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |         authors = [" ".join(author['creator'].split(', ')[::-1]) for author in record['creators']] | 
					
						
							|  |  |  |         tags = record.get('genre') | 
					
						
							|  |  |  |         if isinstance(tags, str): | 
					
						
							|  |  |  |             tags = [tags] | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         results.append( | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |                 'template': 'paper.html', | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |                 'url': record['url'][0]['value'].replace('http://', 'https://', 1), | 
					
						
							| 
									
										
										
										
											2022-09-25 14:57:02 +02:00
										 |  |  |                 'title': record['title'], | 
					
						
							|  |  |  |                 'content': record['abstract'], | 
					
						
							|  |  |  |                 'comments': record['publicationName'], | 
					
						
							|  |  |  |                 'tags': tags, | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |                 'publishedDate': published, | 
					
						
							| 
									
										
										
										
											2022-09-25 14:57:02 +02:00
										 |  |  |                 'type': record.get('contentType'), | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |                 'authors': authors, | 
					
						
							| 
									
										
										
										
											2022-09-25 14:57:02 +02:00
										 |  |  |                 # 'editor': '', | 
					
						
							|  |  |  |                 'publisher': record.get('publisher'), | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |                 'journal': record.get('publicationName'), | 
					
						
							|  |  |  |                 'volume': record.get('volume') or None, | 
					
						
							| 
									
										
										
										
											2022-09-25 14:57:02 +02:00
										 |  |  |                 'pages': '-'.join([x for x in [record.get('startingPage'), record.get('endingPage')] if x]), | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |                 'number': record.get('number') or None, | 
					
						
							| 
									
										
										
										
											2022-09-25 14:57:02 +02:00
										 |  |  |                 'doi': record.get('doi'), | 
					
						
							|  |  |  |                 'issn': [x for x in [record.get('issn')] if x], | 
					
						
							|  |  |  |                 'isbn': [x for x in [record.get('isbn')] if x], | 
					
						
							|  |  |  |                 # 'pdf_url' : '' | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-03-17 16:43:09 +01:00
										 |  |  |     return results |