
"""Write a file containing data file urls from zenodo for a given DOI and
zenodo access token. example DOI is "10.5281/zenodo.16407"

usage: python get_zenodo_url.py DOI token output_filename

"""

import sys
import json
import requests


def get_dep_id(doi, token):

    """get the deposition id for the DOI"""

    response = requests.get(ZENODO_URL_BASE + "?access_token=" + token)

    json_data = response.json()

    for deposition in json_data:
        if deposition["doi"] == str(doi):
            dep_id = deposition["id"]
    return dep_id


ZENODO_URL_BASE="https://zenodo.org/api/deposit/depositions/"


#DOI & output file from command line args
#TODO error handling!
doi = sys.argv[1]
token = sys.argv[2]
output_filename = sys.argv[3]

#Get data file meta data from Zenodo
dep_id = get_dep_id(doi, token)
url = ZENODO_URL_BASE + str(dep_id) + "?access_token=" + token
deposition_json = requests.get(url).json()

for dep_file in deposition_json["files"]:
    file_id = dep_file["id"]
    url = ZENODO_URL_BASE + str(dep_id) + "/files/" + str(file_id) + "?access_token=" + token
    file_json = requests.get(url).json()
    dep_file["url"] = "https://zenodo.org/record/" + str(dep_id) + "/files/" + str(file_json["filename"])

#Write data file names and Zenodo urls to output file
with open(output_filename, 'w') as f:
    f.write(json.dumps(deposition_json, indent = 2))

@@ -170,16 +170,17 @@
 I token 
+%5B
 output_f
@@ -186,16 +186,17 @@
 filename
+%5D
 %0A%0A%22%22%22%0A%0Ai
@@ -731,16 +731,65 @@
 lename =
+ None%0Aif len(sys.argv) %3E 3:%0A    output_filename =
  sys.arg
@@ -793,16 +793,16 @@
 argv%5B3%5D%0A
-
 %0A#Get da
@@ -1299,50 +1299,121 @@
 ata 
-file names and Zenodo urls to output file%0A
+to output file or stdout%0Aif output_filename == None:%0A    print json.dumps(deposition_json, indent = 2)%0Aelse:%0A    
 with
@@ -1446,16 +1446,20 @@
 ) as f:%0A
+    
     f.wr

Make output filename optional.