Update app.py
Browse files
app.py
CHANGED
@@ -8,31 +8,27 @@ from urllib.parse import urljoin
|
|
8 |
|
9 |
def process_url(url):
|
10 |
try:
|
11 |
-
headers = {'User-Agent': 'Mozilla/5.0 (
|
12 |
response = requests.get(url, headers=headers)
|
13 |
response.raise_for_status()
|
14 |
except Exception as e:
|
15 |
return None, f"Erreur de connexion : {str(e)}"
|
16 |
|
17 |
-
#
|
18 |
mp3_links = []
|
19 |
-
pattern = r'(?:contentUrl|url)"\s*:\s*"([^"]+?\.mp3)
|
20 |
matches = re.findall(pattern, response.text)
|
21 |
|
22 |
for match in matches:
|
23 |
-
# Nettoyage de l'URL
|
24 |
clean_url = match.split('";')[0] if '";' in match else match
|
25 |
absolute_url = urljoin(response.url, clean_url)
|
26 |
-
mp3_links
|
27 |
-
|
28 |
-
# Dédoublonnage tout en conservant l'ordre
|
29 |
-
seen = set()
|
30 |
-
mp3_links = [x for x in mp3_links if not (x in seen or seen.add(x))]
|
31 |
|
32 |
if not mp3_links:
|
33 |
return None, "Aucun lien MP3 détecté dans le code source"
|
34 |
|
35 |
-
# Téléchargement
|
36 |
temp_dir = tempfile.mkdtemp()
|
37 |
filenames = []
|
38 |
|
@@ -49,14 +45,13 @@ def process_url(url):
|
|
49 |
f.write(chunk)
|
50 |
filenames.append(filepath)
|
51 |
except Exception as e:
|
52 |
-
print(f"Échec du téléchargement {mp3_url} : {str(e)}")
|
53 |
continue
|
54 |
|
55 |
if not filenames:
|
56 |
-
return None, "Échec
|
57 |
|
58 |
-
# Création
|
59 |
-
zip_path = os.path.join(temp_dir, '
|
60 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
61 |
for file in filenames:
|
62 |
zipf.write(file, arcname=os.path.basename(file))
|
@@ -69,15 +64,33 @@ def download_podcast(url):
|
|
69 |
raise gr.Error(error)
|
70 |
return zip_path
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
|
|
|
|
8 |
|
9 |
def process_url(url):
|
10 |
try:
|
11 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'}
|
12 |
response = requests.get(url, headers=headers)
|
13 |
response.raise_for_status()
|
14 |
except Exception as e:
|
15 |
return None, f"Erreur de connexion : {str(e)}"
|
16 |
|
17 |
+
# Extraction des URLs MP3
|
18 |
mp3_links = []
|
19 |
+
pattern = r'(?:contentUrl|url)"\s*:\s*"([^"]+?\.mp3)'
|
20 |
matches = re.findall(pattern, response.text)
|
21 |
|
22 |
for match in matches:
|
|
|
23 |
clean_url = match.split('";')[0] if '";' in match else match
|
24 |
absolute_url = urljoin(response.url, clean_url)
|
25 |
+
if absolute_url not in mp3_links:
|
26 |
+
mp3_links.append(absolute_url)
|
|
|
|
|
|
|
27 |
|
28 |
if not mp3_links:
|
29 |
return None, "Aucun lien MP3 détecté dans le code source"
|
30 |
|
31 |
+
# Téléchargement
|
32 |
temp_dir = tempfile.mkdtemp()
|
33 |
filenames = []
|
34 |
|
|
|
45 |
f.write(chunk)
|
46 |
filenames.append(filepath)
|
47 |
except Exception as e:
|
|
|
48 |
continue
|
49 |
|
50 |
if not filenames:
|
51 |
+
return None, "Échec des téléchargements"
|
52 |
|
53 |
+
# Création ZIP
|
54 |
+
zip_path = os.path.join(temp_dir, 'podcast.zip')
|
55 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
56 |
for file in filenames:
|
57 |
zipf.write(file, arcname=os.path.basename(file))
|
|
|
64 |
raise gr.Error(error)
|
65 |
return zip_path
|
66 |
|
67 |
+
# Configuration Gradio compatible Hugging Face
|
68 |
+
with gr.Blocks() as app:
|
69 |
+
gr.Markdown("# 📻 Téléchargeur de Podcasts Radio France")
|
70 |
+
|
71 |
+
with gr.Row():
|
72 |
+
url_input = gr.Textbox(
|
73 |
+
label="URL de la série podcast",
|
74 |
+
placeholder="Collez l'URL Radio France ici..."
|
75 |
+
)
|
76 |
+
download_btn = gr.Button("Télécharger les épisodes")
|
77 |
+
|
78 |
+
output_file = gr.File(label="Fichier ZIP à télécharger")
|
79 |
+
error_output = gr.Textbox(visible=False)
|
80 |
+
|
81 |
+
examples = gr.Examples(
|
82 |
+
examples=[[
|
83 |
+
"https://www.radiofrance.fr/franceculture/podcasts/serie-le-secret-de-la-licorne-les-aventures-de-tintin"
|
84 |
+
]],
|
85 |
+
inputs=[url_input]
|
86 |
+
)
|
87 |
+
|
88 |
+
download_btn.click(
|
89 |
+
fn=download_podcast,
|
90 |
+
inputs=url_input,
|
91 |
+
outputs=output_file,
|
92 |
+
api_name="download"
|
93 |
+
)
|
94 |
|
95 |
+
# Configuration spécifique pour Hugging Face
|
96 |
+
app.launch(debug=False, show_error=True)
|