diff --git a/apps/musicseerr/tasks.py b/apps/musicseerr/tasks.py index 1e648a8..6b10b16 100644 --- a/apps/musicseerr/tasks.py +++ b/apps/musicseerr/tasks.py @@ -229,8 +229,8 @@ def send_telegram_notification(chat_id: int, text: str): def symlink_existing_tracks(temp_dir: str): """ - Finds all MP3 files recursively in /remote-music/music and creates symlinks to them - in the temp_dir so spotDL and yt-dlp skip downloading duplicates. + Finds all audio files recursively in /remote-music/music, extracts artist and title, + and creates symlinks named "Artist - Title.mp3" in temp_dir so spotDL skips them. """ remote_base = "/remote-music/music" if not os.path.isdir(remote_base): @@ -240,15 +240,39 @@ def symlink_existing_tracks(temp_dir: str): count = 0 for root, dirs, files in os.walk(remote_base): for file in files: - if file.endswith(".mp3"): - src_path = os.path.join(root, file) - dest_path = os.path.join(temp_dir, file) + if file.lower().endswith((".mp3", ".flac", ".m4a", ".ogg", ".wav", ".opus")): + # Try to clean/extract artist and title + base_name = os.path.splitext(file)[0] + + # Strip leading numbers like "01. ", "01 - ", etc. + cleaned = re.sub(r'^\d+[\s.-]+', '', base_name).strip() + + # We expect the file to have a " - " separator + if " - " in cleaned: + parts = cleaned.split(" - ", 1) + artist = parts[0].strip() + title = parts[1].strip() + + # Target filename that spotDL expects + target_name = f"{artist} - {title}.mp3" + else: + # Fallback to direct name with .mp3 extension + target_name = f"{cleaned}.mp3" + + # Replaces invalid chars + target_name = "".join([c for c in target_name if c.isalnum() or c in " .-_()"]).strip() + if not target_name.endswith(".mp3"): + target_name += ".mp3" + + dest_path = os.path.join(temp_dir, target_name) if not os.path.exists(dest_path): try: + src_path = os.path.join(root, file) os.symlink(src_path, dest_path) count += 1 except Exception as e: - logger.warning(f"Failed to create symlink for {file}: {e}") + logger.warning(f"Failed to create symlink for {file} -> {target_name}: {e}") + logger.info(f"Created {count} symlinks for existing tracks.")