Skip to content

Commit 662f4c1

Browse files
committed
Formatting
1 parent b7251fb commit 662f4c1

1 file changed

Lines changed: 42 additions & 42 deletions

File tree

‎caltechdata_api/download_from_record.py‎

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
``authors=True``, and from the test (development) instances of either
1010
repository by passing ``production=False``.
1111
"""
12+
1213
import argparse
1314
import os
1415
import requests
@@ -36,12 +37,12 @@ def get_base_url(production: bool = True, authors: bool = False) -> str:
3637
"""
3738
if production:
3839
if authors:
39-
return 'https://authors.library.caltech.edu'
40-
return 'https://data.caltech.edu'
40+
return "https://authors.library.caltech.edu"
41+
return "https://data.caltech.edu"
4142
else:
4243
if authors:
43-
return 'https://authors.caltechlibrary.dev'
44-
return 'https://data.caltechlibrary.dev'
44+
return "https://authors.caltechlibrary.dev"
45+
return "https://data.caltechlibrary.dev"
4546

4647

4748
def get_files_from_record(
@@ -69,22 +70,23 @@ def get_files_from_record(
6970
associated with those files as values.
7071
"""
7172
base_url = get_base_url(production=production, authors=authors)
72-
with requests.get(f'{base_url}/api/records/{record_id}/files') as r:
73+
with requests.get(f"{base_url}/api/records/{record_id}/files") as r:
7374
r.raise_for_status()
7475
files = dict()
75-
for entry in r.json().get('entries', []):
76-
key = entry.pop('key')
76+
for entry in r.json().get("entries", []):
77+
key = entry.pop("key")
7778
files[key] = entry
7879

7980
return files
8081

8182

8283
def download_files_from_record(
83-
record_id: str, output_path: os.PathLike,
84+
record_id: str,
85+
output_path: os.PathLike,
8486
filenames: Optional[Sequence[str]] = None,
8587
max_redirects: int = 5,
8688
production: bool = True,
87-
authors: bool = False
89+
authors: bool = False,
8890
):
8991
"""Download one or more files from a record.
9092
@@ -124,27 +126,25 @@ def download_files_from_record(
124126
Whether to download from CaltechAUTHORS instead of CaltechDATA.
125127
"""
126128
if not os.path.isdir(output_path):
127-
raise IOError(f'{output_path} is not an extant directory')
129+
raise IOError(f"{output_path} is not an extant directory")
128130

129-
files = get_files_from_record(
130-
record_id, production=production, authors=authors)
131+
files = get_files_from_record(record_id, production=production, authors=authors)
131132
if filenames is None:
132133
filenames = sorted(files.keys())
133134

134135
for filename in filenames:
135136
output_file = os.path.join(output_path, filename)
136137
if filename not in files:
137-
raise KeyError(
138-
f'File "{filename}" not found in record {record_id}')
138+
raise KeyError(f'File "{filename}" not found in record {record_id}')
139139

140140
entry = files[filename]
141-
if 'links' not in entry or 'content' not in entry['links']:
141+
if "links" not in entry or "content" not in entry["links"]:
142142
raise NotImplementedError(
143143
f'Metadata for file "{filename}" in record {record_id} is '
144-
'missing the content link'
144+
"missing the content link"
145145
)
146146

147-
content_url = entry['links']['content']
147+
content_url = entry["links"]["content"]
148148
download_content(content_url, output_file, max_redirects=max_redirects)
149149

150150

@@ -174,55 +174,55 @@ def download_content(content_url: str, fname: os.PathLike, max_redirects=5):
174174
# likely expected behavior for ``max_redirects = 0`` is to only
175175
# look at the link directly given back by the record, we add 1
176176
# for the loop to ensure it runs once in that case.
177-
for _ in range(max_redirects+1):
177+
for _ in range(max_redirects + 1):
178178
with requests.get(url, stream=True) as r:
179179
r.raise_for_status()
180-
if 'Location' in r.headers:
180+
if "Location" in r.headers:
181181
# Redirection - follow to the next URL
182-
url = r.headers['Location']
182+
url = r.headers["Location"]
183183
else:
184-
with open(fname, 'wb') as f:
185-
content_length = r.headers.get('content-length', None)
184+
with open(fname, "wb") as f:
185+
content_length = r.headers.get("content-length", None)
186186
if content_length is not None:
187187
content_length = int(content_length)
188-
pbar = tqdm(total=content_length // 1024, unit='kB')
188+
pbar = tqdm(total=content_length // 1024, unit="kB")
189189
for chunk in r.iter_content(chunk_size=1024):
190190
if chunk:
191191
pbar.update()
192192
f.write(chunk)
193-
print(f'Download to {fname} complete.')
193+
print(f"Download to {fname} complete.")
194194
return
195-
raise RuntimeError(
196-
f'Exceeded maximum number of redirects ({max_redirects})'
197-
)
195+
raise RuntimeError(f"Exceeded maximum number of redirects ({max_redirects})")
198196

199197

200-
if __name__ == '__main__':
198+
if __name__ == "__main__":
201199
parser = argparse.ArgumentParser(
202-
description='download_from_record downloads the files attached to a '
203-
'CaltechDATA or CaltechAUTHORS record'
200+
description="download_from_record downloads the files attached to a "
201+
"CaltechDATA or CaltechAUTHORS record"
204202
)
205203
parser.add_argument(
206-
'record_id',
207-
help='The record ID for the record to download files from'
204+
"record_id", help="The record ID for the record to download files from"
208205
)
209206
parser.add_argument(
210-
'filenames',
211-
nargs='*',
207+
"filenames",
208+
nargs="*",
212209
default=None,
213-
help='The names of the files to download. If omitted, all files in '
214-
'the record are downloaded.'
210+
help="The names of the files to download. If omitted, all files in "
211+
"the record are downloaded.",
215212
)
216213
parser.add_argument(
217-
'-output_path', default='.',
218-
help='Existing directory to download the files into'
214+
"-output_path",
215+
default=".",
216+
help="Existing directory to download the files into",
219217
)
220218
parser.add_argument(
221-
'-max_redirects', type=int, default=5,
222-
help='Maximum number of redirects to follow per file'
219+
"-max_redirects",
220+
type=int,
221+
default=5,
222+
help="Maximum number of redirects to follow per file",
223223
)
224-
parser.add_argument('-test', dest='production', action='store_false')
225-
parser.add_argument('-authors', dest='authors', action='store_true')
224+
parser.add_argument("-test", dest="production", action="store_false")
225+
parser.add_argument("-authors", dest="authors", action="store_true")
226226

227227
args = parser.parse_args()
228228

0 commit comments

Comments
 (0)