-
Notifications
You must be signed in to change notification settings - Fork 35
/
SemanticScholar.py
267 lines (208 loc) · 8.32 KB
/
SemanticScholar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import warnings
from semanticscholar.ApiRequester import ApiRequester
from semanticscholar.Author import Author
from semanticscholar.PaginatedResults import PaginatedResults
from semanticscholar.Paper import Paper
class SemanticScholar:
'''
Main class to retrieve data from Semantic Scholar Graph API
'''
DEFAULT_API_URL = 'https://api.semanticscholar.org/graph/v1'
DEFAULT_PARTNER_API_URL = 'https://partner.semanticscholar.org/v1'
auth_header = {}
def __init__(
self,
timeout: int = 10,
api_key: str = None,
api_url: str = None,
graph_api: bool = True
) -> None:
'''
:param float timeout: (optional) an exception is raised
if the server has not issued a response for timeout seconds.
:param str api_key: (optional) private API key.
:param str api_url: (optional) custom API url.
:param bool graph_api: (optional) whether use new Graph API.
'''
if api_url:
self.api_url = api_url
else:
self.api_url = self.DEFAULT_API_URL
if not graph_api:
self.api_url = self.api_url.replace('/graph', '')
if api_key:
self.auth_header = {'x-api-key': api_key}
if not api_url:
self.api_url = self.DEFAULT_PARTNER_API_URL
self._timeout = timeout
self._requester = ApiRequester(self._timeout)
def get_timeout(self) -> int:
'''
:type: :class:`int`
'''
return self._timeout
def set_timeout(self, timeout: int):
'''
:param int timeout:
'''
self._timeout = timeout
self._requester.timeout = timeout
timeout = property(get_timeout, set_timeout)
def get_paper(
self,
paper_id: str,
include_unknown_refs: bool = False,
fields: list = None
) -> Paper:
'''Paper lookup
:calls: `GET https://api.semanticscholar.org/graph/v1/paper/{paper_id} \
<https://api.semanticscholar.org/api-docs/graph#tag/Paper-Data/ \
operation/get_graph_get_paper>`_
:param str paper_id: S2PaperId, CorpusId, DOI, ArXivId, MAG, ACL, \
PMID, PMCID, or URL from:
- semanticscholar.org
- arxiv.org
- aclweb.org
- acm.org
- biorxiv.org
:param bool include_unknown_refs: (optional) include non referenced \
paper.
:param list fields: (optional) list of the fields to be returned.
:returns: paper data or empty :class:`dict` if not found.
:rtype: :class:`dict`
'''
if not fields:
fields = Paper.FIELDS
url = f'{self.api_url}/paper/{paper_id}'
fields = ','.join(fields)
parameters = f'&fields={fields}'
if include_unknown_refs:
parameters += '&include_unknown_references=true'
data = self._requester.get_data(url, parameters, self.auth_header)
paper = Paper(data)
return paper
def search_paper(
self,
query: str,
year: str = None,
fields_of_study: list = None,
fields: list = None,
limit: int = 100
) -> PaginatedResults:
'''Search for papers by keyword
:calls: `GET https://api.semanticscholar.org/graph/v1/paper/search \
<https://api.semanticscholar.org/api-docs/graph#tag/Paper-Data/ \
operation/get_graph_get_paper_search>`_
:param str query: plain-text search query string.
:param str year: restrict results to the given range of \
publication year.
:param str fields_of_study: restrict results to given field-of-study, \
using the s2FieldsOfStudy paper field.
:param list fields: (optional) list of the fields to be returned.
:param int limit: (optional) maximum number of results to return \
(must be <= 100).
:returns: query results.
:rtype: :class:`PaginatedResults`
'''
if not fields:
fields = Paper.SEARCH_FIELDS
url = f'{self.api_url}/paper/search'
query += f'&year={year}' if year else ''
if fields_of_study:
fields_of_study = ','.join(fields_of_study)
query += f'&fields_of_study={fields_of_study}'
results = PaginatedResults(
self._requester,
Paper,
url,
query,
fields,
limit,
self.auth_header
)
return results
def get_author(self, author_id: str, fields: list = None) -> Author:
'''Author lookup
:calls: `GET https://api.semanticscholar.org/graph/v1/author/\
{author_id} <https://api.semanticscholar.org/api-docs/\
graph#tag/Author-Data/operation/get_graph_get_author>`_
:param str author_id: S2AuthorId.
:returns: author data or empty :class:`dict` if not found.
:rtype: :class:`dict`
'''
if not fields:
fields = Author.FIELDS
url = f'{self.api_url}/author/{author_id}'
fields = ','.join(fields)
parameters = f'&fields={fields}'
data = self._requester.get_data(url, parameters, self.auth_header)
author = Author(data)
return author
def search_author(
self,
query: str,
fields: list = None,
limit: int = 1000
) -> PaginatedResults:
'''Search for authors by name
:calls: `GET https://api.semanticscholar.org/graph/v1/author/search\
<https://api.semanticscholar.org/api-docs/graph#tag/Author-Data/\
operation/get_graph_get_author_search>`_
:param str query: plain-text search query string.
:param list fields: (optional) list of the fields to be returned.
:param int limit: (optional) maximum number of results to return \
(must be <= 1000).
:returns: query results.
:rtype: :class:`PaginatedResults`
'''
if not fields:
fields = Author.SEARCH_FIELDS
url = f'{self.api_url}/author/search'
results = PaginatedResults(
self._requester,
Author,
url,
query,
fields,
limit,
self.auth_header
)
return results
def paper(self, paper_id: str, include_unknown_refs: bool = False) -> dict:
'''Paper lookup
:calls: `GET https://api.semanticscholar.org/graph/v1/paper/{paper_id} \
<https://api.semanticscholar.org/api-docs/graph#tag/Paper-Data/ \
operation/get_graph_get_paper>`_
:param str paper_id: S2PaperId, DOI or ArXivId.
:param float timeout: an exception is raised \
if the server has not issued a response for timeout seconds.
:param bool include_unknown_refs: (optional) include non \
referenced paper.
:returns: paper data or empty :class:`dict` if not found.
:rtype: :class:`dict`
.. deprecated:: 0.3.0
Use :func:`get_paper` instead
'''
warnings.warn(
"paper() is deprecated and will be disabled in the future," +
" use get_paper() instead.",
DeprecationWarning)
data = self.get_paper(paper_id, include_unknown_refs)
return data.raw_data
def author(self, paper_id: str) -> dict:
'''Author lookup
:calls: `GET https://api.semanticscholar.org/graph/v1/author/\
{author_id} <https://api.semanticscholar.org/api-docs/\
graph#tag/Author-Data/operation/get_graph_get_author>`_
:param str paper_id: S2AuthorId.
:returns: author data or empty :class:`dict` if not found.
:rtype: :class:`dict`
.. deprecated:: 0.3.0
Use :func:`get_author` instead
'''
warnings.warn(
"author() is deprecated and will be disabled in the future," +
" use get_author() instead.",
DeprecationWarning)
data = self.get_author(paper_id)
return data.raw_data