# Rename Winamp playlists for Auacious

import os
import xml.etree.ElementTree as ET
from pathlib import Path

home_dir = Path.home()
playlist_dir = home_dir / "Documents/playlists"           # Replace with your path
new_playlist_dir = home_dir / "Documents/newplaylists"    # Replace with your path
xml_file = home_dir / "Documents/playlists/playlists.xml" # Replace with your path

tree = ET.parse(xml_file)
root = tree.getroot()

for playlist in root.findall(".//playlist"):
    filename = playlist.get("filename")  # e.g. "MyList.m3u8"
    title = playlist.get("title")        # e.g. "My Favorite Songs"

    if not filename or not title:
        continue

    old_path = os.path.join(playlist_dir, filename)
    
    # Clean title for filesystem use
    safe_title = "".join(c for c in title if c not in r'\/:*?"<>|')
    new_filename = safe_title + ".m3u8"
    new_path = os.path.join(new_playlist_dir, new_filename)

    if os.path.exists(old_path):
        print(f"Renaming: {filename} → {new_filename}")
        os.rename(old_path, new_path)
    else:
        print(f"Missing: {filename}")
