Download a file from URL, including Google Drive shared URL. Args: url (str, optional): Google Drive URL is also supported. Defaults to None. output (str, optional): Output filename. Default is basename of URL. quiet (bool, optional): Suppress terminal output. Default is
(
url=None,
output=None,
quiet=False,
proxy=None,
speed=None,
use_cookies=True,
verify=True,
id=None,
fuzzy=False,
resume=False,
unzip=True,
overwrite=False,
subfolder=False,
)
| 90 | |
| 91 | |
| 92 | def download_file( |
| 93 | url=None, |
| 94 | output=None, |
| 95 | quiet=False, |
| 96 | proxy=None, |
| 97 | speed=None, |
| 98 | use_cookies=True, |
| 99 | verify=True, |
| 100 | id=None, |
| 101 | fuzzy=False, |
| 102 | resume=False, |
| 103 | unzip=True, |
| 104 | overwrite=False, |
| 105 | subfolder=False, |
| 106 | ): |
| 107 | """Download a file from URL, including Google Drive shared URL. |
| 108 | |
| 109 | Args: |
| 110 | url (str, optional): Google Drive URL is also supported. Defaults to None. |
| 111 | output (str, optional): Output filename. Default is basename of URL. |
| 112 | quiet (bool, optional): Suppress terminal output. Default is False. |
| 113 | proxy (str, optional): Proxy. Defaults to None. |
| 114 | speed (float, optional): Download byte size per second (e.g., 256KB/s = 256 * 1024). Defaults to None. |
| 115 | use_cookies (bool, optional): Flag to use cookies. Defaults to True. |
| 116 | verify (bool | str, optional): Either a bool, in which case it controls whether the server's TLS certificate is verified, or a string, |
| 117 | in which case it must be a path to a CA bundle to use. Default is True.. Defaults to True. |
| 118 | id (str, optional): Google Drive's file ID. Defaults to None. |
| 119 | fuzzy (bool, optional): Fuzzy extraction of Google Drive's file Id. Defaults to False. |
| 120 | resume (bool, optional): Resume the download from existing tmp file if possible. Defaults to False. |
| 121 | unzip (bool, optional): Unzip the file. Defaults to True. |
| 122 | overwrite (bool, optional): Overwrite the file if it already exists. Defaults to False. |
| 123 | subfolder (bool, optional): Create a subfolder with the same name as the file. Defaults to False. |
| 124 | |
| 125 | Returns: |
| 126 | str: The output file path. |
| 127 | """ |
| 128 | import zipfile |
| 129 | |
| 130 | try: |
| 131 | import gdown |
| 132 | except ImportError: |
| 133 | print( |
| 134 | "The gdown package is required for this function. Use `pip install gdown` to install it." |
| 135 | ) |
| 136 | return |
| 137 | |
| 138 | if output is None: |
| 139 | if isinstance(url, str) and url.startswith("http"): |
| 140 | output = os.path.basename(url) |
| 141 | |
| 142 | out_dir = os.path.abspath(os.path.dirname(output)) |
| 143 | if not os.path.exists(out_dir): |
| 144 | os.makedirs(out_dir) |
| 145 | |
| 146 | if isinstance(url, str): |
| 147 | if os.path.exists(os.path.abspath(output)) and (not overwrite): |
| 148 | print( |
| 149 | f"{output} already exists. Skip downloading. Set overwrite=True to overwrite." |
no test coverage detected