generated from caltechlibrary/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_doi.py
50 lines (38 loc) · 1.23 KB
/
check_doi.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
import argparse
import csv
import json
import os
import requests
def check_doi(doi, production=True, token=None):
# Returns whether or not a DOI has already been added to CaltechAUTHORS
if production == True:
url = "https://authors.library.caltech.edu/api/records"
else:
url = "https://authors.caltechlibrary.dev/api/records"
query = f'?q=pids.doi.identifier:"{doi}"&allversions=true'
if token:
headers = {"Authorization": f"Bearer {token}"}
else:
headers = {}
response = requests.get(url + query, headers=headers)
if response.status_code != 200:
raise Exception(response.text)
else:
metadata = response.json()
if metadata["hits"]["total"] > 0:
return True
else:
# Check queue
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="check_doi queries the caltechDATA (Invenio 3) API\
for a given DOI and returns whether it is present"
)
parser.add_argument(
"doi",
)
parser.add_argument("-test", dest="production", action="store_false")
args = parser.parse_args()
production = args.production
print(check_doi(args.doi, production))