Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
|
4 |
import os
|
5 |
import zipfile
|
6 |
import tempfile
|
7 |
-
import re
|
8 |
from urllib.parse import urljoin
|
9 |
|
10 |
def process_url(url):
|
11 |
try:
|
12 |
-
headers = {
|
13 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
|
14 |
-
}
|
15 |
response = requests.get(url, headers=headers)
|
16 |
response.raise_for_status()
|
17 |
-
except
|
18 |
-
return None, f"Erreur
|
19 |
-
|
20 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
21 |
|
|
|
22 |
mp3_links = []
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
if '.mp3' in mp3_url:
|
30 |
-
absolute_url = urljoin(response.url, mp3_url.split('?')[0]) # Nettoyer l'URL
|
31 |
-
mp3_links.append(absolute_url)
|
32 |
-
|
33 |
-
# Recherche alternative dans les données JSON
|
34 |
-
script_tags = soup.find_all('script', type='application/ld+json')
|
35 |
-
for script in script_tags:
|
36 |
-
content = script.string
|
37 |
-
if content and '"episode"' in content:
|
38 |
-
matches = re.findall(r'"contentUrl"\s*:\s*"([^"]+\.mp3[^"]*)"', content)
|
39 |
-
for match in matches:
|
40 |
-
absolute_url = urljoin(response.url, match.split('?')[0])
|
41 |
-
mp3_links.append(absolute_url)
|
42 |
|
43 |
-
#
|
44 |
-
|
|
|
45 |
|
46 |
if not mp3_links:
|
47 |
-
return None, "Aucun lien MP3
|
48 |
|
|
|
49 |
temp_dir = tempfile.mkdtemp()
|
50 |
filenames = []
|
51 |
|
52 |
-
for idx, mp3_url in enumerate(mp3_links,
|
53 |
try:
|
54 |
filename = f"{idx:02d}_{os.path.basename(mp3_url).split('?')[0]}"
|
55 |
filepath = os.path.join(temp_dir, filename)
|
@@ -58,16 +45,18 @@ def process_url(url):
|
|
58 |
r.raise_for_status()
|
59 |
with open(filepath, 'wb') as f:
|
60 |
for chunk in r.iter_content(chunk_size=8192):
|
61 |
-
|
|
|
62 |
filenames.append(filepath)
|
63 |
except Exception as e:
|
64 |
-
print(f"
|
65 |
continue
|
66 |
|
67 |
if not filenames:
|
68 |
-
return None, "Échec
|
69 |
|
70 |
-
|
|
|
71 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
72 |
for file in filenames:
|
73 |
zipf.write(file, arcname=os.path.basename(file))
|
@@ -82,13 +71,13 @@ def download_podcast(url):
|
|
82 |
|
83 |
iface = gr.Interface(
|
84 |
fn=download_podcast,
|
85 |
-
inputs=gr.Textbox(label="URL
|
86 |
-
outputs=gr.File(label="
|
87 |
-
title="Téléchargeur Radio France",
|
88 |
examples=[[
|
89 |
"https://www.radiofrance.fr/franceculture/podcasts/serie-le-secret-de-la-licorne-les-aventures-de-tintin"
|
90 |
]],
|
91 |
-
|
|
|
92 |
)
|
93 |
|
94 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
import re
|
4 |
import os
|
5 |
import zipfile
|
6 |
import tempfile
|
|
|
7 |
from urllib.parse import urljoin
|
8 |
|
9 |
def process_url(url):
|
10 |
try:
|
11 |
+
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) 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 |
+
# Recherche approfondie dans les scripts
|
18 |
mp3_links = []
|
19 |
+
pattern = r'(?:contentUrl|url)"\s*:\s*"([^"]+?\.mp3)(?:[^"]*)"' # Capture les URLs .mp3 dans les objets JS
|
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.append(absolute_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 des fichiers
|
36 |
temp_dir = tempfile.mkdtemp()
|
37 |
filenames = []
|
38 |
|
39 |
+
for idx, mp3_url in enumerate(mp3_links, 1):
|
40 |
try:
|
41 |
filename = f"{idx:02d}_{os.path.basename(mp3_url).split('?')[0]}"
|
42 |
filepath = os.path.join(temp_dir, filename)
|
|
|
45 |
r.raise_for_status()
|
46 |
with open(filepath, 'wb') as f:
|
47 |
for chunk in r.iter_content(chunk_size=8192):
|
48 |
+
if chunk:
|
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 de tous les téléchargements"
|
57 |
|
58 |
+
# Création du ZIP
|
59 |
+
zip_path = os.path.join(temp_dir, 'episodes.zip')
|
60 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
61 |
for file in filenames:
|
62 |
zipf.write(file, arcname=os.path.basename(file))
|
|
|
71 |
|
72 |
iface = gr.Interface(
|
73 |
fn=download_podcast,
|
74 |
+
inputs=gr.Textbox(label="URL Radio France", placeholder="Collez l'URL ici..."),
|
75 |
+
outputs=gr.File(label="Épisodes téléchargés"),
|
|
|
76 |
examples=[[
|
77 |
"https://www.radiofrance.fr/franceculture/podcasts/serie-le-secret-de-la-licorne-les-aventures-de-tintin"
|
78 |
]],
|
79 |
+
title="Extracteur MP3 Radio France",
|
80 |
+
description="Téléchargez les épisodes MP3 des podcasts Radio France directement depuis l'URL de la série"
|
81 |
)
|
82 |
|
83 |
iface.launch()
|