import xml.etree.ElementTree as ET
from datetime import datetime, timedelta
RULES = [
{"channel": "Sky Sport F1 DE", "shift": 30, "match": "Vorbericht"}, # Vorberichte später starten
{"channel": "Sky Sport F1 DE", "shift": 1, "match": "Rennen"}, # Rennen leicht nach hinten
{"channel": "Sky Sport F1 DE", "shift": -2, "match": "Qualifying"}, # Qualifying 2 Minuten früher
{"channel": "Sky Sport F1 DE", "shift": 5, "match": "Training"}, # Training 5 Minuten später
{"channel": "Sky Sport F1 DE", "shift": 0, "match": "Warm Up"}, # Warm Up unverändert
{"channel": "Sky Sport F1 DE", "shift": 10, "match": "Highlights"}, # Highlights etwas später
{"channel": "Sky Sport F1 DE", "shift": 0, "match": "Pressekonferenz"}, # PK unverändert
{"channel": "Sky Sport F1 DE", "shift": -1, "match": "Grid"}, # Grid Walk 1 Minute früher
{"channel": "Sky Sport F1 DE", "shift": 2, "match": "Siegerehrung"}, # Podium leicht nach hinten
{"channel": "Sky Sport F1 DE", "shift": 0, "match": "Analyse"}, # Studioanalyse normal
{"channel": "DAZN 1 DE", "shift": 15, "match": ""},
{"channel": "ServusTV DE", "shift": 10, "match": "Countdown"},
{"channel": "Sky Sport Bundesliga 1 DE", "shift": 20, "match": "Bundesliga"},
{"channel": "Eurosport 1 DE", "shift": 5, "match": ""},
{"channel": "RTL DE", "shift": 25, "match": "Formel 1"},
{"channel": "ProSieben DE", "shift": -5, "match": "NFL"},
{"channel": "Sky Sport UHD DE","shift": 30, "match": "Vorbericht"},
{"channel": "Magentasport DE", "shift": 15, "match": "Eishockey"},
{"channel": "Sky Cinema DE", "shift": 0, "match": ""}, # keine Änderung
]
INPUT_XML = "epg.xml"
OUTPUT_XML = "epg_modified.xml"
def shift_time(timestr: str, minutes: int) -> str:
"""Verschiebt XMLTV Zeit um Minuten."""
dt_str, tz = timestr.split(" ")
dt = datetime.strptime(dt_str, "%Y%m%d%H%M%S")
dt_shifted = dt + timedelta(minutes=minutes)
return dt_shifted.strftime("%Y%m%d%H%M%S") + " " + tz
def main():
tree = ET.parse(INPUT_XML)
root = tree.getroot()
total = 0
for prog in root.findall("programme"):
channel = prog.attrib.get("channel", "")
title_el = prog.find("title")
title = title_el.text if title_el is not None else ""
for rule in RULES:
if channel != rule["channel"]:
continue
if rule["match"] and rule["match"].lower() not in title.lower():
continue
old_start, old_stop = prog.attrib["start"], prog.attrib["stop"]
prog.attrib["start"] = shift_time(old_start, rule["shift"])
prog.attrib["stop"] = shift_time(old_stop, rule["shift"])
total += 1
break # Regel angewendet → nächste Sendung
tree.write(OUTPUT_XML, encoding="utf-8", xml_declaration=True)
print(f"{total} Sendungen angepasst. Neue Datei: {OUTPUT_XML}")
if __name__ == "__main__":
main()