Ivan000 commited on
Commit
1ae2ab1
·
verified ·
1 Parent(s): 3cdc146

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -1,12 +1,13 @@
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
  # Fetch full list of stations from the API
9
-
10
  def fetch_stations():
11
  try:
12
  response = requests.get(API_URL)
@@ -20,7 +21,6 @@ def fetch_stations():
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")
@@ -34,8 +34,17 @@ def save_as_m3u(stations):
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(
@@ -43,36 +52,30 @@ def create_app():
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
 
1
+ hare=False)
2
+ import requests
3
  import gradio as gr
4
  import tempfile
5
  import os
6
+ import pandas as pd
7
 
8
  API_URL = "https://de1.api.radio-browser.info/json/stations"
9
 
10
  # Fetch full list of stations from the API
 
11
  def fetch_stations():
12
  try:
13
  response = requests.get(API_URL)
 
21
  return None, str(e)
22
 
23
  # Write stations to M3U file
 
24
  def save_as_m3u(stations):
25
  tmp_dir = tempfile.mkdtemp()
26
  file_path = os.path.join(tmp_dir, "radio_stations.m3u")
 
34
  print(f"M3U file created at {file_path} with {len(stations)} entries.")
35
  return file_path
36
 
37
+ # Write stations to Excel file
38
+ def save_as_excel(stations):
39
+ tmp_dir = tempfile.mkdtemp()
40
+ file_path = os.path.join(tmp_dir, "radio_stations.xlsx")
41
+ data = [{"Station Name": s.get("name", ""), "Stream URL": s.get("url_resolved", s.get("url", ""))} for s in stations]
42
+ df = pd.DataFrame(data)
43
+ df.to_excel(file_path, index=False)
44
+ print(f"Excel file created at {file_path} with {len(stations)} entries.")
45
+ return file_path
46
 
47
+ # Gradio interface
48
  def create_app():
49
  with gr.Blocks() as demo:
50
  gr.Markdown(
 
52
  "Fetch the full list of radio stations from radio-browser.info and export in your chosen format."
53
  )
54
  format_selector = gr.Radio(
55
+ choices=["M3U", "Excel"],
56
  value="M3U",
57
  label="Select output format"
58
  )
59
  export_button = gr.Button("Export")
60
+ download_file = gr.File(label="Download File")
 
 
 
 
61
  status = gr.Textbox(label="Status")
62
  count_box = gr.Textbox(label="Stations Exported")
63
 
64
  def on_export(selected_format):
65
  stations, error = fetch_stations()
66
  if error:
67
+ return None, f"❌ Error: {error}", "0"
68
  count = len(stations)
69
  if selected_format == "M3U":
70
  file_path = save_as_m3u(stations)
 
71
  else:
72
+ file_path = save_as_excel(stations)
73
+ return file_path, "✔️ Export completed.", str(count)
 
74
 
75
  export_button.click(
76
  fn=on_export,
77
  inputs=format_selector,
78
+ outputs=[download_file, status, count_box]
79
  )
80
 
81
  return demo