diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-11-05 22:05:31 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-11-05 22:05:31 +0100 |
| commit | f16debd0d003aa2b278ef824b6bde2fe5d7367b6 (patch) | |
| tree | 6b566d64b6ad13d17e4392195220e2beb66cc4a4 /s2.py | |
| parent | d385e204185ef3ea195e366edcc5fba73ace33ab (diff) | |
pdf downloader
Diffstat (limited to 's2.py')
| -rw-r--r-- | s2.py | 24 |
1 files changed, 20 insertions, 4 deletions
@@ -116,17 +116,33 @@ class SemanticScholarAPI(object): PAPER_ENDPOINT = "{}/{}".format(BASE_URL, "paper") SEARCH_ENDPOINT = "https://www.semanticscholar.org/api/1/search" RAW_PAPER_ENDPOINT = "https://www.semanticscholar.org/api/1/paper" + headers = { + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36', + } + + @staticmethod + def fetch_file(url, fn, **kwargs): + resp = requests.get(url, params=kwargs, headers=SemanticScholarAPI.headers) + if resp.status_code != 200: + return None + size = 0 + with open(fn, 'wb') as f: + for chunk in resp.iter_content(chunk_size=1024): + if chunk: + size += len(chunk) + f.write(chunk) + return size @staticmethod def paper(paper_id, **kwargs): url = "{}/{}".format(SemanticScholarAPI.PAPER_ENDPOINT, paper_id) - resp = requests.get(url, params=kwargs) + resp = requests.get(url, params=kwargs, headers=SemanticScholarAPI.headers) 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) + resp = requests.get(url, params=kwargs, headers=SemanticScholarAPI.headers) return None if resp.status_code != 200 else resp.json() # Author(**resp.json()) @staticmethod @@ -136,7 +152,7 @@ class SemanticScholarAPI(object): @staticmethod def raw_paper(paper_id, **kwargs): url = "{}/{}".format(SemanticScholarAPI.RAW_PAPER_ENDPOINT, paper_id) - resp = requests.get(url, params=kwargs) + resp = requests.get(url, params=kwargs, headers=SemanticScholarAPI.headers) return None if resp.status_code != 200 else resp.json() # Paper(**resp.json()) @staticmethod @@ -153,6 +169,6 @@ class SemanticScholarAPI(object): 'sort': "relevance", 'venues': [], 'yearFilter': None, - }) + }, headers=SemanticScholarAPI.headers) # print(resp.status_code) return None if resp.status_code != 200 else resp.json() |
