File size: 3,213 Bytes
11fd592
 
7c806f5
11fd592
7c806f5
 
 
11fd592
9a10296
7c806f5
 
 
 
11fd592
7c806f5
9a10296
7c806f5
1f317db
9a10296
 
 
 
1f317db
9a10296
 
 
 
 
 
c0153c3
9a10296
 
 
 
 
 
 
1f317db
7c806f5
1f317db
11fd592
9a10296
7c806f5
 
9a10296
7c806f5
9a10296
 
7c806f5
9a10296
7c806f5
9a10296
7c806f5
9a10296
 
 
 
 
7c806f5
 
9a10296
 
7c806f5
9a10296
 
 
7c806f5
 
 
 
9a10296
7c806f5
9a10296
7c806f5
 
9a10296
7c806f5
 
 
 
 
9a10296
7c806f5
 
9a10296
7c806f5
9a10296
 
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
87
88
89
90
91
92
93
import os
import re
import zipfile
import requests
import tempfile
import subprocess
import gradio as gr

# Installation automatique des dépendances si nécessaire
try:
    import bs4
except ImportError:
    subprocess.run(["pip", "install", "-q", "gradio", "beautifulsoup4", "requests"])

def sanitize_filename(name):
    # Rend le nom de fichier compatible avec tous les OS
    return re.sub(r"[^\w\-_.]", "_", name.strip())[:50]

def extract_mp3_links_and_titles(html_text):
    # Expression pour trouver les URL MP3
    mp3_pattern = re.compile(r'https?://[^\s"\'<>]+\.mp3')
    mp3_links = mp3_pattern.findall(html_text)

    # Expression pour tenter d'extraire les titres associés
    item_pattern = re.compile(
        r'title:"\\?"([^"]+)\\?".*?url:"(https?://[^\s"\'<>]+\.mp3)"',
        re.DOTALL
    )
    titled_links = {match[1]: match[0] for match in item_pattern.findall(html_text)}

    results = []
    for link in mp3_links:
        title = titled_links.get(link, None)
        results.append((link, title))
    return results

def download_and_zip_mp3s(url):
    try:
        response = requests.get(url)
        response.raise_for_status()
    except Exception as e:
        return f"Erreur de téléchargement de la page : {e}", None

    html_text = response.text
    mp3_entries = extract_mp3_links_and_titles(html_text)

    if not mp3_entries:
        return "Aucun lien .mp3 trouvé sur cette page.", None

    # Crée un ZIP dans un dossier temporaire
    with tempfile.TemporaryDirectory() as tmpdir:
        zip_path = os.path.join(tmpdir, "episodes_radiofrance.zip")
        with zipfile.ZipFile(zip_path, "w") as zipf:
            for idx, (mp3_url, title) in enumerate(mp3_entries, 1):
                if title:
                    filename = f"{idx:02d}-{sanitize_filename(title)}.mp3"
                else:
                    filename = f"{idx:02d}-episode.mp3"
                filepath = os.path.join(tmpdir, filename)
                try:
                    print(f"Téléchargement : {mp3_url}")
                    audio_resp = requests.get(mp3_url)
                    audio_resp.raise_for_status()
                    if len(audio_resp.content) < 30_000:
                        print(f"Fichier trop petit, ignoré : {mp3_url}")
                        continue
                    with open(filepath, "wb") as f:
                        f.write(audio_resp.content)
                    zipf.write(filepath, arcname=filename)
                except Exception as e:
                    print(f"Erreur lors du téléchargement de {mp3_url} : {e}")

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

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

# Interface Gradio
demo = gr.Interface(
    fn=gradio_interface,
    inputs=gr.Textbox(label="URL de la page contenant des MP3"),
    outputs=[
        gr.Textbox(label="Message"),
        gr.File(label="Fichier ZIP")
    ],
    title="Extracteur MP3 Radio France (ou autre)",
    description="Collez une URL contenant des fichiers MP3, et récupérez-les dans un ZIP avec titres et numérotation."
)

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