Last active
September 21, 2019 13:42
-
-
Save peterstadler/79d402bb989c8a066b482381979557a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# this script adds new (old) versions to the TEI zenodo space | |
# it assumes the zip archives in the current folder along with | |
# the readme files. Naming convention is `tei-x.x.x.zip` for the | |
# archives and `readme-x.x.x.html.xml`(!) for the readme files. | |
# NB: The readme files (which are to be found in the current release) | |
# need to be transformed first by tei2zenodo-readme.xsl | |
import requests | |
import json | |
import re | |
import argparse | |
ACCESS_TOKEN='put your token here' | |
INITIAL_DEPOSIT_URL="https://sandbox.zenodo.org/api/deposit/depositions/371993" | |
def newVersion(initial_deposit_url): | |
"create a new draft version" | |
r = requests.post(initial_deposit_url + "/actions/newversion", params={'access_token': ACCESS_TOKEN}) | |
if r.status_code != 201: | |
raise Exception("Creation of new version failed: " + str(r.json())) | |
return r.json()['links']['latest_draft'] | |
def uploadFile(deposit_url, tei_version): | |
"upload a file to the new draft, using the yet undocumented upload API, see https://github.com/zenodo/zenodo/issues/833" | |
bucket_url = requests.get(deposit_url, params={'access_token': ACCESS_TOKEN}).json()['links']['bucket'] | |
filename = 'tei-' + tei_version + '.zip' | |
r = requests.put('%s/%s' % (bucket_url,filename), | |
data=open('tei-' + tei_version + '.zip', 'rb'), | |
headers={"Accept":"application/json", | |
"Authorization":"Bearer %s" % ACCESS_TOKEN, | |
"Content-Type":"application/octet-stream"}) | |
if r.status_code != 200: | |
raise Exception("Upload failed: " + str(r.json())) | |
return r | |
def deleteAllFiles(deposit_url): | |
"delete all files that are currently associated with the publication" | |
files = requests.get(deposit_url + '/files', params={'access_token': ACCESS_TOKEN}) | |
for file in files.json(): | |
requests.delete(file['links']['self'], params={'access_token': ACCESS_TOKEN}) | |
return | |
def updateMetadata(deposit_url, tei_version): | |
"update the metadata of the deposit" | |
# first, get the readme file | |
try: | |
readme = open('readme-' + tei_version + '.html.xml', 'rt') | |
desc = readme.read() | |
readme.close() | |
except IOError: | |
print('Failed to open readme file for version ' + tei_version) | |
desc = 'Need to add a description manually' | |
# extract the date from the closing address of the readme by matching a regex | |
dates = re.findall(r'Date: (\d{4}-\d{2}-\d{2})<br', desc) | |
if len(dates) == 0: | |
date = '1970-01-01' | |
else: | |
date = dates[0] | |
# update the metadata fields of the new deposit | |
metadata = requests.get(deposit_url, params={'access_token': ACCESS_TOKEN}).json()['metadata'] | |
metadata['version'] = 'v' + tei_version | |
metadata['description'] = desc | |
metadata['publication_date'] = date | |
# finally, do the upload | |
url = deposit_url + "?access_token=" + ACCESS_TOKEN | |
headers = {"Content-Type": "application/json"} | |
r = requests.put(url, data=json.dumps({"metadata": metadata}), headers=headers) | |
if r.status_code != 200: | |
raise Exception("Update failed: " + str(r.json())) | |
return r | |
def publish(deposit_url): | |
"publish the draft" | |
r = requests.post(deposit_url + '/actions/publish', params={'access_token': ACCESS_TOKEN}) | |
if r.status_code != 202: | |
raise Exception("Publish failed: " + str(r.json())) | |
return r | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-v", "--version", help="specify the TEI version you want to upload to Zenodo") | |
args = parser.parse_args() | |
tei_version=args.version | |
# ***** | |
# main stuff starts here | |
# ***** | |
# create a new version | |
deposit_url = newVersion(INITIAL_DEPOSIT_URL) | |
# delete old uploads (which are copied to the new deposit by default) | |
deleteAllFiles(deposit_url) | |
# upload the zip archive | |
upload = uploadFile(deposit_url, tei_version) | |
# update the metadata | |
update = updateMetadata(deposit_url, tei_version) | |
# publish the deposit | |
publish(deposit_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment