[docs]defdecompress_zip(filename,dest:str,verbose:bool=False)->List[str]:""" Unzips a zip file. :param filename: file to process :param dest: destination :param verbose: verbosity :return: return the list of decompressed files """try:fp=zipfile.ZipFile(filename,"r")exceptzipfile.BadZipFilease:raiseRuntimeError(f"Unable to unzip {filename!r}")fromefiles=[]forinfoinfp.infolist():ifnotos.path.exists(info.filename):data=fp.read(info.filename)tos=os.path.join(dest,info.filename)ifnotos.path.exists(tos):finalfolder=os.path.split(tos)[0]ifnotos.path.exists(finalfolder):ifverbose:print(f"creating folder {finalfolder!r}")os.makedirs(finalfolder)ifnotinfo.filename.endswith("/"):withopen(tos,"wb")asu:u.write(data)files.append(tos)ifverbose:print(f"unzipped {info.filename!r} to {tos!r}")elifnottos.endswith("/"):files.append(tos)elifnotinfo.filename.endswith("/"):files.append(info.filename)returnfiles
defdownload(url:str,dest:str=".",timeout:int=10,verbose:bool=False)->str:""" Download one file. :param url: url :param dest: destination folder :param timeout: timeout :param verbose: display progress :return: filename """filename=url.split("/")[-1]dest_zip=os.path.join(dest,filename)ifnotos.path.exists(dest_zip):ifverbose:print(f"downloads into {dest_zip!r} from {url!r}")withurlopen(url,timeout=timeout)asu:content=u.read()withopen(dest_zip,"wb")asf:f.write(content)elifverbose:print(f"already downloaded {dest_zip!r}")returndest_zip
[docs]defdownload_and_unzip(url:str,dest:str=".",timeout:int=10,verbose:bool=False)->List[str]:""" Downloads a file and unzip it. :param url: url :param dest: destination folder :param timeout: timeout :param verbose: display progress :return: list of unzipped files """filename=url.split("/")[-1]dest_zip=os.path.join(dest,filename)ifnotos.path.exists(dest_zip):ifverbose:print(f"downloads into {dest_zip!r} from {url!r}")withurlopen(url,timeout=timeout)asu:content=u.read()withopen(dest_zip,"wb")asf:f.write(content)elifverbose:print(f"already downloaded {dest_zip!r}")returndecompress_zip(dest_zip,dest,verbose=verbose)