import re
import json

INPUT_M3U = "playlist.m3u"
OUTPUT_JSON = "channels_hungary.json"

channels = {}

with open(INPUT_M3U, "r", encoding="utf-8", errors="ignore") as f:
    for line in f:
        if line.startswith("#EXTINF"):

            id_match = re.search(r'tvg-id="(\d+)"', line)
            name_match = re.search(r',(.+)$', line)

            if not id_match or not name_match:
                continue

            cid = id_match.group(1)
            name = name_match.group(1).strip()

            # odstráni číslo v zátvorke na konci
            name = re.sub(r'\s*\(\d+\)\s*$', '', name)

            channels[cid] = name

with open(OUTPUT_JSON, "w", encoding="utf-8") as f:
    json.dump(channels, f, ensure_ascii=False, indent=4)

print(f"Hotovo. Uložených {len(channels)} kanálov.")