import json

INPUT = "/opt/epg/ua/logo.txt"
OUTPUT = "/opt/epg/ua.json"

channels = {}

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

    for line in f:
        filename = line.strip()

        if not filename.lower().endswith(".png"):
            continue

        name = filename.rsplit("/", 1)[-1]

        name = name[:-4]  # odstráni .png

        parts = name.split("-", 1)

        if len(parts) != 2:
            print(f"Preskakujem: {filename}")
            continue

        cid = parts[0]
        channel_name = parts[1]

        if not cid.isdigit():
            print(f"Neplatné ID: {filename}")
            continue

        channel_name = channel_name.replace("-", " ")
        channel_name = channel_name.title()

        display_name = f"{channel_name} ({cid}) UA"

        channels[cid] = display_name

channels = dict(
    sorted(channels.items(), key=lambda x: int(x[0]))
)

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

print("Hotovo.")
print(f"Nájdených kanálov: {len(channels)}")
print(f"Výstup: {OUTPUT}")