mirror of
https://github.com/darkzoul5/YoutubePlaylistSync.git
synced 2026-07-03 12:34:00 +03:00
Refactor file renaming logic to support separate handling of audio and video files in "both" mode
This commit is contained in:
+32
-18
@@ -306,32 +306,46 @@ class PlaylistDownloader:
|
|||||||
print(f"\n{STEP} Renumbering files according to playlist order")
|
print(f"\n{STEP} Renumbering files according to playlist order")
|
||||||
temp_suffix = ".renametemp"
|
temp_suffix = ".renametemp"
|
||||||
|
|
||||||
final_map = {}
|
# --- Build mapping of safe_title → correct filename ---
|
||||||
|
final_map_audio = {}
|
||||||
|
final_map_video = {}
|
||||||
|
|
||||||
for idx, video in enumerate(playlist_entries, start=1):
|
for idx, video in enumerate(playlist_entries, start=1):
|
||||||
title = video.get("title", "[Unknown]")
|
title = video.get("title", "[Unknown]")
|
||||||
safe_title = self.sanitize_title(title, video["id"])
|
safe_title = self.sanitize_title(title, video["id"])
|
||||||
correct_fname = f"{idx:03d} - {safe_title}.mp3"
|
|
||||||
final_map[safe_title] = correct_fname
|
|
||||||
|
|
||||||
for safe_title, correct_fname in final_map.items():
|
if self.download_mode in ("audio", "both"):
|
||||||
matches = list(self.save_path.glob(f"* - {safe_title}.mp3"))
|
final_map_audio[safe_title] = f"{idx:03d} - {safe_title}.mp3"
|
||||||
if matches:
|
if self.download_mode in ("video", "both"):
|
||||||
current_path = matches[0]
|
final_map_video[safe_title] = f"{idx:03d} - {safe_title}.mp4"
|
||||||
if current_path.name != correct_fname:
|
|
||||||
temp_path = current_path.with_suffix(current_path.suffix + temp_suffix)
|
|
||||||
#print(f"Temporarily renaming '{current_path.name}' → '{temp_path.name}'")
|
|
||||||
current_path.rename(temp_path)
|
|
||||||
|
|
||||||
for safe_title, correct_fname in final_map.items():
|
# --- Helper function to rename files in folder ---
|
||||||
temp_match = list(self.save_path.glob(f"* - {safe_title}.mp3{temp_suffix}"))
|
def rename_files(folder, mapping, ext):
|
||||||
if temp_match:
|
folder.mkdir(parents=True, exist_ok=True)
|
||||||
temp_path = temp_match[0]
|
for safe_title, correct_fname in mapping.items():
|
||||||
final_path = self.save_path / correct_fname
|
matches = list(folder.glob(f"*{ext}"))
|
||||||
print(f"Renaming '{temp_path.name}' → '{final_path.name}'")
|
# Find matching file
|
||||||
temp_path.rename(final_path)
|
for m in matches:
|
||||||
|
if safe_title in m.name:
|
||||||
|
if m.name != correct_fname:
|
||||||
|
temp_path = m.with_suffix(m.suffix + temp_suffix)
|
||||||
|
m.rename(temp_path)
|
||||||
|
|
||||||
|
for safe_title, correct_fname in mapping.items():
|
||||||
|
temp_match = list(folder.glob(f"*{ext}{temp_suffix}"))
|
||||||
|
for temp_path in temp_match:
|
||||||
|
final_path = folder / correct_fname
|
||||||
|
print(f"Renaming '{temp_path.name}' → '{final_path.name}'")
|
||||||
|
temp_path.rename(final_path)
|
||||||
|
|
||||||
|
if self.download_mode in ("audio", "both"):
|
||||||
|
rename_files(self.save_path / "audio", final_map_audio, ".mp3")
|
||||||
|
if self.download_mode in ("video", "both"):
|
||||||
|
rename_files(self.save_path / "video", final_map_video, ".mp4")
|
||||||
|
|
||||||
print(f"{OK} Renumbering complete.")
|
print(f"{OK} Renumbering complete.")
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
playlist_id = self.url or self.save_path or "unknown playlist"
|
playlist_id = self.url or self.save_path or "unknown playlist"
|
||||||
if getattr(self, "skip", False):
|
if getattr(self, "skip", False):
|
||||||
|
|||||||
Reference in New Issue
Block a user