error handling, renaming

This commit is contained in:
Georg Perhofer 2022-12-12 11:26:00 +01:00
parent d47d394b79
commit c907599472

View File

@ -138,7 +138,8 @@ class Download:
if "sample" not in parse and parse["sampling_rate"]: if "sample" not in parse and parse["sampling_rate"]:
meta = self.client.get_track_meta(self.item_id) meta = self.client.get_track_meta(self.item_id)
track_title = _get_title(meta) track_title = _get_title(meta)
logger.info(f"\n{YELLOW}Downloading: {track_title}") artist = _safe_get(meta, "performer", "name")
logger.info(f"\n{YELLOW}Downloading: {artist} - {track_title}")
format_info = self._get_format(meta, is_track_id=True, track_url_dict=parse) format_info = self._get_format(meta, is_track_id=True, track_url_dict=parse)
file_format, quality_met, bit_depth, sampling_rate = format_info file_format, quality_met, bit_depth, sampling_rate = format_info
@ -221,8 +222,7 @@ class Download:
logger.info(f"{OFF}{track_title} was already downloaded") logger.info(f"{OFF}{track_title} was already downloaded")
return return
desc = _get_description(track_url_dict, track_title, multiple) tqdm_download(url, filename, filename)
tqdm_download(url, filename, desc)
tag_function = metadata.tag_mp3 if is_mp3 else metadata.tag_flac tag_function = metadata.tag_mp3 if is_mp3 else metadata.tag_flac
try: try:
tag_function( tag_function(
@ -305,21 +305,26 @@ class Download:
return ("Unknown", quality_met, None, None) return ("Unknown", quality_met, None, None)
def tqdm_download(url, fname, track_name): def tqdm_download(url, fname, desc):
r = requests.get(url, allow_redirects=True, stream=True) r = requests.get(url, allow_redirects=True, stream=True)
total = int(r.headers.get("content-length", 0)) total = int(r.headers.get("content-length", 0))
size = 0
with open(fname, "wb") as file, tqdm( with open(fname, "wb") as file, tqdm(
total=total, total=total,
unit="iB", unit="iB",
unit_scale=True, unit_scale=True,
unit_divisor=1024, unit_divisor=1024,
desc=track_name, desc=desc,
bar_format=CYAN + "{n_fmt}/{total_fmt} /// {desc}", bar_format=CYAN + "{n_fmt}/{total_fmt} /// {desc}",
) as bar: ) as bar:
for data in r.iter_content(chunk_size=1024): for data in r.iter_content(chunk_size=1024):
size = file.write(data) size = file.write(data)
bar.update(size) bar.update(size)
if total != size:
# https://stackoverflow.com/questions/69919912/requests-iter-content-thinks-file-is-complete-but-its-not
raise ConnectionError("File download was interrupted.")
def _get_description(item: dict, track_title, multiple=None): def _get_description(item: dict, track_title, multiple=None):
downloading_title = f"{track_title} " downloading_title = f"{track_title} "