File size: 3,180 Bytes
11fd592
 
7c806f5
11fd592
7c806f5
 
11fd592
7c806f5
11fd592
7c806f5
 
 
 
 
11fd592
7c806f5
 
1f317db
7c806f5
 
 
1f317db
7c806f5
 
 
 
 
c0153c3
7c806f5
 
1f317db
7c806f5
1f317db
11fd592
7c806f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11fd592
 
7c806f5
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import re
import zipfile
import requests
import tempfile
import subprocess
from bs4 import BeautifulSoup
import gradio as gr

# Installation automatique des dépendances (à lancer en local une seule fois)
try:
    import bs4
except ImportError:
    subprocess.run(["pip", "install", "-q", "gradio", "beautifulsoup4", "requests"])

def sanitize_filename(name):
    return re.sub(r"[^\w\-_.]", "_", name.strip())[:50]

def extract_audio_links_from_html(html_text):
    pattern = r'https://media\.radiofrance-podcast\.net/[^"]*ITEMA[^"]*\.(mp3|m4a)'
    return list(set(re.findall(pattern, html_text)))

def extract_titles_and_links(html_text):
    # Recherche de segments JSON avec les titres + urls audio
    pattern = r'title:\\"(.*?)\\",url:\\"(https://media\.radiofrance-podcast\.net/[^"]*ITEMA[^"]*\.(mp3|m4a))\\"'
    matches = re.findall(pattern, html_text)
    return [(sanitize_filename(title), url) for title, url, _ in matches]

def download_and_zip(url):
    # Téléchargement du HTML
    try:
        response = requests.get(url)
        response.raise_for_status()
    except Exception as e:
        return f"Erreur de téléchargement : {e}", None

    html_text = response.text

    # Extraction des titres et des liens
    titles_links = extract_titles_and_links(html_text)
    if not titles_links:
        # fallback brut si les titres ne sont pas extraits
        urls = extract_audio_links_from_html(html_text)
        titles_links = [(f"track_{i+1:02d}", u) for i, u in enumerate(urls)]

    if not titles_links:
        return "Aucun fichier audio trouvé avec ITEMA dans l'URL", None

    # Création dossier temporaire
    with tempfile.TemporaryDirectory() as tmpdir:
        zip_path = os.path.join(tmpdir, "podcasts.zip")
        with zipfile.ZipFile(zip_path, "w") as zipf:
            for idx, (title, audio_url) in enumerate(titles_links, 1):
                ext = ".mp3" if ".mp3" in audio_url else ".m4a"
                filename = f"{idx:02d}-{title}{ext}"
                filepath = os.path.join(tmpdir, filename)
                try:
                    audio_resp = requests.get(audio_url)
                    audio_resp.raise_for_status()
                    with open(filepath, "wb") as f:
                        f.write(audio_resp.content)
                    zipf.write(filepath, arcname=filename)
                except Exception as e:
                    print(f"Erreur téléchargement {audio_url} : {e}")

        return "Téléchargement terminé avec succès", zip_path

def gradio_interface(url):
    message, zip_file = download_and_zip(url)
    return message, zip_file

# Interface Gradio
demo = gr.Interface(
    fn=gradio_interface,
    inputs=gr.Textbox(label="URL de la page Radio France (Podcast)", placeholder="https://www.radiofrance.fr/franceculture/podcasts/..."),
    outputs=[
        gr.Textbox(label="Message"),
        gr.File(label="Fichier ZIP des épisodes")
    ],
    title="Téléchargement de Podcasts Radio France",
    description="Collez une URL vers un podcast de Radio France pour télécharger tous les épisodes (mp3/m4a) avec les bons noms."
)

if __name__ == "__main__":
    demo.launch()