diff --git a/mvw-dl.py b/mvw-dl.py index 2cff5f4..b26d7d4 100644 --- a/mvw-dl.py +++ b/mvw-dl.py @@ -339,11 +339,11 @@ signal.signal(signal.SIGINT, handle_sigint) def copy_url( - show: type_def.mvw_json_response.Show) -> None: + show: type_def.mvw_json_response.Show, + max_quality_url: str) -> None: """Copy data from a url to a local file.""" - url = show.url_video_hd - filename = url.split("/")[-1] + filename = max_quality_url.split("/")[-1] dest_path = os.path.join("./", filename) release_timestamp = d.datetime.utcfromtimestamp(show.timestamp).strftime('%A %x %X') #s = requests.Session() @@ -355,7 +355,7 @@ def copy_url( log.debug(f"""Downloading "{show_name}" posted {release_timestamp} ...""") with open(dest_path, "wb") as dest_file: last_update_time = time.time() - r = requests.get(url, stream=True) + r = requests.get(max_quality_url, stream=True) total_length = int(r.headers.get('content-length')) size_downloaded = 0 for chunk in r.iter_content(32768): @@ -401,16 +401,28 @@ def copy_url( # pool.submit(copy_url, task_id, url, dest_path) +def get_max_quality_url( + show: type_def.mvw_json_response.Show) -> str: + if show.url_video_hd: + max_quality_url = show.url_video_hd + elif show.url_video: + max_quality_url = show.url_video + else: + max_quality_url = show.url_video_low + return max_quality_url + + def download_media( section_name: str, - config_obj: configparser.ConfigParser()) -> None: + config_obj: configparser.ConfigParser(), + json_obj: MVWJSONResponse) -> None: with ThreadPoolExecutor(max_workers=2) as pool: - for result in json_response.result.results.copy(): + for result in json_obj.result.results.copy(): # filename = url.split("/")[-1] # dest_path = os.path.join(dest_dir, filename) # task_id = progress.add_task("download", filename=filename, start=False) - pool.submit(copy_url, result) - # TODO before sending into pool validate which url we're going to use + max_quality_url = get_max_quality_url(result) + pool.submit(copy_url, result, max_quality_url) # TODO from each url get total content-length # TODO use total content-length for overall progress of what we want to download pass @@ -444,6 +456,6 @@ if __name__ == '__main__': json_response = dedup_json_titles(section, config, json_response) log.debug(f"Downloading shows ...") - download_media(section, config) + download_media(section, config, json_response) # console.print_json(json_response.json())