Create App.py
Browse files
App.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
API_URL = "https://de1.api.radio-browser.info/json/stations"
|
| 7 |
+
|
| 8 |
+
# Function to fetch stations and save as .m3u
|
| 9 |
+
|
| 10 |
+
def fetch_and_save_m3u():
|
| 11 |
+
try:
|
| 12 |
+
# Fetch full database of stations
|
| 13 |
+
response = requests.get(API_URL)
|
| 14 |
+
response.raise_for_status()
|
| 15 |
+
stations = response.json()
|
| 16 |
+
|
| 17 |
+
# Create temporary file
|
| 18 |
+
tmp_dir = tempfile.mkdtemp()
|
| 19 |
+
file_path = os.path.join(tmp_dir, "radio_stations.m3u")
|
| 20 |
+
|
| 21 |
+
# Write M3U file
|
| 22 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 23 |
+
f.write("#EXTM3U\n")
|
| 24 |
+
for station in stations:
|
| 25 |
+
name = station.get("name", "Unknown")
|
| 26 |
+
url = station.get("url_resolved", station.get("url",""))
|
| 27 |
+
# Extinf: length -1, display name
|
| 28 |
+
f.write(f"#EXTINF:-1,{name}\n")
|
| 29 |
+
f.write(f"{url}\n")
|
| 30 |
+
|
| 31 |
+
return file_path, "✔️ Экспорт завершен."
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return None, f"❌ Ошибка: {str(e)}"
|
| 35 |
+
|
| 36 |
+
# Gradio interface
|
| 37 |
+
def create_app():
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
gr.Markdown("## Экспорт базы радиостанций в M3U\nНажмите кнопку, чтобы скачать полный список радиостанций из radio-browser.info в формате .m3u.")
|
| 40 |
+
download_button = gr.Button("Экспортировать в M3U")
|
| 41 |
+
output_file = gr.File(label="Скачать файл .m3u")
|
| 42 |
+
status = gr.Textbox(label="Статус")
|
| 43 |
+
|
| 44 |
+
def on_click():
|
| 45 |
+
file_path, msg = fetch_and_save_m3u()
|
| 46 |
+
if file_path:
|
| 47 |
+
return file_path, msg
|
| 48 |
+
else:
|
| 49 |
+
return None, msg
|
| 50 |
+
|
| 51 |
+
download_button.click(fn=on_click, outputs=[output_file, status])
|
| 52 |
+
|
| 53 |
+
return demo
|
| 54 |
+
|
| 55 |
+
# Запуск приложения
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
app = create_app()
|
| 58 |
+
app.launch(share=False)
|