Download episodes and provide regular updates

This commit is contained in:
hygienic-books 2022-03-17 17:37:57 +01:00
parent 47bb1f761a
commit 34ef348929
2 changed files with 15 additions and 3 deletions

View File

@ -9,6 +9,7 @@ state_file_name_suffix = .log
mvw_endpoint = http://localhost:8000/api/query mvw_endpoint = http://localhost:8000/api/query
title_dedup_winner = first title_dedup_winner = first
dl_progress_update_interval = 10 dl_progress_update_interval = 10
dl_threads = 2
[maus] [maus]
min_duration = 1200 min_duration = 1200

View File

@ -89,7 +89,8 @@ class CONST(object):
{"key": "state_file_name_suffix", "value": ".log"}, {"key": "state_file_name_suffix", "value": ".log"},
{"key": "mvw_endpoint", "value": "http://localhost:8000/api/query"}, {"key": "mvw_endpoint", "value": "http://localhost:8000/api/query"},
{"key": "title_dedup_winner", "value": "first"}, {"key": "title_dedup_winner", "value": "first"},
{"key": "dl_progress_update_interval", "value": "10"} {"key": "dl_progress_update_interval", "value": "10"},
{"key": "dl_threads", "value": "2"}
] ]
CFG_KNOWN_SECTION = [ CFG_KNOWN_SECTION = [
{"key": "min_duration", "is_mandatory": False}, {"key": "min_duration", "is_mandatory": False},
@ -355,11 +356,15 @@ def copy_url(
global download_start_time global download_start_time
global download_last_update_time global download_last_update_time
global size_downloaded global size_downloaded
update_interval = config_obj.getint(section_name, "dl_progress_update_interval") update_interval = config_obj.getint(section_name, "dl_progress_update_interval")
max_quality_url = video_metadata["url"] max_quality_url = video_metadata["url"]
filename = max_quality_url.split("/")[-1] filename = max_quality_url.split("/")[-1]
dest_path = os.path.join("./", filename) dest_path = os.path.join("./", filename)
show_name = f"{show.topic} - {show.title}"
with open(dest_path, "wb") as dest_file: with open(dest_path, "wb") as dest_file:
log.info(f"""Downloading "{show_name}" ...""")
r = requests.get(max_quality_url, stream=True) r = requests.get(max_quality_url, stream=True)
for chunk in r.iter_content(32768): for chunk in r.iter_content(32768):
size_downloaded += len(chunk) size_downloaded += len(chunk)
@ -375,8 +380,9 @@ def copy_url(
log.debug(f"Downloaded {human_pct}% ({human_size_dl}/{human_total_dl} at an average " log.debug(f"Downloaded {human_pct}% ({human_size_dl}/{human_total_dl} at an average "
f"{human_dl_speed_so_far})") f"{human_dl_speed_so_far})")
if done_event.is_set(): if done_event.is_set():
log.debug(f"done_event") log.info(f"""Download of "{show_name}" interrupted""")
return return
log.info(f"""Download of "{show_name}" done""")
def get_max_quality_url( def get_max_quality_url(
@ -403,9 +409,13 @@ def download_media(
section_name: str, section_name: str,
config_obj: configparser.ConfigParser(), config_obj: configparser.ConfigParser(),
json_obj: MVWJSONResponse) -> None: json_obj: MVWJSONResponse) -> None:
global download_start_time global download_start_time
global download_last_update_time global download_last_update_time
dl_threads = config_obj.getint(section_name, "dl_threads")
video_metadata = {} video_metadata = {}
for result in json_obj.result.results.copy(): for result in json_obj.result.results.copy():
max_quality_url = get_max_quality_url(result) max_quality_url = get_max_quality_url(result)
content_length = get_content_length(max_quality_url) content_length = get_content_length(max_quality_url)
@ -414,7 +424,8 @@ def download_media(
for video in video_metadata: for video in video_metadata:
total_content_length += video_metadata[video]["content_length"] total_content_length += video_metadata[video]["content_length"]
video_metadata["total_content_length"] = total_content_length video_metadata["total_content_length"] = total_content_length
with ThreadPoolExecutor(max_workers=2) as pool: log.info(f"Limiting parallel downloads to {dl_threads} ...")
with ThreadPoolExecutor(max_workers=dl_threads) as pool:
download_last_update_time = time.time() download_last_update_time = time.time()
download_start_time = download_last_update_time download_start_time = download_last_update_time
update_interval = config_obj.getint(section_name, "dl_progress_update_interval") update_interval = config_obj.getint(section_name, "dl_progress_update_interval")