70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
import xml.etree.ElementTree as ET
|
||
from pathlib import Path
|
||
|
||
# Default XML content to write if missing
|
||
DEFAULT_XML = """<?xml version="1.0" encoding="UTF-8"?>
|
||
<config>
|
||
<path_mappings>
|
||
<map from="P:\\tv" to="/mnt/plex/tv" />
|
||
<map from="P:\\anime" to="/mnt/plex/anime" />
|
||
</path_mappings>
|
||
<services>
|
||
<sonarr>
|
||
<url>http://localhost:8989</url>
|
||
<api_key>YOUR_SONARR_API_KEY</api_key>
|
||
<new_release_group>CONVERTED</new_release_group>
|
||
</sonarr>
|
||
<radarr>
|
||
<url>http://localhost:7878</url>
|
||
<api_key>YOUR_RADARR_API_KEY</api_key>
|
||
<new_release_group>CONVERTED</new_release_group>
|
||
</radarr>
|
||
</services>
|
||
</config>
|
||
"""
|
||
|
||
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
|
||
}
|