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