-
Notifications
You must be signed in to change notification settings - Fork 35
/
SemanticScholar.py
490 lines (383 loc) · 15.6 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import warnings
from typing import List
from semanticscholar.ApiRequester import ApiRequester
from semanticscholar.Author import Author
from semanticscholar.BaseReference import BaseReference
from semanticscholar.Citation import Citation
from semanticscholar.PaginatedResults import PaginatedResults
from semanticscholar.Paper import Paper
from semanticscholar.Reference import Reference
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/graph/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 api_key:
self.auth_header = {'x-api-key': api_key}
if not api_url:
self.api_url = self.DEFAULT_PARTNER_API_URL
if not graph_api:
warnings.warn(
'graph_api parameter is deprecated and will be disabled ' +
'in the future', DeprecationWarning)
self.api_url = self.api_url.replace('/graph', '')
self._timeout = timeout
self._requester = ApiRequester(self._timeout)
@property
def timeout(self) -> int:
'''
:type: :class:`int`
'''
return self._timeout
@timeout.setter
def timeout(self, timeout: int) -> None:
'''
:param int timeout:
'''
self._timeout = timeout
self._requester.timeout = timeout
def get_paper(
self,
paper_id: str,
include_unknown_refs: bool = False,
fields: list = None
) -> Paper:
'''Paper lookup
:calls: `GET /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
:rtype: :class:`semanticscholar.Paper.Paper`
:raises: ObjectNotFoundExeception: if Paper ID not found.
'''
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:
warnings.warn(
'include_unknown_refs parameter is deprecated and will be disabled ' +
'in the future', DeprecationWarning)
parameters += '&include_unknown_references=true'
data = self._requester.get_data(url, parameters, self.auth_header)
paper = Paper(data)
return paper
def get_papers(
self,
paper_ids: List[str],
fields: list = None
) -> List[Paper]:
'''Get details for multiple papers at once
:calls: `POST /paper/batch <https://api.semanticscholar.org/api-docs/\
graph#tag/Paper-Data/operation/post_graph_get_papers>`_
:param str paper_ids: list of IDs (must be <= 1000) - S2PaperId,\
CorpusId, DOI, ArXivId, MAG, ACL, PMID, PMCID, or URL from:
- semanticscholar.org
- arxiv.org
- aclweb.org
- acm.org
- biorxiv.org
:param list fields: (optional) list of the fields to be returned.
:returns: papers data
:rtype: :class:`List` of :class:`semanticscholar.Paper.Paper`
:raises: BadQueryParametersException: if no paper was found.
'''
if not fields:
fields = Paper.SEARCH_FIELDS
url = f'{self.api_url}/paper/batch'
fields = ','.join(fields)
parameters = f'&fields={fields}'
payload = { "ids": paper_ids }
data = self._requester.get_data(
url, parameters, self.auth_header, payload)
papers = [Paper(item) for item in data]
return papers
def get_paper_authors(
self,
paper_id: str,
fields: list = None,
limit: int = 1000
) -> PaginatedResults:
'''Get details about a paper's authors
:calls: `POST /paper/{paper_id}/authors \
<https://api.semanticscholar.org/api-docs/graph#tag/Paper-Data\
/operation/get_graph_get_paper_authors>`_
: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 list fields: (optional) list of the fields to be returned.
:param int limit: (optional) maximum number of results to return\
(must be <= 1000).
'''
if limit < 1 or limit > 1000:
raise ValueError(
'The limit parameter must be between 1 and 1000 inclusive.')
if not fields:
fields = [item for item in Author.SEARCH_FIELDS
if not item.startswith('papers')]
url = f'{self.api_url}/paper/{paper_id}/authors'
results = PaginatedResults(
requester=self._requester,
data_type=Author,
url=url,
fields=fields,
limit=limit
)
return results
def get_paper_citations(
self,
paper_id: str,
fields: list = None,
limit: int = 1000
) -> PaginatedResults:
'''Get details about a paper's citations
:calls: `POST /paper/{paper_id}/citations \
<https://api.semanticscholar.org/api-docs/graph#tag/Paper-Data\
/operation/get_graph_get_paper_citations>`_
: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 list fields: (optional) list of the fields to be returned.
:param int limit: (optional) maximum number of results to return\
(must be <= 1000).
'''
if limit < 1 or limit > 1000:
raise ValueError(
'The limit parameter must be between 1 and 1000 inclusive.')
if not fields:
fields = BaseReference.FIELDS + Paper.SEARCH_FIELDS
url = f'{self.api_url}/paper/{paper_id}/citations'
results = PaginatedResults(
requester=self._requester,
data_type=Citation,
url=url,
fields=fields,
limit=limit
)
return results
def get_paper_references(
self,
paper_id: str,
fields: list = None,
limit: int = 1000
) -> PaginatedResults:
'''Get details about a paper's references
:calls: `POST /paper/{paper_id}/references \
<https://api.semanticscholar.org/api-docs/graph#tag/Paper-Data\
/operation/get_graph_get_paper_references>`_
: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 list fields: (optional) list of the fields to be returned.
:param int limit: (optional) maximum number of results to return\
(must be <= 1000).
'''
if limit < 1 or limit > 1000:
raise ValueError(
'The limit parameter must be between 1 and 1000 inclusive.')
if not fields:
fields = BaseReference.FIELDS + Paper.SEARCH_FIELDS
url = f'{self.api_url}/paper/{paper_id}/references'
results = PaginatedResults(
requester=self._requester,
data_type=Reference,
url=url,
fields=fields,
limit=limit
)
return results
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 /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:`semanticscholar.PaginatedResults.PaginatedResults`
'''
if limit < 1 or limit > 100:
raise ValueError(
'The limit parameter must be between 1 and 100 inclusive.')
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 /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
:rtype: :class:`semanticscholar.Author.Author`
:raises: ObjectNotFoundExeception: if Author ID not found.
'''
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 get_authors(
self,
author_ids: List[str],
fields: list = None
) -> List[Author]:
'''Get details for multiple authors at once
:calls: `POST /author/batch <https://api.semanticscholar.org/api-docs/\
graph#tag/Author-Data/operation/get_graph_get_author>`_
:param str author_ids: list of S2AuthorId (must be <= 1000).
:returns: author data
:rtype: :class:`List` of :class:`semanticscholar.Author.Author`
:raises: BadQueryParametersException: if no author was found.
'''
if not fields:
fields = Author.SEARCH_FIELDS
url = f'{self.api_url}/author/batch'
fields = ','.join(fields)
parameters = f'&fields={fields}'
payload = { "ids": author_ids }
data = self._requester.get_data(
url, parameters, self.auth_header, payload)
authors = [Author(item) for item in data]
return authors
def get_author_papers(
self,
author_id: str,
fields: list = None,
limit: int = 1000
) -> PaginatedResults:
'''Get details about a author's papers
:calls: `POST /paper/{author_id}/papers \
<https://api.semanticscholar.org/api-docs/graph#tag/Paper-Data\
/operation/get_graph_get_author_papers>`_
: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 list fields: (optional) list of the fields to be returned.
:param int limit: (optional) maximum number of results to return\
(must be <= 1000).
'''
if limit < 1 or limit > 1000:
raise ValueError(
'The limit parameter must be between 1 and 1000 inclusive.')
if not fields:
fields = Paper.SEARCH_FIELDS
url = f'{self.api_url}/author/{author_id}/papers'
results = PaginatedResults(
requester=self._requester,
data_type=Paper,
url=url,
fields=fields,
limit=limit
)
return results
def search_author(
self,
query: str,
fields: list = None,
limit: int = 1000
) -> PaginatedResults:
'''Search for authors by name
:calls: `GET /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:`semanticscholar.PaginatedResults.PaginatedResults`
'''
if limit < 1 or limit > 1000:
raise ValueError(
'The limit parameter must be between 1 and 1000 inclusive.')
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