Skip to content

Instantly share code, notes, and snippets.

@Ravencentric
Created March 4, 2024 17:15
Show Gist options
  • Select an option

  • Save Ravencentric/9b7d085a19a92e34396435b5cb9ae836 to your computer and use it in GitHub Desktop.

Select an option

Save Ravencentric/9b7d085a19a92e34396435b5cb9ae836 to your computer and use it in GitHub Desktop.
Download a file using requests with a fancy rich progress bar
from __future__ import annotations
from pathlib import Path
import requests
from rich.progress import (
BarColumn,
DownloadColumn,
Progress,
SpinnerColumn,
TextColumn,
TimeRemainingColumn,
TransferSpeedColumn,
)
def download(url: str, output: str | Path | None = None, chunksize: int = 128) -> Path:
"""
Download a file from the specified URL and save it to the specified location.
Parameters
----------
url : str
The URL of the file to be downloaded.
output : str or Path or None, optional
The local path where the downloaded file will be saved. If not provided,
the file will be saved in the current working directory with the same name
as the file in the URL. Defaults to None.
chunksize : int, optional
The size of each data chunk to be downloaded in bytes. Defaults to 128.
Returns
-------
Path
The resolved path to the downloaded file.
Examples
--------
>>> url = "https://github.com/pallets/flask/releases/download/3.0.2/flask-3.0.2.tar.gz"
>>> output = "~/downloads/flask.tar.gz"
>>> download(url)
PosixPath('/home/username/cwd/flask-3.0.2.tar.gz')
>>> download(url, output=output)
PosixPath('/home/username/downloads/flask.tar.gz')
"""
response = requests.get(url, stream=True)
total = int(response.headers.get("content-length", 0))
with Progress(
SpinnerColumn(),
TextColumn("[bold blue] Downloading {task.fields[filename]}"),
BarColumn(),
"[progress.percentage]{task.percentage:>3.1f}%",
"•",
DownloadColumn(),
"•",
TransferSpeedColumn(),
"•",
TimeRemainingColumn(),
) as progress:
filename = url.split("/")[-1]
task = progress.add_task("download", filename=filename, total=total)
output = (Path(output) if output else Path.cwd() / filename).expanduser().resolve()
with open(output, "wb") as file:
for chunk in response.iter_content(chunk_size=chunksize):
file.write(chunk)
progress.update(task, advance=len(chunk))
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment