forked from OpenAPC/openapc-de
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossref_test.py
executable file
·38 lines (32 loc) · 1.11 KB
/
crossref_test.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
Look up a DOI in crossref.
This script looks up a DOI in Crossref.
Prints a listing of fields that could be received and are relevant to OpenAPC,
an error message otherwise.
"""
import argparse
from urllib.request import urlopen, Request
from openapc_toolkit import get_metadata_from_crossref as gmfc
def main():
parser = argparse.ArgumentParser()
parser.add_argument("doi", help="A DOI to look up in crossref.")
parser.add_argument("-r", "--raw", action="store_true", help="Print the raw Crossref XML.")
args = parser.parse_args()
if args.raw:
url = 'https://data.crossref.org/' + args.doi
req = Request(url)
req.add_header("Accept", "application/vnd.crossref.unixsd+xml")
response = urlopen(req)
content_string = response.read()
print(content_string.decode("utf-8"))
else:
res = gmfc(args.doi)
if res["success"]:
for key, value in res["data"].items():
print(key, ":", value)
else:
print(res["error_msg"])
if __name__ == '__main__':
main()