File size: 2,759 Bytes
44ecdc3
 
 
 
 
 
 
3cdc146
44ecdc3
3cdc146
44ecdc3
 
 
 
756e073
3cdc146
 
 
 
 
44ecdc3
3cdc146
44ecdc3
3cdc146
 
 
 
 
 
 
 
 
 
 
 
44ecdc3
 
3cdc146
44ecdc3
 
3cdc146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44ecdc3
3cdc146
 
 
44ecdc3
3cdc146
 
 
 
 
44ecdc3
 
 
 
 
 
3cdc146
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
import gradio as gr
import tempfile
import os

API_URL = "https://de1.api.radio-browser.info/json/stations"

# Fetch full list of stations from the API

def fetch_stations():
    try:
        response = requests.get(API_URL)
        response.raise_for_status()
        stations = response.json()
        count = len(stations)
        print(f"Starting fetch of {count} stations...")
        return stations, None
    except Exception as e:
        print(f"Error fetching stations: {e}")
        return None, str(e)

# Write stations to M3U file

def save_as_m3u(stations):
    tmp_dir = tempfile.mkdtemp()
    file_path = os.path.join(tmp_dir, "radio_stations.m3u")
    with open(file_path, "w", encoding="utf-8") as f:
        f.write("#EXTM3U\n")
        for station in stations:
            name = station.get("name", "Unknown")
            url = station.get("url_resolved", station.get("url", ""))
            f.write(f"#EXTINF:-1,{name}\n")
            f.write(f"{url}\n")
    print(f"M3U file created at {file_path} with {len(stations)} entries.")
    return file_path

# Gradio interface

def create_app():
    with gr.Blocks() as demo:
        gr.Markdown(
            "## Radio Station Database Export\n"
            "Fetch the full list of radio stations from radio-browser.info and export in your chosen format."
        )
        format_selector = gr.Radio(
            choices=["M3U", "Table"],
            value="M3U",
            label="Select output format"
        )
        export_button = gr.Button("Export")
        download_file = gr.File(label="Download .m3u File")
        station_table = gr.Dataframe(
            headers=["Station Name", "Stream URL"],
            label="Station Table"
        )
        status = gr.Textbox(label="Status")
        count_box = gr.Textbox(label="Stations Exported")

        def on_export(selected_format):
            stations, error = fetch_stations()
            if error:
                return None, None, f"❌ Error: {error}", "0"
            count = len(stations)
            if selected_format == "M3U":
                file_path = save_as_m3u(stations)
                return file_path, None, "✔️ Export completed.", str(count)
            else:
                table_data = [[s.get("name", ""), s.get("url_resolved", s.get("url", ""))] for s in stations]
                print(f"Table generated with {count} stations.")
                return None, table_data, "✔️ Table generated.", str(count)

        export_button.click(
            fn=on_export,
            inputs=format_selector,
            outputs=[download_file, station_table, status, count_box]
        )

    return demo

if __name__ == "__main__":
    app = create_app()
    app.launch(share=False)