def download_url(url, dir_name='<STR_LIT:.>', save_name=None, store_directory=None, messages=True, suffix='<STR_LIT>'):
if sys.version_info>=(<NUM_LIT:3>,<NUM_LIT:0>):<EOL><INDENT>from urllib.parse import quote<EOL>from urllib.request import urlopen <EOL>from urllib.error import HTTPError, URLError<EOL><DEDENT>else:<EOL><INDENT>from urllib2 import quote<EOL>from urllib2 import urlopen<EOL>from urllib2 import URLError as HTTPError<EOL><DEDENT>i = url.rfind('<STR_LIT:/>')<EOL>file = url[i+<NUM_LIT:1>:]<EOL>if store_directory is not None:<EOL><INDENT>dir_name = os.path.join(dir_name, store_directory)<EOL><DEDENT>if save_name is None:<EOL><INDENT>save_name = file<EOL><DEDENT>save_name = os.path.join(dir_name, save_name)<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 HTTPError as e:<EOL><INDENT>if not hasattr(e, "<STR_LIT:code>"):<EOL><INDENT>raise<EOL><DEDENT>if e.code > <NUM_LIT> and e.code<<NUM_LIT>:<EOL><INDENT>raise ValueError('<STR_LIT>' + url + suffix + '<STR_LIT>' + str(e.code))<EOL><DEDENT>elif e.code > <NUM_LIT>:<EOL><INDENT>raise ValueError('<STR_LIT>' + url + suffix + '<STR_LIT>' + str(e.code))<EOL><DEDENT><DEDENT>except URLError as e:<EOL><INDENT>raise ValueError('<STR_LIT>' + url + suffix + '<STR_LIT>' + str(e.reason))<EOL><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>try:<EOL><INDENT>file_size = int(content_length_str)<EOL><DEDENT>except:<EOL><INDENT>try: <EOL><INDENT>file_size = int(content_length_str[<NUM_LIT:0>])<EOL><DEDENT>except:<EOL><INDENT>file_size = None<EOL><DEDENT><DEDENT>if file_size == <NUM_LIT:1>:<EOL><INDENT>file_size = None<EOL><DEDENT><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>percentage = <NUM_LIT:1.>/line_length<EOL>if file_size:<EOL><INDENT>print("<STR_LIT:|>"+"<STR_LIT>".format("<STR_LIT>".format(file_size/(<NUM_LIT>)), ll=line_length)+"<STR_LIT:|>")<EOL>from itertools import cycle<EOL>cycle_str = cycle('<STR_LIT:>>')<EOL>sys.stdout.write("<STR_LIT:|>")<EOL><DEDENT>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>if file_size:<EOL><INDENT>if (float(file_size_dl)/file_size) >= percentage:<EOL><INDENT>sys.stdout.write(next(cycle_str))<EOL>sys.stdout.flush()<EOL>percentage += <NUM_LIT:1.>/line_length<EOL><DEDENT><DEDENT>else:<EOL><INDENT>sys.stdout.write("<STR_LIT:U+0020>"*(len(status)) + "<STR_LIT:\r>")<EOL>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>sys.stdout.write(status)<EOL>sys.stdout.flush()<EOL><DEDENT><DEDENT>if file_size:<EOL><INDENT>sys.stdout.write("<STR_LIT:|>")<EOL>sys.stdout.flush()<EOL><DEDENT>print(status)<EOL><DEDENT>
Download a file from a url and save it to disk.