def download_file(url, output_directory, filename=None): if not filename: local_filename = os.path.join(output_directory, url.split('/')[-1]) else: local_filename = os.path.join(output_directory, filename) dw = Download(url, des=local_filename, overwrite=True, ) dw.download() return local_filename
def test__build_headers(): """Test the _build_headers method""" download = Download(TEST_URL) download._build_headers(1024) header_built = download.headers assert header_built == {"Range": "bytes={}-".format(1024)}, \ "Should be 1024"
def test__format_size(): """ Test the function that formats the size """ download = Download(TEST_URL) size, unit = download._format_size(255678999) # Size should be 243.83449459075928 # and unit should be `MB` size = int(size) assert size == 243, "Should be 243" assert unit == "MB", "Should be MB"
def dw(value, song_name='ytmdl_temp.mp3'): """Download the song.""" try: # Get the audio stream link url = get_youtube_streams(value) # If song_name doesnt have mp3 extension, add it if not song_name.endswith('.mp3'): song_name += '.mp3' # Replace the spaces with hashes song_name = song_name.replace(' ', '#') song_name = song_name.replace('/', '#') # The directory where we will download to. dw_dir = defaults.DEFAULT.SONG_TEMP_DIR if not os.path.exists(dw_dir): os.makedirs(dw_dir) # Name of the temp file name = os.path.join(dw_dir, song_name) # Start downloading the song Download(url, name).download() return name except Exception as e: return e
def progress_handler(d): d_obj = Download('', '') if d['status'] == 'downloading': length = d_obj._get_terminal_length() time_left = d['eta'] f_size_disp, dw_unit = d_obj._format_size(d['downloaded_bytes']) percent = d['downloaded_bytes'] / d['total_bytes'] * 100 speed, s_unit, time_left, time_unit = d_obj._get_speed_n_time( d['downloaded_bytes'], 0, cur_time=d['elapsed'] - 6 ) status = r"%-7s" % ("%s %s" % (round(f_size_disp), dw_unit)) if d['speed'] is not None: speed, s_unit = d_obj._format_speed(d['speed'] / 1000) status += r"| %-3s " % ("%s %s" % (round(speed), s_unit)) status += r"|| ETA: %-4s " % ( "%s %s" % (round(time_left), time_unit)) status = d_obj._get_bar(status, length, percent) status += r" %-4s" % ("{}%".format(round(percent))) stdout.write('\r') stdout.write(status) stdout.flush()
def test__format_time(): """ Test the format time function that formats the passed time into a readable value """ download = Download(TEST_URL) time, unit = download._format_time(2134991) # Time should be 9 days assert int(time) == 9, "Should be 9" assert unit == "d", "Should be d" time, unit = download._format_time(245) # Time should be 4 minutes assert int(time) == 4, "Should be 4" assert unit == "m", "Should be m"
def test_file_integrity(): """ Test the integrity of the downloaded file. We will test the 5MB.zip file which has a hash of `eb08885e3082037a12a42308c521fa3c`. """ HASH = "eb08885e3082037a12a42308c521fa3c" download = Download(TEST_URL) download.download() # Once download is done, check the integrity _hash = md5(open("5MB.zip", "rb").read()).hexdigest() assert _hash == HASH, "Integrity check failed for 5MB.zip" # Remove the file now remove(download.basename)
def test__extract_border_icon(): """Test the _extract_border_icon method""" download = Download(TEST_URL) icon_one = download._extract_border_icon("#") icon_two = download._extract_border_icon("[]") icon_none = download._extract_border_icon("") icon_more = download._extract_border_icon("sdafasdfasdf") assert icon_one == ('#', '#'), "Should be ('#', '#')" assert icon_two == ('[', ']'), "Should be ('[', '])" assert icon_none == ('|', '|'), "Should be ('|', '|')" assert icon_more == ('|', '|'), "Should be ('|', '|')"
def dw(value, proxy=None, song_name='ytmdl_temp.mp3'): """Download the song.""" try: # Get the audio stream link url = get_audio_URL(value, proxy) logger.debug("Audio URL is: {}".format(url)) logger.hold() # If song_name doesn't have mp3 extension, add it if not song_name.endswith('.mp3'): song_name += '.mp3' # Replace the spaces with hashes song_name = stringutils.remove_unwanted_chars(song_name) # The directory where we will download to. dw_dir = defaults.DEFAULT.SONG_TEMP_DIR logger.info("Saving the files to: {}".format(dw_dir)) if not os.path.exists(dw_dir): os.makedirs(dw_dir) # Name of the temp file name = os.path.join(dw_dir, song_name) # Start downloading the song status = Download(url, name).download() if status: return name else: logger.critical("Downloader returned false!") except Exception as e: # traceback.print_exception(e) return e
def _dw(self, url): """ Download the file using a download manager. """ Download(url, self._file_path, icon_done="-", icon_left=' ').download()
def test__preprocess_conn(): """Test the _preprocess_conn method""" download = Download(TEST_URL) download._preprocess_conn() assert download.f_size == 5242880, "Should be 5242880"
def download_video(link, destination): Download(link, destination).download()