Update app.py
Browse files
app.py
CHANGED
|
@@ -5,58 +5,79 @@ import os
|
|
| 5 |
|
| 6 |
API_URL = "https://de1.api.radio-browser.info/json/stations"
|
| 7 |
|
| 8 |
-
#
|
| 9 |
|
| 10 |
-
def
|
| 11 |
try:
|
| 12 |
-
# Fetch full database of stations
|
| 13 |
response = requests.get(API_URL)
|
| 14 |
response.raise_for_status()
|
| 15 |
stations = response.json()
|
| 16 |
count = len(stations)
|
| 17 |
-
print(f"
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Write M3U file
|
| 24 |
-
with open(file_path, "w", encoding="utf-8") as f:
|
| 25 |
-
f.write("#EXTM3U\n")
|
| 26 |
-
for station in stations:
|
| 27 |
-
name = station.get("name", "Unknown")
|
| 28 |
-
url = station.get("url_resolved", station.get("url",""))
|
| 29 |
-
f.write(f"#EXTINF:-1,{name}\n")
|
| 30 |
-
f.write(f"{url}\n")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
return file_path, "✔️ Экспорт завершен.", count
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Gradio interface
|
|
|
|
| 40 |
def create_app():
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
-
gr.Markdown(
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
return demo
|
| 58 |
|
| 59 |
-
# Запуск приложения
|
| 60 |
if __name__ == "__main__":
|
| 61 |
app = create_app()
|
| 62 |
app.launch(share=False)
|
|
|
|
|
|
| 5 |
|
| 6 |
API_URL = "https://de1.api.radio-browser.info/json/stations"
|
| 7 |
|
| 8 |
+
# Fetch full list of stations from the API
|
| 9 |
|
| 10 |
+
def fetch_stations():
|
| 11 |
try:
|
|
|
|
| 12 |
response = requests.get(API_URL)
|
| 13 |
response.raise_for_status()
|
| 14 |
stations = response.json()
|
| 15 |
count = len(stations)
|
| 16 |
+
print(f"Starting fetch of {count} stations...")
|
| 17 |
+
return stations, None
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"Error fetching stations: {e}")
|
| 20 |
+
return None, str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Write stations to M3U file
|
|
|
|
| 23 |
|
| 24 |
+
def save_as_m3u(stations):
|
| 25 |
+
tmp_dir = tempfile.mkdtemp()
|
| 26 |
+
file_path = os.path.join(tmp_dir, "radio_stations.m3u")
|
| 27 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 28 |
+
f.write("#EXTM3U\n")
|
| 29 |
+
for station in stations:
|
| 30 |
+
name = station.get("name", "Unknown")
|
| 31 |
+
url = station.get("url_resolved", station.get("url", ""))
|
| 32 |
+
f.write(f"#EXTINF:-1,{name}\n")
|
| 33 |
+
f.write(f"{url}\n")
|
| 34 |
+
print(f"M3U file created at {file_path} with {len(stations)} entries.")
|
| 35 |
+
return file_path
|
| 36 |
|
| 37 |
# Gradio interface
|
| 38 |
+
|
| 39 |
def create_app():
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown(
|
| 42 |
+
"## Radio Station Database Export\n"
|
| 43 |
+
"Fetch the full list of radio stations from radio-browser.info and export in your chosen format."
|
| 44 |
+
)
|
| 45 |
+
format_selector = gr.Radio(
|
| 46 |
+
choices=["M3U", "Table"],
|
| 47 |
+
value="M3U",
|
| 48 |
+
label="Select output format"
|
| 49 |
+
)
|
| 50 |
+
export_button = gr.Button("Export")
|
| 51 |
+
download_file = gr.File(label="Download .m3u File")
|
| 52 |
+
station_table = gr.Dataframe(
|
| 53 |
+
headers=["Station Name", "Stream URL"],
|
| 54 |
+
label="Station Table"
|
| 55 |
+
)
|
| 56 |
+
status = gr.Textbox(label="Status")
|
| 57 |
+
count_box = gr.Textbox(label="Stations Exported")
|
| 58 |
+
|
| 59 |
+
def on_export(selected_format):
|
| 60 |
+
stations, error = fetch_stations()
|
| 61 |
+
if error:
|
| 62 |
+
return None, None, f"❌ Error: {error}", "0"
|
| 63 |
+
count = len(stations)
|
| 64 |
+
if selected_format == "M3U":
|
| 65 |
+
file_path = save_as_m3u(stations)
|
| 66 |
+
return file_path, None, "✔️ Export completed.", str(count)
|
| 67 |
else:
|
| 68 |
+
table_data = [[s.get("name", ""), s.get("url_resolved", s.get("url", ""))] for s in stations]
|
| 69 |
+
print(f"Table generated with {count} stations.")
|
| 70 |
+
return None, table_data, "✔️ Table generated.", str(count)
|
| 71 |
|
| 72 |
+
export_button.click(
|
| 73 |
+
fn=on_export,
|
| 74 |
+
inputs=format_selector,
|
| 75 |
+
outputs=[download_file, station_table, status, count_box]
|
| 76 |
+
)
|
| 77 |
|
| 78 |
return demo
|
| 79 |
|
|
|
|
| 80 |
if __name__ == "__main__":
|
| 81 |
app = create_app()
|
| 82 |
app.launch(share=False)
|
| 83 |
+
|