upload_new_deposit.py 2.99 KB
Newer Older
1 2 3 4 5
# -*- coding: utf-8 -*-
import os
import json
import argparse
from distutils.util import strtobool
Enrique Garcia's avatar
Enrique Garcia committed
6
from zenodoapi import ZenodoAPI
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


parser = argparse.ArgumentParser(description="Upload new deposit entry to Zenodo")

# Required arguments
parser.add_argument('--token', '-t', type=str,
                    dest='zenodo_token',
                    help='Personal access token to (sandbox)Zenodo')

parser.add_argument('--sandbox_zenodo', '-s', action='store',
                    type=lambda x: bool(strtobool(x)),
                    dest='sandbox_flag',
                    help='Set the Zenodo environment.'
                         'If True connects with Zenodo. If False with Sanbox Zenodo',
                    default=False)

parser.add_argument('--input-directory', '-i', type=str,
                    dest='input_directory',
                    help='Path to the directory containing the files to upload.'
                         'ALL files will be uploaded.',
                    required=True)

args = parser.parse_args()

if __name__ == '__main__':

    z = ZenodoAPI(access_token=args.zenodo_token,
                  sandbox=args.sandbox_flag  # True for sandbox.zenodo.org !! False for zenodo.org
                  )

    # 1 - create empty deposit
    new_entry = z.create_new_entry()

    if new_entry.status_code < 399:
        deposition_id = new_entry.json()['id']
        doi = new_entry.json()['metadata']['prereserve_doi']['doi']
        print(f"Status {new_entry.status_code}. New entry to Zenodo created ! Deposition id {deposition_id}")
    else:
        print(new_entry.json())

    # 2 - upload files
    for file in os.listdir(args.input_directory):
        full_path_file = args.input_directory + '/' + file

51 52 53
        new_upload = z.upload_file_entry(deposition_id,
                                         name_file=file,
                                         path_file=full_path_file)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

        print(f"File {file} correctly uploaded !\n", new_upload)

    # 3 - Upload repository information - that you must have filled before the json file !
    with open('.zenodoci/repository_information.json') as json_file:
        entry_info = json.load(json_file)

    # entry_info['metadata']['doi'] = doi  # In the new version of the API the doi is updated automatically.
    update_entry = z.update_info_entry(deposition_id,
                                       data=entry_info)

    if update_entry.status_code < 399:
        print(f"Status {update_entry.status_code}. Repository information correctly uploaded !")
    else:
        print(f"Repository information NOT correctly uploaded ! Status {update_entry.status_code}\n",
              update_entry.json())

    # 4 - publish entry - to publish the entry, uncomment the two lone below
    # publish = z.publish_entry(deposition_id)
    # print(publish.json())

    print("New deposit correctly published !")
    print(f"The new doi should look like 10.5281/{deposition_id}. However please")
    print(f" ** Check the upload at {z.zenodo_api_url[:-4]}deposit/{deposition_id}  **")