-
Notifications
You must be signed in to change notification settings - Fork 5
/
process.py
executable file
·83 lines (70 loc) · 2.95 KB
/
process.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
#! /usr/bin/env python3
# -----------------------------------------------------------------------------
# Copyright 2018 ReScience C - BSD two-clauses licence
#
# This script reserves a DOI on zenodo
# Zenodo REST API at http://developers.zenodo.org
# -----------------------------------------------------------------------------
import json
import yaml
import requests
def reserve_doi(server, token):
""" Reserve a new DOI on Zenodo """
headers = { "Content-Type": "application/json" }
url = 'https://%s/api/deposit/depositions' % server
response = requests.post(url, params={'access_token': token},
json={}, headers=headers)
if response.status_code != 201:
raise IOError("%s: " % response.status_code +
response.json()["message"])
data = response.json()
return data["id"], data["metadata"]["prereserve_doi"]["doi"]
# -----------------------------------------------------------------------------
if __name__ == '__main__':
import os
import sys
import argparse
from article import Article
parser = argparse.ArgumentParser(description='DOI pre-reservation on Zenodo')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--sandbox', action='store_true',
help='Use the sandbox server')
group.add_argument('--zenodo', action='store_true',
help='Use the production server')
parser.add_argument('--metadata', action='store', required=True,
help="Article metadata (YAML format)")
parser.add_argument('--pdf', action='store', required=True,
help="Article (PDF format)")
args = parser.parse_args()
# Check for metadata and article files
metadata_file = args.metadata
if not os.path.exists(metadata_file):
print("Metadata file not found ({0}).".format(metadata_file))
sys.exit(0)
article_file = args.pdf
if not os.path.exists(article_file):
print("Article file not found ({0}).".format(article_file))
sys.exit(0)
# Read article metadata
with open(metadata_file) as file:
article = Article(file.read())
# Assign server and token
if args.zenodo:
server = "zenodo.org"
token_name = "ZENODO_TOKEN"
else:
server = "sandbox.zenodo.org"
token_name = "ZENODO_SANDBOX_TOKEN"
token = os.getenv(token_name)
if token is None:
url = "".format(server)
print("No token found ({0})".format(token_name))
print("You can request one from https://{0}/account/settings/applications/tokens/new/".format(server))
sys.exit(0)
# Get DOI
article_id, article_doi = reserve_doi(server, token)
# Display information
print("Article ID: ", article_id)
print("Article DOI:", article_doi)
print("Article URL: https://{0}/record/{1}/files/{2}".format(
server, article_id, os.path.basename(article_file)))