[docs]defdownload_from_url(url,destination_path=None):"""**Download Files from URLs** Download a file from the given URL and save it to the destination path. Parameters ---------- url : str The URL of the file to download. destination_path : str, Path The path to which the file will be downloaded. If None, the file name will be taken from the last part of the URL path and downloaded to the current working directory. Returns ------- bool True if the file was downloaded successfully, False otherwise. """destination_path=_download_path_sanitize(url,destination_path)# Download the fileresponse=urllib.request.urlopen(url)ifresponse.status_code==200:withdestination_path.open("wb")asfile:file.write(response.content)returnTrueelse:returnFalse
[docs]defdownload_zip(url,destination_path=None,unzip=True):"""**Download ZIP files** Download a ZIP file from a URL and extract it to a destination directory. Parameters ---------- url : str The URL of the ZIP file to download. destination_path : str, Path The path to which the ZIP file will be extracted. If None, the folder name will be taken from the last part of the URL path and downloaded to the current working directory. unzip : bool Whether to unzip the file or not. Defaults to True. Returns ------- bool True if the ZIP file was downloaded successfully, False otherwise. """destination_path=_download_path_sanitize(url,destination_path)destination_directory=pathlib.Path(destination_path).parent# Ensure that the destination path is a Path object ending with ".zip"zip_filepath=pathlib.Path(destination_path)ifzip_filepath.suffix!=".zip":zip_filepath=pathlib.Path(zip_filepath.parent,zip_filepath.name+".zip")# Download the ZIP filedownload_successful=download_from_url(url,zip_filepath)ifdownload_successful:ifunzip:# Extract the ZIP filewithzipfile.ZipFile(zip_filepath,"r")aszip_ref:extracted_folder_name=pathlib.Path(zip_ref.namelist()[0]).parts[0]# Extract the contentszip_ref.extractall(destination_directory)# Rename the extracted folder to the desired nameextracted_folder_path=destination_directory/extracted_folder_namenew_folder_path=(destination_directory/pathlib.Path(destination_path).name)extracted_folder_path.rename(new_folder_path)# Clean up by removing the downloaded ZIP filezip_filepath.unlink()returnTrueelse:returnFalse
def_download_path_sanitize(url,destination_path=None):"""Sanitize the destination path of a file to be downloaded from a URL. Parameters ---------- url : str The URL of the file to download. destination_path : str, Path The path to which the file will be downloaded. If None, the file name will be taken from the last part of the URL path and downloaded to the current working directory. Returns ------- Path The sanitized destination path. """ifdestination_pathisNone:# Name the file to be downloaded after the last part of the URL pathurl_parts=urllib.parse.urlsplit(url)destination_path=pathlib.Path(url_parts.path).name# Ensure that the destination path is a Path objectdestination_path=pathlib.Path(destination_path)# Create the destination directory if it does not existdestination_directory=destination_path.parentpathlib.Path(destination_directory).mkdir(parents=True,exist_ok=True)returndestination_path