forked from UtrechtUniversity/yoda-ruleset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatacite.py
94 lines (60 loc) · 2.78 KB
/
datacite.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
"""Functions for communicating with DataCite and some utilities."""
__copyright__ = 'Copyright (c) 2019-2020, Utrecht University'
__license__ = 'GPLv3, see LICENSE'
import random
import string
import requests
from util import *
__all__ = ['rule_generate_random_id',
'rule_register_doi_metadata',
'rule_register_doi_url',
'rule_check_doi_availability',
'rule_delete_doi_metadata']
@rule.make(inputs=[0], outputs=[1])
def rule_generate_random_id(ctx, length):
return generate_random_id(ctx, length)
def generate_random_id(ctx, length):
"""Generate random ID for DOI."""
characters = string.ascii_uppercase + string.digits
return ''.join(random.choice(characters) for x in range(int(length)))
@rule.make(inputs=[0, 1], outputs=[2])
def rule_register_doi_metadata(ctx, doi, payload):
return register_doi_metadata(ctx, doi, payload)
def register_doi_metadata(ctx, doi, payload):
"""Register DOI metadata with DataCite."""
url = "{}/metadata/{}".format(config.datacite_url, doi)
auth = (config.datacite_username, config.datacite_password)
headers = {'Content-Type': 'application/xml', 'charset': 'UTF-8'}
response = requests.put(url, auth=auth, data=payload, headers=headers, timeout=30)
return response.status_code
@rule.make(inputs=[0, 1], outputs=[2])
def rule_register_doi_url(ctx, doi, url):
return register_doi_url(ctx, doi, url)
def register_doi_url(ctx, doi, landingpage_url):
"""Register DOI url with DataCite."""
url = "{}/doi/{}".format(config.datacite_url, doi)
auth = (config.datacite_username, config.datacite_password)
payload = "doi={}\nurl={}".format(doi, landingpage_url)
headers = {'content-type': 'text/plain', 'charset': 'UTF-8'}
response = requests.put(url, auth=auth, data=payload, headers=headers, timeout=30)
return response.status_code
@rule.make(inputs=[0], outputs=[1])
def rule_check_doi_availability(ctx, doi):
return check_doi_availability(ctx, doi)
def check_doi_availability(ctx, doi):
"""Check with DataCite if DOI is available."""
url = "{}/doi/{}".format(config.datacite_url, doi)
auth = (config.datacite_username, config.datacite_password)
response = requests.get(url, auth=auth, timeout=30)
return response.status_code
@rule.make(inputs=[0], outputs=[1])
def rule_delete_doi_metadata(ctx, doi):
return delete_doi_metadata(ctx, doi)
def delete_doi_metadata(ctx, doi):
"""Delete DOI metadata with DataCite."""
url = "{}/metadata/{}".format(config.datacite_url, doi)
auth = (config.datacite_username, config.datacite_password)
headers = {'content-type': 'text/plain', 'charset': 'UTF-8'}
response = requests.delete(url, auth=auth, headers=headers, timeout=30)
return response.status_code