File size: 2,848 Bytes
ee930cb 44ecdc3 1ae2ab1 44ecdc3 3cdc146 44ecdc3 756e073 3cdc146 44ecdc3 3cdc146 44ecdc3 1ae2ab1 3cdc146 1ae2ab1 44ecdc3 3cdc146 1ae2ab1 3cdc146 1ae2ab1 3cdc146 1ae2ab1 3cdc146 44ecdc3 1ae2ab1 44ecdc3 3cdc146 1ae2ab1 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 84 85 |
import requests
import gradio as gr
import tempfile
import os
import pandas as pd
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
# Write stations to Excel file
def save_as_excel(stations):
tmp_dir = tempfile.mkdtemp()
file_path = os.path.join(tmp_dir, "radio_stations.xlsx")
data = [{"Station Name": s.get("name", ""), "Stream URL": s.get("url_resolved", s.get("url", ""))} for s in stations]
df = pd.DataFrame(data)
df.to_excel(file_path, index=False)
print(f"Excel 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", "Excel"],
value="M3U",
label="Select output format"
)
export_button = gr.Button("Export")
download_file = gr.File(label="Download File")
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, f"❌ Error: {error}", "0"
count = len(stations)
if selected_format == "M3U":
file_path = save_as_m3u(stations)
else:
file_path = save_as_excel(stations)
return file_path, "✔️ Export completed.", str(count)
export_button.click(
fn=on_export,
inputs=format_selector,
outputs=[download_file, status, count_box]
)
return demo
if __name__ == "__main__":
app = create_app()
app.launch(share=False)
|