From 66556eac0a59b9b7d32e0c1b7665a92624d6f848 Mon Sep 17 00:00:00 2001 From: Rafael Moraes <50295204+glomatico@users.noreply.github.com> Date: Fri, 13 Sep 2024 21:20:20 -0300 Subject: [PATCH] update `update_playlist_file` --- gamdl/cli.py | 18 ++++++++++++------ gamdl/downloader.py | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/gamdl/cli.py b/gamdl/cli.py index 3ed81e3..42fd3d3 100644 --- a/gamdl/cli.py +++ b/gamdl/cli.py @@ -746,12 +746,18 @@ def main( downloader.apply_tags(remuxed_path, tags, cover_url) logger.debug(f'Moving to "{final_path}"') downloader.move_to_output_path(remuxed_path, final_path) - if save_playlist and download_queue.playlist_attributes: - playlist_file_path = downloader.get_playlist_file_path(tags) - logger.debug( - f'Updating M3U8 playlist from "{playlist_file_path}"' - ) - downloader.update_playlist_file(playlist_file_path, final_path) + if ( + not synced_lyrics_only + and save_playlist + and download_queue.playlist_attributes + ): + playlist_file_path = downloader.get_playlist_file_path(tags) + logger.debug(f'Updating M3U8 playlist from "{playlist_file_path}"') + downloader.update_playlist_file( + playlist_file_path, + final_path, + playlist_track, + ) except Exception as e: error_count += 1 logger.error( diff --git a/gamdl/downloader.py b/gamdl/downloader.py index a527590..37a7f4e 100644 --- a/gamdl/downloader.py +++ b/gamdl/downloader.py @@ -283,6 +283,7 @@ class Downloader: self, playlist_file_path: Path, final_path: Path, + playlist_track: int, ): playlist_file_path.parent.mkdir(parents=True, exist_ok=True) playlist_file_path_parent_parts_len = len(playlist_file_path.parent.parts) @@ -291,8 +292,18 @@ class Downloader: ("../" * (playlist_file_path_parent_parts_len - output_path_parts_len)), *final_path.parts[output_path_parts_len:], ) - with playlist_file_path.open("a", encoding="utf8") as playlist_file: - playlist_file.write(final_path_relative.as_posix() + "\n") + playlist_file_lines = ( + playlist_file_path.open("r", encoding="utf8").readlines() + if playlist_file_path.exists() + else [] + ) + if len(playlist_file_lines) < playlist_track: + playlist_file_lines.extend( + "\n" for _ in range(playlist_track - len(playlist_file_lines)) + ) + playlist_file_lines[playlist_track - 1] = final_path_relative.as_posix() + "\n" + with playlist_file_path.open("w", encoding="utf8") as playlist_file: + playlist_file.writelines(playlist_file_lines) @staticmethod def millis_to_min_sec(millis):