Skip to content

Commit 524d05d

Browse files
committed
Add download url function
1 parent b258280 commit 524d05d

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

caltechdata_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
from .customize_schema import customize_schema
44
from .decustomize_schema import decustomize_schema
55
from .get_metadata import get_metadata
6-
from .download_file import download_file
6+
from .download_file import download_file, download_url

caltechdata_api/download_file.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from tqdm import tnrange, tqdm_notebook
33

44

5-
def download_file(doi, fname=None, media_type=None):
6-
"""Download a file listed in the media API for a DataCite DOI"""
5+
def download_url(doi, media_type=None):
6+
"""Get a download link for a file listed in the media API for a DataCite DOI"""
77
api_url = "https://api.datacite.org/dois/" + doi + "/media"
88
r = requests.get(api_url).json()
99
data = r["data"]
@@ -13,6 +13,12 @@ def download_file(doi, fname=None, media_type=None):
1313
for media in data:
1414
if media["attributes"]["mediaType"] == media_type:
1515
url = media["attributes"]
16+
return url
17+
18+
19+
def download_file(doi, fname=None, media_type=None):
20+
"""Download a file listed in the media API for a DataCite DOI"""
21+
url = download_url(doi, media_type)
1622
r = requests.get(url, stream=True)
1723
# Set file name
1824
if fname == None:

codemeta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"codeRepository": "https://github.com/caltechlibrary/caltechdata_api",
77
"issueTracker": "https://github.com/caltechlibrary/caltechdata_api/issues",
88
"license": "https://data.caltech.edu/license",
9-
"version": "0.1.7",
9+
"version": "0.1.8",
1010
"author": [
1111
{
1212
"@type": "Person",

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def read(fname):
5151

5252
# What packages are required for this module to be executed?
5353
REQUIRED = [
54-
'requests','datacite','tqdm'
54+
'requests','datacite>1.1.0','tqdm'
5555
]
5656

5757
# What packages are optional?

tests/test_download.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# This file is part of caltechdata_api.
4+
#
5+
# Copyright (C) 2020 Caltech.
6+
#
7+
# caltechdata_api is free software; you can redistribute it and/or modify it
8+
# under the terms of the Revised BSD License; see LICENSE file for
9+
# more details.
10+
11+
"""Tests for format transformations."""
12+
13+
import pytest
14+
15+
from caltechdata_api import download_url, download_file
16+
17+
def test_download():
18+
"""Test that downloads from the DataCite Media API work."""
19+
example_doi = '10.22002/D1.1945'
20+
expected_url = 'https://data.caltech.edu/tindfiles/serve/0cedc067-1ff0-4fc9-871a-2869f3a4957d/'
21+
assert expected_url == download_url(example_doi)

0 commit comments

Comments
 (0)