File size: 3,010 Bytes
11fd592
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re
import sys
import subprocess
import importlib.util
import shutil
import tempfile

# === INSTALLATION AUTOMATIQUE DES DÉPENDANCES ===
def install_if_missing(package_name, import_name=None):
    import_name = import_name or package_name
    if importlib.util.find_spec(import_name) is None:
        print(f"Installation de {package_name}...")
        subprocess.check_call([sys.executable, "-m", "pip", "install", package_name])

for package in [
    ("requests",),
    ("bs4", "bs4"),
    ("gradio",),
]:
    install_if_missing(*package)

# === IMPORTS ===
import requests
import gradio as gr
from bs4 import BeautifulSoup

# === UTILITAIRES ===
def slugify(text, max_length=50):
    text = text.lower()
    text = re.sub(r'[^\w\s-]', '', text)
    text = re.sub(r'[-\s]+', '_', text)
    return text[:max_length].strip('_')

def extract_mp3_links_and_title(url):
    response = requests.get(url)
    response.raise_for_status()
    soup = BeautifulSoup(response.text, 'html.parser')

    # Titre du podcast
    title_tag = soup.find('h1') or soup.find('title')
    podcast_title = slugify(title_tag.get_text()) if title_tag else "podcast"

    # Liens MP3
    mp3_links = [a['href'] for a in soup.find_all('a', href=True) if a['href'].endswith('.mp3')]

    return podcast_title, mp3_links

def download_and_zip_podcast(url):
    try:
        podcast_title, mp3_links = extract_mp3_links_and_title(url)
        if not mp3_links:
            return "Aucun fichier MP3 trouvé.", None

        temp_dir = tempfile.mkdtemp()
        for i, mp3_url in enumerate(mp3_links, start=1):
            filename = f"{podcast_title}_{i:02}.mp3"
            filepath = os.path.join(temp_dir, filename)
            with requests.get(mp3_url, stream=True) as r:
                r.raise_for_status()
                with open(filepath, 'wb') as f:
                    for chunk in r.iter_content(chunk_size=8192):
                        f.write(chunk)

        zip_path = os.path.join(temp_dir, f"{podcast_title}.zip")
        shutil.make_archive(zip_path.replace('.zip', ''), 'zip', temp_dir)

        return f"{len(mp3_links)} fichiers téléchargés avec succès.", zip_path

    except Exception as e:
        return f"Erreur : {str(e)}", None

# === INTERFACE GRADIO ===
with gr.Blocks() as app:
    gr.Markdown("# Téléchargeur de Podcasts MP3")
    with gr.Row():
        url_input = gr.Textbox(label="URL de la page série", placeholder="https://www.radiofrance.fr/...")
    download_button = gr.Button("Télécharger et compresser")
    output_text = gr.Textbox(label="Message")
    file_output = gr.File(label="Fichier ZIP", file_types=[".zip"])

    def process(url):
        message, zip_file = download_and_zip_podcast(url)
        return message, zip_file

    download_button.click(fn=process, inputs=[url_input], outputs=[output_text, file_output])

# === LANCEMENT LOCAL OU SUR HUGGINGFACE ===
if __name__ == "__main__":
    app.launch(share=True)  # `share=True` utile pour Hugging Face