def download_url(url, store_directory, save_name=None, messages=True, suffix='<STR_LIT>'):
i = url.rfind('<STR_LIT:/>')<EOL>file = url[i+<NUM_LIT:1>:]<EOL>print(file)<EOL>dir_name = os.path.join(data_path, store_directory)<EOL>if save_name is None: save_name = os.path.join(dir_name, file)<EOL>else: save_name = os.path.join(dir_name, save_name)<EOL>if suffix is None: suffix='<STR_LIT>'<EOL>print("<STR_LIT>", url, "<STR_LIT>", save_name)<EOL>if not os.path.exists(dir_name):<EOL><INDENT>os.makedirs(dir_name)<EOL><DEDENT>try:<EOL><INDENT>response = urlopen(url+suffix)<EOL><DEDENT>except URLError as e:<EOL><INDENT>if not hasattr(e, "<STR_LIT:code>"):<EOL><INDENT>raise<EOL><DEDENT>response = e<EOL>if response.code > <NUM_LIT> and response.code<<NUM_LIT>:<EOL><INDENT>raise ValueError('<STR_LIT>' + url + suffix + '<STR_LIT>' + str(response.code))<EOL><DEDENT>elif response.code > <NUM_LIT>:<EOL><INDENT>raise ValueError('<STR_LIT>' + url + suffix + '<STR_LIT>' + str(response.code))<EOL><DEDENT><DEDENT>with open(save_name, '<STR_LIT:wb>') as f:<EOL><INDENT>meta = response.info()<EOL>content_length_str = meta.get("<STR_LIT>")<EOL>if content_length_str:<EOL><INDENT>file_size = int(content_length_str)<EOL><DEDENT>else:<EOL><INDENT>file_size = None<EOL><DEDENT>status = "<STR_LIT>"<EOL>file_size_dl = <NUM_LIT:0><EOL>block_sz = <NUM_LIT><EOL>line_length=<NUM_LIT:30><EOL>while True:<EOL><INDENT>buff = response.read(block_sz)<EOL>if not buff:<EOL><INDENT>break<EOL><DEDENT>file_size_dl += len(buff)<EOL>f.write(buff)<EOL>sys.stdout.write("<STR_LIT:U+0020>"*(len(status)) + "<STR_LIT:\r>")<EOL>if file_size:<EOL><INDENT>status = r"<STR_LIT>".format(dl=file_size_dl/(<NUM_LIT>),<EOL>full=file_size/(<NUM_LIT>), ll=line_length,<EOL>perc="<STR_LIT:=>"*int(line_length*float(file_size_dl)/file_size))<EOL><DEDENT>else:<EOL><INDENT>status = r"<STR_LIT>".format(dl=file_size_dl/(<NUM_LIT>),<EOL>ll=line_length,<EOL>perc="<STR_LIT:.>"*int(line_length*float(file_size_dl/(<NUM_LIT:10>*<NUM_LIT>))))<EOL><DEDENT>sys.stdout.write(status)<EOL>sys.stdout.flush()<EOL><DEDENT>sys.stdout.write("<STR_LIT:U+0020>"*(len(status)) + "<STR_LIT:\r>")<EOL>print(status)<EOL><DEDENT>
Download a file from a url and save it to disk.