"""
Author: Ismael Seidel (ismael.seidel@ufsc.br)
Affiliation: Embedded Computing Lab (ECL), Federal University of Santa Catarina (UFSC)
Description:
This module defines the LightFieldDownloader class, which handles Light Fields
Dataset downloads from a defined URL.
"""
from pathlib import Path
from typing import Union
import requests
from tqdm import tqdm
[docs]
class LightfieldDownloader:
[docs]
@staticmethod
def download(url: str, filename: str, extension: str, destination: Union[str, Path]):
"""Download Light Field from defined URL
:param url: Database URL
:type url: str
:param filename: Filename, usually the LF name (e.g, Bikes, greek)
:type filename: str
:param extension: Downloaded file extension (e.g, zip)
:type extension: str
:param destination: Output path for the downloaded dataset
:type destination: Union[str, Path]
"""
response = requests.get(url, stream=True)
total_size = int(response.headers.get("content-length", 0))
chunk_size = 16384
destination_path = destination / f"{filename}.{extension}"
tqdm_params = dict(
desc=filename,
total=total_size,
unit="iB",
unit_scale=True,
unit_divisor=1024,
)
print(f"Downloading from {url}")
with open(destination_path, "wb") as handle, tqdm(**tqdm_params) as bar:
for data in response.iter_content(chunk_size):
size = handle.write(data)
bar.update(size)
# TODO: check why tqdm bar is shown again after the message below
print("Finished downloading")
# TODO: check if the downloaded file has an expected checksum