import re

INPUT = "/opt/epg/czsk2.m3u"
OUTPUT = "/opt/epg/czsk3.m3u"

with open(INPUT, "r", encoding="utf-8") as f:
    lines = f.readlines()

out = []

for line in lines:
    if line.startswith("#EXTINF:"):
        match = re.search(r'tvg-name=".*?\((\d+)\)', line)

        if match:
            channel_id = match.group(1)

            # pridá tvg-id hneď za #EXTINF:-1
            line = re.sub(
                r'(#EXTINF:-1)(\s+)',
                rf'\1 tvg-id="{channel_id}"\2',
                line,
                count=1
            )

    out.append(line)

with open(OUTPUT, "w", encoding="utf-8") as f:
    f.writelines(out)

print("Hotovo.")