| 
									
										
										
										
											2021-03-27 16:50:03 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2022-03-04 22:00:59 +01:00
										 |  |  | # lint: pylint | 
					
						
							|  |  |  | """Semantic Scholar (Science)
 | 
					
						
							| 
									
										
										
										
											2021-03-27 16:50:03 +01:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from json import dumps, loads | 
					
						
							| 
									
										
										
										
											2022-03-04 22:00:59 +01:00
										 |  |  | from datetime import datetime | 
					
						
							| 
									
										
										
										
											2021-03-27 16:50:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | from flask_babel import gettext | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-04 22:00:59 +01:00
										 |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://www.semanticscholar.org/', | 
					
						
							|  |  |  |     "wikidata_id": 'Q22908627', | 
					
						
							|  |  |  |     "official_api_documentation": 'https://api.semanticscholar.org/', | 
					
						
							|  |  |  |     "use_official_api": True, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'JSON', | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-27 16:50:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | categories = ['science', 'scientific publications'] | 
					
						
							| 
									
										
										
										
											2022-03-04 22:00:59 +01:00
										 |  |  | paging = True | 
					
						
							| 
									
										
										
										
											2021-03-27 16:50:03 +01:00
										 |  |  | search_url = 'https://www.semanticscholar.org/api/1/search' | 
					
						
							| 
									
										
										
										
											2022-03-04 22:00:59 +01:00
										 |  |  | paper_url = 'https://www.semanticscholar.org/paper' | 
					
						
							| 
									
										
										
										
											2021-03-27 16:50:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							|  |  |  |     params['url'] = search_url | 
					
						
							|  |  |  |     params['method'] = 'POST' | 
					
						
							|  |  |  |     params['headers']['content-type'] = 'application/json' | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     params['data'] = dumps( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "queryString": query, | 
					
						
							|  |  |  |             "page": params['pageno'], | 
					
						
							|  |  |  |             "pageSize": 10, | 
					
						
							|  |  |  |             "sort": "relevance", | 
					
						
							|  |  |  |             "useFallbackRankerService": False, | 
					
						
							|  |  |  |             "useFallbackSearchCluster": False, | 
					
						
							|  |  |  |             "getQuerySuggestions": False, | 
					
						
							|  |  |  |             "authors": [], | 
					
						
							|  |  |  |             "coAuthors": [], | 
					
						
							|  |  |  |             "venues": [], | 
					
						
							|  |  |  |             "performTitleMatch": True, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2021-03-27 16:50:03 +01:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     res = loads(resp.text) | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  |     for result in res['results']: | 
					
						
							| 
									
										
										
										
											2022-03-04 22:00:59 +01:00
										 |  |  |         url = result.get('primaryPaperLink', {}).get('url') | 
					
						
							|  |  |  |         if not url and result.get('links'): | 
					
						
							|  |  |  |             url = result.get('links')[0] | 
					
						
							|  |  |  |         if not url: | 
					
						
							|  |  |  |             alternatePaperLinks = result.get('alternatePaperLinks') | 
					
						
							|  |  |  |             if alternatePaperLinks: | 
					
						
							|  |  |  |                 url = alternatePaperLinks[0].get('url') | 
					
						
							|  |  |  |         if not url: | 
					
						
							|  |  |  |             url = paper_url + '/%s' % result['id'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |         # publishedDate | 
					
						
							|  |  |  |         if 'pubDate' in result: | 
					
						
							|  |  |  |             publishedDate = datetime.strptime(result['pubDate'], "%Y-%m-%d") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             publishedDate = None | 
					
						
							| 
									
										
										
										
											2022-03-04 22:00:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |         # authors | 
					
						
							|  |  |  |         authors = [author[0]['name'] for author in result.get('authors', [])] | 
					
						
							| 
									
										
										
										
											2022-03-04 22:00:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |         # pick for the first alternate link, but not from the crawler | 
					
						
							|  |  |  |         pdf_url = None | 
					
						
							|  |  |  |         for doc in result.get('alternatePaperLinks', []): | 
					
						
							| 
									
										
										
										
											2022-09-23 19:58:14 +02:00
										 |  |  |             if doc['linkType'] not in ('crawler', 'doi'): | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |                 pdf_url = doc['url'] | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2022-03-04 22:00:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |         # comments | 
					
						
							|  |  |  |         comments = None | 
					
						
							|  |  |  |         if 'citationStats' in result: | 
					
						
							|  |  |  |             comments = gettext( | 
					
						
							|  |  |  |                 '{numCitations} citations from the year {firstCitationVelocityYear} to {lastCitationVelocityYear}' | 
					
						
							|  |  |  |             ).format( | 
					
						
							|  |  |  |                 numCitations=result['citationStats']['numCitations'], | 
					
						
							|  |  |  |                 firstCitationVelocityYear=result['citationStats']['firstCitationVelocityYear'], | 
					
						
							|  |  |  |                 lastCitationVelocityYear=result['citationStats']['lastCitationVelocityYear'], | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-03-04 22:00:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |         results.append( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 'template': 'paper.html', | 
					
						
							|  |  |  |                 'url': url, | 
					
						
							|  |  |  |                 'title': result['title']['text'], | 
					
						
							|  |  |  |                 'content': result['paperAbstract']['text'], | 
					
						
							|  |  |  |                 'journal': result.get('venue', {}).get('text') or result.get('journal', {}).get('name'), | 
					
						
							|  |  |  |                 'doi': result.get('doiInfo', {}).get('doi'), | 
					
						
							|  |  |  |                 'tags': result.get('fieldsOfStudy'), | 
					
						
							|  |  |  |                 'authors': authors, | 
					
						
							|  |  |  |                 'pdf_url': pdf_url, | 
					
						
							|  |  |  |                 'publishedDate': publishedDate, | 
					
						
							|  |  |  |                 'comments': comments, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-03-27 16:50:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return results |