-
Notifications
You must be signed in to change notification settings - Fork 35
/
AsyncSemanticScholar.py
623 lines (488 loc) · 20 KB
/
AsyncSemanticScholar.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
from typing import List, Literal
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 AsyncSemanticScholar:
'''
Main class to retrieve data from Semantic Scholar Graph API asynchronously.
'''
DEFAULT_API_URL = 'https://api.semanticscholar.org'
BASE_PATH_GRAPH = '/graph/v1'
BASE_PATH_RECOMMENDATIONS = '/recommendations/v1'
auth_header = {}
def __init__(
self,
timeout: int = 10,
api_key: str = None,
api_url: str = None,
debug: bool = False
) -> 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 debug: (optional) enable debug mode.
'''
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}
self._timeout = timeout
self._debug = debug
self._requester = ApiRequester(self._timeout, self._debug)
@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
@property
def debug(self) -> bool:
'''
:type: :class:`bool`
'''
return self._debug
@debug.setter
def debug(self, debug: bool) -> None:
'''
:param bool debug:
'''
self._debug = debug
self._requester.debug = debug
async def get_paper(
self,
paper_id: str,
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 list fields: (optional) list of the fields to be returned.
:returns: paper data
:rtype: :class:`semanticscholar.Paper.Paper`
:raises: ObjectNotFoundException: if Paper ID not found.
'''
if not fields:
fields = Paper.FIELDS
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f'{base_url}/paper/{paper_id}'
fields = ','.join(fields)
parameters = f'&fields={fields}'
data = await self._requester.get_data_async(url, parameters, self.auth_header)
paper = Paper(data)
return paper
async 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
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f'{base_url}/paper/batch'
fields = ','.join(fields)
parameters = f'&fields={fields}'
payload = { "ids": paper_ids }
data = await self._requester.get_data_async(
url, parameters, self.auth_header, payload)
papers = [Paper(item) for item in data]
return papers
async 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')]
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f'{base_url}/paper/{paper_id}/authors'
results = await PaginatedResults.create(
requester=self._requester,
data_type=Author,
url=url,
fields=fields,
limit=limit
)
return results
async 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
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f'{base_url}/paper/{paper_id}/citations'
results = await PaginatedResults.create(
requester=self._requester,
data_type=Citation,
url=url,
fields=fields,
limit=limit
)
return results
async 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
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f'{base_url}/paper/{paper_id}/references'
results = await PaginatedResults.create(
requester=self._requester,
data_type=Reference,
url=url,
fields=fields,
limit=limit
)
return results
async def search_paper(
self,
query: str,
year: str = None,
publication_types: list = None,
open_access_pdf: bool = None,
venue: list = 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: (optional) restrict results to the given range of \
publication year.
:param list publication_type: (optional) restrict results to the given \
publication type list.
:param bool open_access_pdf: (optional) restrict results to papers \
with public PDFs.
:param list venue: (optional) restrict results to the given venue list.
:param list fields_of_study: (optional) restrict results to given \
field-of-study list, 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
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f'{base_url}/paper/search'
query += f'&year={year}' if year else ''
if publication_types:
publication_types = ','.join(publication_types)
query += f'&publicationTypes={publication_types}'
query += '&openAccessPdf' if open_access_pdf else ''
if venue:
venue = ','.join(venue)
query += f'&venue={venue}'
if fields_of_study:
fields_of_study = ','.join(fields_of_study)
query += f'&fieldsOfStudy={fields_of_study}'
results = await PaginatedResults.create(
self._requester,
Paper,
url,
query,
fields,
limit,
self.auth_header,
max_results=1000
)
return results
async 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: ObjectNotFoundException: if Author ID not found.
'''
if not fields:
fields = Author.FIELDS
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f'{base_url}/author/{author_id}'
fields = ','.join(fields)
parameters = f'&fields={fields}'
data = await self._requester.get_data_async(url, parameters, self.auth_header)
author = Author(data)
return author
async 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
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f'{base_url}/author/batch'
fields = ','.join(fields)
parameters = f'&fields={fields}'
payload = { "ids": author_ids }
data = await self._requester.get_data_async(
url, parameters, self.auth_header, payload)
authors = [Author(item) for item in data]
return authors
async 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
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f'{base_url}/author/{author_id}/papers'
results = await PaginatedResults.create(
requester=self._requester,
data_type=Paper,
url=url,
fields=fields,
limit=limit
)
return results
async 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
base_url = self.api_url + self.BASE_PATH_GRAPH
url = f'{base_url}/author/search'
results = await PaginatedResults.create(
self._requester,
Author,
url,
query,
fields,
limit,
self.auth_header,
max_results=1000
)
return results
async def get_recommended_papers(
self,
paper_id: str,
fields: list = None,
limit: int = 100,
pool_from: Literal["recent", "all-cs"] = "recent"
) -> List[Paper]:
'''Get recommended papers for a single positive example.
:calls: `GET /recommendations/v1/papers/forpaper/{paper_id} \
<https://api.semanticscholar.org/api-docs/recommendations#\
tag/Paper-Recommendations/operation/get_papers_for_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 list fields: (optional) list of the fields to be returned.
:param int limit: (optional) maximum number of recommendations to \
return (must be <= 500).
:param str pool_from: (optional) which pool of papers to recommend \
from. Must be either "recent" or "all-cs".
:returns: list of recommendations.
:rtype: :class:`List` of :class:`semanticscholar.Paper.Paper`
'''
if pool_from not in ["recent", "all-cs"]:
raise ValueError(
'The pool_from parameter must be either "recent" or "all-cs".')
if limit < 1 or limit > 500:
raise ValueError(
'The limit parameter must be between 1 and 500 inclusive.')
if not fields:
fields = Paper.SEARCH_FIELDS
base_url = self.api_url + self.BASE_PATH_RECOMMENDATIONS
url = f'{base_url}/papers/forpaper/{paper_id}'
fields = ','.join(fields)
parameters = f'&fields={fields}&limit={limit}&from={pool_from}'
data = await self._requester.get_data_async(url, parameters, self.auth_header)
papers = [Paper(item) for item in data['recommendedPapers']]
return papers
async def get_recommended_papers_from_lists(
self,
positive_paper_ids: List[str],
negative_paper_ids: List[str] = None,
fields: list = None,
limit: int = 100
) -> List[Paper]:
'''Get recommended papers for lists of positive and negative examples.
:calls: `POST /recommendations/v1/papers/ \
<https://api.semanticscholar.org/api-docs/recommendations#\
tag/Paper-Recommendations/operation/post_papers>`_
:param list positive_paper_ids: list of paper IDs \
that the returned papers should be related to.
:param list negative_paper_ids: (optional) list of paper IDs \
that the returned papers should not be related to.
:param list fields: (optional) list of the fields to be returned.
:param int limit: (optional) maximum number of recommendations to \
return (must be <= 500).
:returns: list of recommendations.
:rtype: :class:`List` of :class:`semanticscholar.Paper.Paper`
'''
if limit < 1 or limit > 500:
raise ValueError(
'The limit parameter must be between 1 and 500 inclusive.')
if not fields:
fields = Paper.SEARCH_FIELDS
base_url = self.api_url + self.BASE_PATH_RECOMMENDATIONS
url = f'{base_url}/papers/'
fields = ','.join(fields)
parameters = f'&fields={fields}&limit={limit}'
payload = {
"positivePaperIds": positive_paper_ids,
"negativePaperIds": negative_paper_ids
}
data = await self._requester.get_data_async(
url, parameters, self.auth_header, payload)
papers = [Paper(item) for item in data['recommendedPapers']]
return papers