Skip to content
Snippets Groups Projects
download.py 1.4 KiB
Newer Older
André Anjos's avatar
André Anjos committed
# SPDX-FileCopyrightText: Copyright © 2023 Idiap Research Institute <contact@idiap.ch>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import logging
import tempfile
import urllib.request

from tqdm import tqdm

logger = logging.getLogger(__name__)


def download_to_tempfile(url, progress=False):
    """Downloads a file to a temporary named file and returns it.

    Parameters
    ----------

    url : str
        The URL pointing to the file to download

    progress : :py:class:`bool`, Optional
        If a progress bar should be displayed for downloading the URL.


    Returns
    -------

    f : :py:func:`tempfile.NamedTemporaryFile`
André Anjos's avatar
André Anjos committed
        A named temporary file that contains the downloaded URL
    """
    file_size = 0
    response = urllib.request.urlopen(url)
    meta = response.info()
    if hasattr(meta, "getheaders"):
        content_length = meta.getheaders("Content-Length")
    else:
        content_length = meta.get_all("Content-Length")

    if content_length is not None and len(content_length) > 0:
        file_size = int(content_length[0])

    progress &= bool(file_size)

    f = tempfile.NamedTemporaryFile()

    with tqdm(total=file_size, disable=not progress) as pbar:
        while True:
            buffer = response.read(8192)
            if len(buffer) == 0:
                break
            f.write(buffer)
            pbar.update(len(buffer))

    f.flush()
    f.seek(0)
    return f