-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdatacite.py
More file actions
80 lines (58 loc) · 2.61 KB
/
datacite.py
File metadata and controls
80 lines (58 loc) · 2.61 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Functions for communicating with DataCite and some utilities."""
__copyright__ = 'Copyright (c) 2019-2024, Utrecht University'
__license__ = 'GPLv3, see LICENSE'
import random
import string
from typing import Dict
import requests
from util import *
def metadata_post(payload: Dict) -> requests.Response:
"""Register DOI metadata with DataCite."""
url = "{}/dois".format(config.datacite_rest_api_url)
auth = (config.datacite_username, config.datacite_password)
headers = {'Content-Type': 'application/json', 'charset': 'UTF-8'}
response = requests.post(url,
auth=auth,
data=payload.encode(),
headers=headers,
timeout=30,
verify=config.datacite_tls_verify)
return response
def metadata_put(doi: str, payload: str) -> requests.Response:
"""Update metadata with DataCite."""
url = "{}/dois/{}".format(config.datacite_rest_api_url, doi)
auth = (config.datacite_username, config.datacite_password)
headers = {'Content-Type': 'application/json', 'charset': 'UTF-8'}
response = requests.put(url,
auth=auth,
data=payload.encode(),
headers=headers,
timeout=30,
verify=config.datacite_tls_verify)
return response
def metadata_get(doi: str) -> requests.Response:
"""Check with DataCite if DOI is available."""
url = "{}/dois/{}".format(config.datacite_rest_api_url, doi)
auth = (config.datacite_username, config.datacite_password)
headers = {'Content-Type': 'application/json', 'charset': 'UTF-8'}
response = requests.get(url,
auth=auth,
headers=headers,
timeout=30,
verify=config.datacite_tls_verify)
return response
def generate_random_id(length: int) -> str:
"""Generate random ID for DOI."""
characters = string.ascii_uppercase + string.digits
return ''.join(random.choice(characters) for x in range(int(length)))
def get_errors(response: dict) -> str:
"""Get errors from DataCite response"""
error_msg = ''
if 'errors' in response:
errors = response['errors']
for error in errors:
if 'source' in error:
error_msg += "DataCite error from attribute \"" + error['source'] + "\": \"" + error['title'] + "\". "
else:
error_msg += "DataCite error: \"" + error['title'] + "\". "
return error_msg