Skip to content

download

download(
    url: str,
    path: Optional[str] = None,
    overwrite: bool = True,
    sha1_hash: Optional[str] = None,
    retries: int = 5,
    verify_ssl: bool = True,
    log: bool = True,
) -> str

Download a given URL.

Code borrowed from dgl

Parameters:

Name Type Description Default
url str

URL to download.

required
path Optional[str]

Destination path to store downloaded file. By default stores to the current directory with the same name as in url.

None
overwrite bool

Whether to overwrite the destination file if it already exists. By default always overwrites the downloaded file.

True
sha1_hash Optional[str]

Expected sha1 hash in hexadecimal digits. Will ignore existing file when hash is specified but doesn't match.

None
retries int

The number of times to attempt downloading in case of failure or non 200 return codes.

5
verify_ssl bool

Verify SSL certificates.

True
log bool

Whether to print the progress for download

True

Returns:

Type Description
str

The file path of the downloaded file.

Source code in mlx_graphs/datasets/utils/download.py
def download(
    url: str,
    path: Optional[str] = None,
    overwrite: bool = True,
    sha1_hash: Optional[str] = None,
    retries: int = 5,
    verify_ssl: bool = True,
    log: bool = True,
) -> str:
    """Download a given URL.

    Code borrowed from dgl

    Args:
        url: URL to download.
        path: Destination path to store downloaded file. By default stores to the
            current directory with the same name as in url.
        overwrite: Whether to overwrite the destination file if it already exists.
            By default always overwrites the downloaded file.
        sha1_hash: Expected sha1 hash in hexadecimal digits. Will ignore existing file
            when hash is specified but doesn't match.
        retries: The number of times to attempt downloading in case of failure or non
            200 return codes.
        verify_ssl: Verify SSL certificates.
        log: Whether to print the progress for download

    Returns:
        The file path of the downloaded file.
    """
    if path is None:
        fname = url.split("/")[-1]
        # Empty filenames are invalid
        assert fname, (
            "Can't construct file-name from this URL. "
            "Please set the `path` option manually."
        )
    else:
        path = os.path.expanduser(path)
        if os.path.isdir(path):
            fname = os.path.join(path, url.split("/")[-1])
        else:
            fname = path
    assert retries >= 0, "Number of retries should be at least 0"

    if not verify_ssl:
        warnings.warn(
            "Unverified HTTPS request is being made (verify_ssl=False). "
            "Adding certificate verification is strongly advised."
        )

    if (
        overwrite
        or not os.path.exists(fname)
        or (sha1_hash and not check_sha1(fname, sha1_hash))
    ):
        dirname = os.path.dirname(os.path.abspath(os.path.expanduser(fname)))
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        while retries + 1 > 0:
            # Disable pyling too broad Exception
            # pylint: disable=W0703
            try:
                if log:
                    print("Downloading %s from %s..." % (fname, url))
                r = requests.get(  # nosec B113 - timeout tuple covers connect + read
                    url, stream=True, verify=verify_ssl, timeout=(30, 300)
                )
                if r.status_code != 200:
                    raise RuntimeError("Failed downloading url %s" % url)
                # Sizes in bytes.
                total_size = int(r.headers.get("content-length", 0))
                block_size = 1024

                with tqdm(total=total_size, unit="B", unit_scale=True) as progress_bar:
                    with open(fname, "wb") as f:
                        for chunk in r.iter_content(chunk_size=block_size):
                            progress_bar.update(len(chunk))
                            if chunk:  # filter out keep-alive new chunks
                                f.write(chunk)
                if sha1_hash and not check_sha1(fname, sha1_hash):
                    raise UserWarning(
                        "File {} is downloaded but the content hash does not match."
                        " The repo may be outdated or download may be incomplete. "
                        'If the "repo_url" is overridden, consider switching to '
                        "the default repo.".format(fname)
                    )
                break
            except Exception as e:
                retries -= 1
                if retries <= 0:
                    raise e
                else:
                    if log:
                        print(
                            "download failed, retrying, {} attempt{} left".format(
                                retries, "s" if retries > 1 else ""
                            )
                        )

    return fname

from mlx_graphs.datasets.utils.download import download · mlx_graphs/datasets/utils/download.py