forked from darvasd/upload-to-zenodo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_to_zenodo.py
95 lines (79 loc) · 3.5 KB
/
upload_to_zenodo.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
95
import json
import requests
import os
import sys
import codecs
BASE_URL = "https://sandbox.zenodo.org" # TODO: once you are sure about what you are doing, remove the "sandbox." part
TOKEN = os.getenv('TOKEN')
print(TOKEN)
def upload(metadata, directory):
if not _is_valid_json(metadata):
return
# Create new submission
url = "{base_url}/api/deposit/depositions".format(base_url=BASE_URL)
headers = {"Content-Type": "application/json"}
params = {'access_token': TOKEN}
response = requests.post(url, params=params, json={}, headers=headers)
#print(response.text)
if response.status_code > 210:
print("Error happened during submission, status code: " + str(response.status_code))
return
# Get the submission ID
submission_id = response.json()["id"]
bucket_url = response.json()["links"]["bucket"]
# Add metadata
response = requests.put('{base_url}/api/deposit/depositions/{id}'.format(base_url=BASE_URL, id=submission_id),
params=params,
data=metadata,
headers=headers)
if response.status_code > 210:
print("Error happened during metadata upload, status code: " + str(response.status_code))
print(response.json())
return
for csv_file in os.listdir(directory):
filepath = os.path.join(directory, csv_file)
with open(filepath, "rb") as fp:
response = requests.put(
"%s/%s" % (bucket_url, csv_file),
data=fp,
params=params,
)
# Upload the file
if response.status_code > 210:
print("Error happened during file upload, status code: " + str(response.status_code))
return
print("{file} submitted with submission ID = {id} (DOI: 10.5281/zenodo.{id})".format(file=directory,id=submission_id))
# The submission needs an additional "Publish" step. This can also be done from a script, but to be on the safe side, it is not included. (The attached file cannot be changed after publication.)
def batch_upload(directory):
for metadata_file in os.listdir(directory):
metadata_file = os.path.join(directory, metadata_file)
if metadata_file.endswith(".json"):
filename = metadata_file.replace(".json","")
if os.path.isdir(filename):
print("Uploading %s & %s" % (metadata_file, filename))
with codecs.open(metadata_file, 'r', 'utf-8') as f:
metadata = f.read()
# Re-encoding in order to support UTF-8 inputs
metadata_json = json.loads(metadata)
metadata = json.dumps(metadata_json, ensure_ascii=True)
upload(metadata, filename)
else:
print("The file %s might be a submission metadata file, but %s does not exist." % (metadata_file, filename))
def _is_valid_json(text):
try:
json.loads(text)
return True
except ValueError as e:
print('Invalid json: %s' % e)
return False
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: upload_to_zenodo.py <token> <directory>")
print(" The directory contains .json metadata descriptors and .pdf files.")
exit()
TOKEN = sys.argv[1]
directory = sys.argv[2]
if not os.path.isdir(directory):
print("Invalid directory.")
exit()
batch_upload(directory)