summaryrefslogtreecommitdiff
path: root/s2-fetch-pdf.py
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-11-06 16:14:36 +0100
committerJules Laplace <julescarbon@gmail.com>2018-11-06 16:14:36 +0100
commit6fa2b98685fafc63385bc5618c043c120933a811 (patch)
treea85c939a836b9eeb742e7dd7c3c38c1bfdf1f2ab /s2-fetch-pdf.py
parente6a19cb5c9db39f00eb83cf0ae48edc85878e08e (diff)
raw paper scrapes
Diffstat (limited to 's2-fetch-pdf.py')
-rw-r--r--s2-fetch-pdf.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/s2-fetch-pdf.py b/s2-fetch-pdf.py
new file mode 100644
index 00000000..7d834ada
--- /dev/null
+++ b/s2-fetch-pdf.py
@@ -0,0 +1,49 @@
+import os
+import sys
+import csv
+import subprocess
+import time
+import random
+import re
+import json
+import click
+from s2 import SemanticScholarAPI
+from util import *
+
+s2 = SemanticScholarAPI()
+
+@click.command()
+@click.option('--fn', '-i', default='db_paper_pdf.csv', help='Filename of CSV (id, url,)')
+def fetch_pdfs(fn):
+ lines = read_csv(fn, keys=False)
+ for line in lines:
+ paper_id, url = line
+ fetch_pdf(paper_id, url)
+ print("{} papers processed".format(len(lines)))
+
+def fetch_pdf(paper_id, url):
+ os.makedirs(make_pdf_path(paper_id), exist_ok=True)
+ pdf_fn = make_pdf_fn(paper_id)
+ txt_fn = make_txt_fn(paper_id)
+ if os.path.exists(pdf_fn) or os.path.exists(txt_fn):
+ # return read_json(pdf_fn)
+ return
+ size = s2.fetch_file(url, pdf_fn)
+ if size is None:
+ print("{} empty?".format(paper_id))
+ time.sleep(random.randint(5, 10))
+ return None
+ print("{} {} kb".format(paper_id, int(size / 1024)))
+ time.sleep(random.randint(5, 10))
+ return
+ # return paper
+
+def make_pdf_path(paper_id):
+ return './datasets/s2/pdf/{}/{}'.format(paper_id[0:2], paper_id)
+def make_pdf_fn(paper_id):
+ return './datasets/s2/pdf/{}/{}/paper.pdf'.format(paper_id[0:2], paper_id)
+def make_txt_fn(paper_id):
+ return './datasets/s2/pdf/{}/{}/paper.txt'.format(paper_id[0:2], paper_id)
+
+if __name__ == '__main__':
+ fetch_pdfs()