forked from krzywon/saspubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_pubsjson.py
57 lines (50 loc) · 1.58 KB
/
make_pubsjson.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
import requests
import json
import urllib
json_headers = {"Accept": "application/vnd.citationstyles.csl+json"}
bibtex_headers = {"Accept": "application/x-bibtex"}
keys_to_import = [
"title",
"author",
"container-title-short",
"container-title",
"volume",
"page",
"article-number",
"issued",
"DOI",
"URL",
"is-referenced-by-count"
]
def make_pubdb(doi_list, filter_keys=False, add_bibtex=False):
output = []
for i, doi in enumerate(doi_list):
escaped_doi = urllib.parse.quote(doi)
print(i, escaped_doi)
rj = requests.get("https://data.crossref.org/%s" % (escaped_doi,), headers = json_headers)
entry = {}
try:
content = rj.json()
if filter_keys:
entry.update(dict([(k, content.get(k, None)) for k in keys_to_import]))
else:
entry.update(content)
if add_bibtex:
rb = requests.get("https://doi.org/%s" % (doi,), headers = bibtex_headers)
entry["__bibtex"] = rb.text
output.append(entry)
except Exception:
print("not processing %s because of error: text" % (doi,))
return output
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
input_filename = sys.argv[1]
doi_list = json.loads(open(input_filename, "r").read())
result = json.dumps(make_pubdb(doi_list))
if len(sys.argv) > 2:
open(sys.argv[2], "w").write(result)
else:
print(result)
else:
print("No arguments supplied.")