|
| 1 | +import requests |
| 2 | +from tqdm import tqdm |
| 3 | +import zipfile |
| 4 | +import os |
| 5 | +import shutil |
| 6 | + |
| 7 | + |
| 8 | +def download(url: str, path: str = None, unzip=False, unzip_path: str = None, force_download=False, force_unzip=False, clean=False) -> None: |
| 9 | + """ |
| 10 | + Download a file from a OneDrive url. |
| 11 | +
|
| 12 | + :param url: The url to download from (should end with '?download=1'). |
| 13 | + :param path: The path to save the file to. Default: download to the current directory. |
| 14 | + :param unzip: Whether to unzip the file. |
| 15 | + :param unzip_path: The path to unzip the file to. Default is `path` + '_unzipped'. |
| 16 | + :param force_download: Whether to force download the file even if it already exists. |
| 17 | + :param force_unzip: Whether to force unzip the file even if it already exists. |
| 18 | + :param clean: Whether to clean the unzipped files after unzipping. |
| 19 | + """ |
| 20 | + |
| 21 | + def _create_if_not_exists(path: str, remove=True) -> None: |
| 22 | + if os.path.exists(path): |
| 23 | + if not remove: |
| 24 | + return |
| 25 | + else: |
| 26 | + shutil.rmtree(path) |
| 27 | + |
| 28 | + if len(os.path.split(path)[1].split(".")) > 1: |
| 29 | + path = os.path.split(path) |
| 30 | + |
| 31 | + os.makedirs(path, exist_ok=True) |
| 32 | + |
| 33 | + if not url.endswith("?download=1"): |
| 34 | + # replace everithing after the last ? with ?download=1 |
| 35 | + url = url.split("?")[0] + "?download=1" |
| 36 | + |
| 37 | + try: |
| 38 | + response = requests.get(url, stream=True) |
| 39 | + total_size_in_bytes = int(response.headers.get('content-length', 0)) |
| 40 | + progress_bar = tqdm(total=total_size_in_bytes, |
| 41 | + unit='iB', unit_scale=True) |
| 42 | + block_size = 1024 |
| 43 | + |
| 44 | + if path is None: |
| 45 | + # get current working directory |
| 46 | + path = os.getcwd() |
| 47 | + |
| 48 | + if not os.path.exists(path) or force_download: |
| 49 | + _create_if_not_exists(path) |
| 50 | + |
| 51 | + with open(path, 'wb') as f: |
| 52 | + for data in response.iter_content(block_size): |
| 53 | + progress_bar.update(len(data)) |
| 54 | + f.write(data) |
| 55 | + progress_bar.close() |
| 56 | + |
| 57 | + if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes: |
| 58 | + raise Exception( |
| 59 | + f"ERROR, something went wrong during download!\nExpected {total_size_in_bytes} Bytes, got {progress_bar.n} Bytes.") |
| 60 | + |
| 61 | + # unzip file if necessary |
| 62 | + if unzip: |
| 63 | + if unzip_path is None: |
| 64 | + unzip_path = path + '_unzipped' |
| 65 | + |
| 66 | + if not os.path.exists(unzip_path) or force_unzip: |
| 67 | + print("Extracting files...") |
| 68 | + |
| 69 | + _create_if_not_exists(path, remove=True) |
| 70 | + with zipfile.ZipFile(path, 'r') as zip_ref: |
| 71 | + zip_ref.extractall(unzip_path) |
| 72 | + |
| 73 | + if clean: |
| 74 | + os.remove(path) |
| 75 | + |
| 76 | + except Exception as e: |
| 77 | + print(e) |
| 78 | + raise Exception("ERROR, something went wrong, see error above!") |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + ln = "https://unimore365-my.sharepoint.com/<path>?download=1" |
| 83 | + print('Downloading dataset') |
| 84 | + download(ln, dest_path="files.zip", unzip=True, unzip_path="./data") |
0 commit comments