diff --git a/pyzenodo3/upload.py b/pyzenodo3/upload.py index 18e10b2..fe19e50 100644 --- a/pyzenodo3/upload.py +++ b/pyzenodo3/upload.py @@ -8,6 +8,7 @@ BASE_URL = "https://zenodo.org/api" HDR = {"Content-Type": "application/json"} + def meta(inifn: Path) -> Path: """ creates metadata for Zenodo upload. 1. create dict() with metadata @@ -72,7 +73,7 @@ def get_token(token: Union[str, Path]) -> str: def upload_meta(token: str, metafn: Path, depid: str): """upload metadata to zenodo""" - + if not metafn: raise ValueError('must specify API token or file containing the token') @@ -84,32 +85,32 @@ def upload_meta(token: str, metafn: Path, depid: str): meta = metafn.read_text() r = requests.put(f"{BASE_URL}/deposit/depositions/{depid}", - params={'access_token': token}, - data=meta, #json.dumps(meta), - headers=HDR) - + params={'access_token': token}, + data=meta, # json.dumps(meta), + headers=HDR) + if r.status_code != 200: raise requests.HTTPError(f"Error in metadata upload, status code: {r.status_code} {r.json()['message']}") - - + + def upload_data(token: str, datafn: Path, depid: str): r = requests.post(f"{BASE_URL}/deposit/depositions/{depid}/files", params={'access_token': token}, - data={'filename': str(datafn)}, + data={'filename': str(datafn)}, files={'file': datafn.open('rb')}) if r.status_code != 201: raise requests.HTTPError(f"Error in data upload, status code: {r.status_code} {r.json()['message']}") - + print(f"{datafn} ID = {depid} (DOI: 10.5281/zenodo.{depid})") - - + + def create(token: str) -> str: - - r = requests.post(f"{BASE_URL}/deposit/depositions", + + r = requests.post(f"{BASE_URL}/deposit/depositions", params={'access_token': token}, - json={}, + json={}, headers=HDR) if r.status_code != 201: @@ -117,6 +118,7 @@ def create(token: str) -> str: # %% Get the deposition ID return r.json()["id"] + def upload(metafn: Path, datafn: Path, token: Union[str, Path]): """takes metadata and file and uploads to Zenodo""" @@ -132,4 +134,4 @@ def upload(metafn: Path, datafn: Path, token: Union[str, Path]): # %% Upload data upload_data(token, datafn, depid) # %% add metadata - # upload_meta(token, metafn, depid) + # upload_meta(token, metafn, depid) diff --git a/setup.cfg b/setup.cfg index bff1f03..4b0e098 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,7 +1,7 @@ [metadata] name = pyzenodo3 -version = 0.1.0 +version = 0.1.1 author = Michael Hirsch, Tom Klaver description = Pure Python 3 wrapper for the Zenodo REST API long_description = file: README.md diff --git a/upload_zenodo.py b/upload_zenodo.py index 18524f4..859ca0a 100755 --- a/upload_zenodo.py +++ b/upload_zenodo.py @@ -21,7 +21,7 @@ def main(): metafn = zup.meta(p.inifn) zup.upload(metafn, p.path, p.apikey) - - + + if __name__ == '__main__': main()