From f16debd0d003aa2b278ef824b6bde2fe5d7367b6 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 5 Nov 2018 22:05:31 +0100 Subject: pdf downloader --- s2.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 's2.py') diff --git a/s2.py b/s2.py index c9de75e6..696b1a45 100644 --- a/s2.py +++ b/s2.py @@ -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() -- cgit v1.2.3-70-g09d2