import xml.etree.ElementTree as ET from pathlib import Path # Default XML content to write if missing DEFAULT_XML = """ http://localhost:8989 YOUR_SONARR_API_KEY CONVERTED http://localhost:7878 YOUR_RADARR_API_KEY CONVERTED """ def load_config_xml(path: Path) -> dict: if not path.exists(): path.write_text(DEFAULT_XML, encoding="utf-8") print(f"ℹ️ Created default config.xml at {path}") tree = ET.parse(path) root = tree.getroot() # --- Path Mappings --- path_mappings = [] for m in root.findall("path_mappings/map"): f = m.attrib.get("from") t = m.attrib.get("to") if f and t: path_mappings.append({"from": f, "to": t}) # --- Services (Sonarr/Radarr) --- services = {"sonarr": {}, "radarr": {}} sonarr_elem = root.find("services/sonarr") if sonarr_elem is not None: url_elem = sonarr_elem.find("url") api_elem = sonarr_elem.find("api_key") rg_elem = sonarr_elem.find("new_release_group") services["sonarr"] = { "url": url_elem.text if url_elem is not None and url_elem.text else None, "api_key": api_elem.text if api_elem is not None and api_elem.text else None, "new_release_group": rg_elem.text if rg_elem is not None and rg_elem.text else "CONVERTED" } radarr_elem = root.find("services/radarr") if radarr_elem is not None: url_elem = radarr_elem.find("url") api_elem = radarr_elem.find("api_key") rg_elem = radarr_elem.find("new_release_group") services["radarr"] = { "url": url_elem.text if url_elem is not None and url_elem.text else None, "api_key": api_elem.text if api_elem is not None and api_elem.text else None, "new_release_group": rg_elem.text if rg_elem is not None and rg_elem.text else "CONVERTED" } return { "path_mappings": path_mappings, "services": services }