File size: 2,347 Bytes
44ecdc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
756e073
 
44ecdc3
 
 
 
 
 
 
 
 
 
 
 
 
 
756e073
 
44ecdc3
 
756e073
 
44ecdc3
 
 
 
 
 
 
 
756e073
44ecdc3
 
756e073
44ecdc3
756e073
44ecdc3
756e073
44ecdc3
756e073
44ecdc3
 
 
 
 
 
 
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
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()
        count = len(stations)
        print(f"Начинаем экспорт {count} радиостанций...")

        # 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",""))
                f.write(f"#EXTINF:-1,{name}\n")
                f.write(f"{url}\n")

        print(f"Экспорт завершен успешно. Всего записано {count} станций.")
        return file_path, "✔️ Экспорт завершен.", count

    except Exception as e:
        print(f"Ошибка при экспорте: {e}")
        return None, f"❌ Ошибка: {str(e)}", 0

# 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="Статус")
        count_box = gr.Textbox(label="Количество станций экспортировано")

        def on_click():
            file_path, msg, count = fetch_and_save_m3u()
            if file_path:
                return file_path, msg, str(count)
            else:
                return None, msg, "0"

        download_button.click(fn=on_click, outputs=[output_file, status, count_box])

    return demo

# Запуск приложения
if __name__ == "__main__":
    app = create_app()
    app.launch(share=False)