Export / App.py
Ivan000's picture
Create App.py
44ecdc3 verified
raw
history blame
1.95 kB
import requests
import gradio as gr
import tempfile
import os
API_URL = "https://de1.api.radio-browser.info/json/stations"
# Function to fetch stations and save as .m3u
def fetch_and_save_m3u():
try:
# Fetch full database of stations
response = requests.get(API_URL)
response.raise_for_status()
stations = response.json()
# Create temporary file
tmp_dir = tempfile.mkdtemp()
file_path = os.path.join(tmp_dir, "radio_stations.m3u")
# Write M3U file
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",""))
# Extinf: length -1, display name
f.write(f"#EXTINF:-1,{name}\n")
f.write(f"{url}\n")
return file_path, "✔️ Экспорт завершен."
except Exception as e:
return None, f"❌ Ошибка: {str(e)}"
# Gradio interface
def create_app():
with gr.Blocks() as demo:
gr.Markdown("## Экспорт базы радиостанций в M3U\nНажмите кнопку, чтобы скачать полный список радиостанций из radio-browser.info в формате .m3u.")
download_button = gr.Button("Экспортировать в M3U")
output_file = gr.File(label="Скачать файл .m3u")
status = gr.Textbox(label="Статус")
def on_click():
file_path, msg = fetch_and_save_m3u()
if file_path:
return file_path, msg
else:
return None, msg
download_button.click(fn=on_click, outputs=[output_file, status])
return demo
# Запуск приложения
if __name__ == "__main__":
app = create_app()
app.launch(share=False)