summaryrefslogtreecommitdiff
path: root/s2.py
blob: 5ebe507bb43d50cd5b0cb756944fb4304a8f4c9a (plain)
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
import requests

class AuthorStub(object):

    def __init__(self, **kwargs):
        self.authorId = kwargs["authorId"]
        self.name = kwargs.get("name", None)
        self.url = kwargs.get("url", None)

    def __str__(self):
        return self.authorId

    def __eq__(self, other):
        return isinstance(other, AuthorStub) and self.authorId == other.authorId

    def __hash__(self):
        return hash(self.authorId)

    def json(self):
        return {
            "authorId" : self.authorId,
            "name" : self.name,
            "url" : self.url
        }

    def full(self, **kwargs):
        return SemanticScholarAPI.author(self.authorId, **kwargs)

class Author(object):

    def __init__(self, **kwargs):
        self._kwargs = kwargs
        self.authorId = kwargs["authorId"]
        self.name = kwargs.get("name", None)
        self.aliases = kwargs.get("aliases", [])
        self.citationVelocity = kwargs.get("citationVelocity", None)
        self.influentialCitationCount = kwargs.get("influentialCitationCount", None)
        self.url = kwargs.get("url", None)

    def __str__(self):
        return self.authorId

    def __eq__(self, other):
        return isinstance(other, Author) and self.authorId == other.authorId

    def __hash__(self):
        return hash(self.authorId)

    def papers(self):
        for elem in self._kwargs.get("papers", []):
            yield SemanticScholarAPI.paper(elem["paperId"])

    def json(self):
        return self._kwargs

class PaperStub(object):

    def __init__(self, **kwargs):
        self.paperId = kwargs["paperId"]
        self.isInfluential = kwargs.get("isInfluential", False)
        self.title = kwargs.get("title", None)
        self.venue = kwargs.get("venue", None)
        self.year = kwargs.get("year", None)

    def __str__(self):
        return self.paperId

    def __eq__(self, other):
        return isinstance(other, PaperStub) and self.paperId == other.paperId

    def __hash__(self):
        return hash(self.paperId)

    def json(self):
        return {
            "paperId" : self.paperId,
            "isInfluential" : self.isInfluential,
            "title" : self.title,
            "venue" : self.venue,
            "year" : self.year,
        }

    def full(self, **kwargs):
        return SemanticScholarAPI.paper(self.paperId, **kwargs)

class Paper(object):

    def __init__(self, **kwargs):
        self.doi = kwargs.get("doi", None)
        self.citationVelocity = kwargs.get("citationVelocity", None)
        self.influentialCitationCount = kwargs.get("influentialCitationCount", None)
        self.url = kwargs.get("url", None)
        self.authors = [AuthorStub(**elem) for elem in kwargs.get("authors", [])]
        self.citations = [PaperStub(**elem) for elem in kwargs.get("citations", [])]
        self.references = [PaperStub(**elem) for elem in kwargs.get("references", [])]
        self.venue = kwargs.get("venue", None)
        self.references = kwargs.get("references", [])
        self.title = kwargs.get("title", None)
        self.year = kwargs.get("year", None)

    def __str__(self):
        return self.paperId

    def __eq__(self, other):
        return isinstance(other, Paper) and self.paperId == other.paperId

    def __hash__(self):
        return hash(self.paperId)

    def json(self):
        return self._kwargs

class SemanticScholarAPI(object):
    BASE_URL = "http://api.semanticscholar.org/v1"
    AUTHOR_ENDPOINT = "{}/{}".format(BASE_URL, "author")
    PAPER_ENDPOINT = "{}/{}".format(BASE_URL, "paper")
    SEARCH_ENDPOINT = "https://www.semanticscholar.org/api/1/search"

    @staticmethod
    def paper(paper_id, **kwargs):
        url = "{}/{}".format(SemanticScholarAPI.PAPER_ENDPOINT, paper_id)
        resp = requests.get(url, params=kwargs)
        return None if resp.status_code != 200 else resp.json() # Paper(**resp.json())

    @staticmethod
    def author(author_id, **kwargs):
        url = "{}/{}".format(SemanticScholarAPI.AUTHOR_ENDPOINT, author_id)
        resp = requests.get(url, params=kwargs)
        return None if resp.status_code != 200 else resp.json() # Author(**resp.json())

    @staticmethod
    def pdf_url(paper_id):
      return "http://pdfs.semanticscholar.org/{}/{}.pdf".format(paper_id[:4], paper_id[4:])

    @staticmethod
    def search(q):
        resp = requests.post(SemanticScholarAPI.SEARCH_ENDPOINT, json={
            'authors': [],
            'coAuthors': [],
            'facets': {},
            'page': 1,
            'pageSize': 10,
            'publicationTypes': [],
            'queryString': q,
            'requireViewablePdf': False,
            'sort': "relevance",
            'venues': [],
            'yearFilter': None,
        })
        # print(resp.status_code)
        return None if resp.status_code != 200 else resp.json()