def download(url, saveas=None, download_path=os.getcwd(), proxies={}, session=None, add_ext=False, max_try=10, overwrite=True): debug(url=url) debug(saveas=saveas) debug(download_path=download_path) debug(proxies=proxies) if proxies: proxies = proxy(proxies) debug(proxies=proxies) if not session: session = cf if saveas: if saveas.lower() == 'poster' or saveas.lower() == 'thumb': session = cf if not saveas: saveas = "file_" + datetime.strftime(datetime.now(), '%Y%m%d_%H%M%S') if download_path: saveas = os.path.join(download_path, saveas) n = 0 progress_bar = progress.Bar(expected_size=10, label="connecting: ") while 1: try: a = session.get(url, proxies=proxies) progress_bar.show(10) break except: progress_bar.label = "re-connecting: " progress_bar.show(n) n += 1 if n == max_try: print( make_colors("Timeout Expected !", 'lightwhite', 'lightred', ['blink'])) break headers = a.headers length = '' ext = "" debug(headers=headers) content_disposition = headers.get('Content-Disposition') if content_disposition: if 'filename=' in content_disposition: saveas_pre = re.split('attachment|;|filename|=', content_disposition)[-1] if download_path and saveas_pre: saveas = os.path.join(download_path, saveas_pre) length = headers.get('content-length') if length: length = int(length) ext = headers.get('Content-Type') debug(ext=ext) if ext: ext = mimelist.get(ext) label = "downloading " + os.path.basename(saveas) + ": " if add_ext and ext: saveas = saveas + "." + ext debug(add_ext=add_ext) debug(ext=ext) debug(saveas=saveas) if not overwrite: if os.path.isfile(saveas): q = raw_input( make_colors("overwrite file %s ? [y/n]: ", 'lightred', 'lightwhite', ['blink'])) if q == 'y': pass else: try: os.remove(saveas) except: pass if length: with open(saveas, 'wb') as f: for ch in progress.bar( a.iter_content(chunk_size=2391975), label, expected_size=(length / 1024) + 1, ): if ch: f.write(ch) if not os.path.splitext(saveas)[1].lower() == ".zip" and not add_ext: zipobj = ZipFile(saveas + ".zip", 'w') zipobj.write(saveas) zipobj.close() else: print( make_colors("No Length data !", 'lightwhite', 'lightred', ['blink']))
def download(url, download_path = "downloads", saveas = None, proxies = {}, session = None, add_ext = False, max_try = 10, overwrite = True, remove_str = None): debug(url = url) debug(saveas = saveas) debug(download_path = download_path) debug(proxies = proxies) if not os.path.isdir(download_path): os.makedirs(download_path) if proxies: proxies = proxy(proxies) debug(proxies = proxies) if not session: session = cf if saveas: if saveas.lower() == 'poster' or saveas.lower() == 'thumb': session = cf if not saveas: saveas = "file_" + datetime.strftime(datetime.now(), '%Y%m%d_%H%M%S') if download_path: saveas = os.path.join(download_path, saveas) n = 0 progress_bar = progress.Bar(expected_size = 10, label = "connecting: ") error = False while 1: try: a = session.get(url, proxies = proxies) progress_bar.show(10) break except: progress_bar.label = "re-connecting: " progress_bar.show(n) n += 1 if n == max_try: print(make_colors("Timeout Expected !", 'lightwhite', 'lightred', ['blink'])) error = True break if error: return False headers = a.headers length = '' ext = "" debug(headers = headers) #raw_input("Enter to Continue") content_disposition = headers.get('Content-Disposition') if content_disposition: saveas = re.findall('filename+=+"(.*?)"$', content_disposition)[0] if download_path: saveas = os.path.join(download_path, saveas) length = headers.get('content-length') if length: length = int(length) ext = headers.get('Content-Type') debug(ext = ext) if ext: ext = mimelist.get(ext) label = "downloading " + os.path.basename(saveas) + ": " if add_ext and ext: saveas = saveas + "." + ext debug(add_ext = add_ext) debug(ext = ext) debug(saveas = saveas) if not overwrite: if os.path.isfile(saveas): q = raw_input(make_colors("overwrite file %s ? [y/n]: ", 'lightred', 'lightwhite', ['blink'])) if q == 'y': pass else: try: os.remove(saveas) except: pass if remove_str: if isinstance(remove_str, list) or isinstance(remove_str, tuple): for i in remove_str: saveas = re.sub(i, "", saveas) elif isinstance(remove_str, str): debug(saveas = saveas) debug(remove_str = remove_str) saveas = re.sub(remove_str, "", saveas) debug(saveas = saveas) if length: with open(saveas, 'wb') as f: for ch in progress.bar(a.iter_content(chunk_size = 2391975), label, expected_size = (length/1024) + 1, ): if ch: f.write(ch) if not os.path.splitext(saveas)[1].lower() == ".torrent" and not add_ext: saveas = saveas + ".torrent" else: print(make_colors("No Length data !", 'lightwhite', 'lightred', ['blink'])) print(make_colors("Save As:", 'white', 'red') + " " + make_colors(saveas, 'red', 'white')) with open(saveas, 'wb') as f: f.write(a.content) return saveas