.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
semantic_scholar.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Semantic Scholar (Science)
3"""
4
5from json import dumps, loads
6from datetime import datetime
7
8from flask_babel import gettext
9
10about = {
11 "website": 'https://www.semanticscholar.org/',
12 "wikidata_id": 'Q22908627',
13 "official_api_documentation": 'https://api.semanticscholar.org/',
14 "use_official_api": True,
15 "require_api_key": False,
16 "results": 'JSON',
17}
18
19categories = ['science', 'scientific publications']
20paging = True
21search_url = 'https://www.semanticscholar.org/api/1/search'
22paper_url = 'https://www.semanticscholar.org/paper'
23
24
25def request(query, params):
26 params['url'] = search_url
27 params['method'] = 'POST'
28 params['headers']['content-type'] = 'application/json'
29 params['data'] = dumps(
30 {
31 "queryString": query,
32 "page": params['pageno'],
33 "pageSize": 10,
34 "sort": "relevance",
35 "getQuerySuggestions": False,
36 "authors": [],
37 "coAuthors": [],
38 "venues": [],
39 "performTitleMatch": True,
40 }
41 )
42 return params
43
44
45def response(resp):
46 res = loads(resp.text)
47 results = []
48 for result in res['results']:
49 url = result.get('primaryPaperLink', {}).get('url')
50 if not url and result.get('links'):
51 url = result.get('links')[0]
52 if not url:
53 alternatePaperLinks = result.get('alternatePaperLinks')
54 if alternatePaperLinks:
55 url = alternatePaperLinks[0].get('url')
56 if not url:
57 url = paper_url + '/%s' % result['id']
58
59 # publishedDate
60 if 'pubDate' in result:
61 publishedDate = datetime.strptime(result['pubDate'], "%Y-%m-%d")
62 else:
63 publishedDate = None
64
65 # authors
66 authors = [author[0]['name'] for author in result.get('authors', [])]
67
68 # pick for the first alternate link, but not from the crawler
69 pdf_url = None
70 for doc in result.get('alternatePaperLinks', []):
71 if doc['linkType'] not in ('crawler', 'doi'):
72 pdf_url = doc['url']
73 break
74
75 # comments
76 comments = None
77 if 'citationStats' in result:
78 comments = gettext(
79 '{numCitations} citations from the year {firstCitationVelocityYear} to {lastCitationVelocityYear}'
80 ).format(
81 numCitations=result['citationStats']['numCitations'],
82 firstCitationVelocityYear=result['citationStats']['firstCitationVelocityYear'],
83 lastCitationVelocityYear=result['citationStats']['lastCitationVelocityYear'],
84 )
85
86 results.append(
87 {
88 'template': 'paper.html',
89 'url': url,
90 'title': result['title']['text'],
91 'content': result['paperAbstract']['text'],
92 'journal': result.get('venue', {}).get('text') or result.get('journal', {}).get('name'),
93 'doi': result.get('doiInfo', {}).get('doi'),
94 'tags': result.get('fieldsOfStudy'),
95 'authors': authors,
96 'pdf_url': pdf_url,
97 'publishedDate': publishedDate,
98 'comments': comments,
99 }
100 )
101
102 return results