ubelt.util_download module

Helpers for downloading data

ubelt.util_download.download(url, fpath=None, hash_prefix=None, chunksize=8192, verbose=1)[source]

downloads a url to a fpath.

Parameters:
  • url (str) – url to download
  • fpath (str) – path to download to. Defaults to basename of url and ubelt’s application cache.
  • chunksize (int) – download chunksize
  • verbose (bool) – verbosity

Notes

Original code taken from pytorch in torch/utils/model_zoo.py and slightly modified.

References

http://blog.moleculea.com/2012/10/04/urlretrieve-progres-indicator/ http://stackoverflow.com/questions/15644964/python-progress-bar-and-downloads http://stackoverflow.com/questions/16694907/how-to-download-large-file-in-python-with-requests-py

Example

>>> from ubelt.util_download import *  # NOQA
>>> url = 'http://i.imgur.com/rqwaDag.png'
>>> fpath = download(url)
>>> print(basename(fpath))
rqwaDag.png
ubelt.util_download.grabdata(url, fpath=None, dpath=None, fname=None, redo=False, verbose=1, appname=None, **download_kw)[source]

Downloads a file, caches it, and returns its local path.

Parameters:
  • url (str) – url to the file to download
  • fpath (str) – The full path to download the file to. If unspecified, the arguments dpath and fname are used to determine this.
  • dpath (str) – where to download the file. If unspecified appname is used to determine this. Mutually exclusive with fpath.
  • fname (str) – What to name the downloaded file. Defaults to the url basename. Mutually exclusive with fpath.
  • redo (bool) – if True forces redownload of the file (default = False)
  • verbose (bool) – verbosity flag (default = True)
  • appname (str) – set dpath to ub.get_app_cache_dir(appname). Mutually exclusive with dpath and fpath.
  • **download_kw – additional kwargs to pass to ub.download
Returns:

fpath - file path string

Return type:

str

Example

>>> import ubelt as ub
>>> file_url = 'http://i.imgur.com/rqwaDag.png'
>>> lena_fpath = ub.grabdata(file_url, fname='mario.png')
>>> result = basename(lena_fpath)
>>> print(result)
mario.png