def maybe_download()

in fclib/fclib/dataset/ojdata.py [0:0]


def maybe_download(url, dest_directory, filename=None):
    """Download a file if it is not already downloaded.
    Args:
        dest_directory (str): Destination directory.
        url (str): URL of the file to download.
        filename (str): File name.
        
    Returns:
        str: File path of the file downloaded.
    """
    if filename is None:
        filename = url.split("/")[-1]
    os.makedirs(dest_directory, exist_ok=True)
    filepath = os.path.join(dest_directory, filename)
    if not os.path.exists(filepath):
        r = requests.get(url, stream=True)
        total_size = int(r.headers.get("content-length", 0))
        block_size = 1024
        num_iterables = math.ceil(total_size / block_size)

        with open(filepath, "wb") as file:
            for data in tqdm(r.iter_content(block_size), total=num_iterables, unit="KB", unit_scale=True,):
                file.write(data)
    else:
        log.debug("File {} already downloaded".format(filepath))

    return filepath