Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +1,84 @@
|
|
|
|
1 |
import requests
|
|
|
2 |
import re
|
3 |
import os
|
4 |
-
import
|
5 |
-
import
|
6 |
-
import gradio as gr
|
7 |
-
from pathlib import Path
|
8 |
-
from bs4 import BeautifulSoup
|
9 |
|
10 |
-
def
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
15 |
episodes = []
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
or audio_tag.get("title")
|
25 |
-
or source.get("title")
|
26 |
-
or "episode"
|
27 |
-
)
|
28 |
-
url = source["src"]
|
29 |
-
if not url.startswith("http"):
|
30 |
-
url = requests.compat.urljoin(base_url, url)
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
episodes.append((title, url))
|
35 |
|
36 |
return episodes
|
37 |
|
38 |
-
def
|
39 |
-
|
40 |
-
r = requests.get(url, timeout=10)
|
41 |
-
r.raise_for_status()
|
42 |
-
except Exception as e:
|
43 |
-
return f"Erreur lors du chargement de la page : {e}", None
|
44 |
-
|
45 |
-
html_text = r.text
|
46 |
-
episodes = extract_episode_links(html_text, url)
|
47 |
|
48 |
if not episodes:
|
49 |
-
return "Aucun épisode
|
50 |
-
|
51 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
52 |
-
zip_path = os.path.join(temp_dir, "podcast.zip")
|
53 |
-
used_filenames = set()
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
filename = base_name + ".mp3"
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
77 |
|
78 |
-
|
79 |
|
80 |
-
|
81 |
-
fn=
|
82 |
-
inputs=gr.Textbox(label="URL
|
83 |
outputs=[
|
84 |
-
gr.
|
85 |
-
gr.File(label="Fichier ZIP
|
86 |
],
|
87 |
-
title="Téléchargeur de Podcast
|
88 |
-
description="
|
89 |
-
allow_flagging="never"
|
90 |
)
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
import re
|
5 |
import os
|
6 |
+
from urllib.parse import urlparse
|
7 |
+
from zipfile import ZipFile
|
|
|
|
|
|
|
8 |
|
9 |
+
def extract_podcast_episodes(url):
|
10 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
11 |
+
response = requests.get(url, headers=headers)
|
12 |
+
response.raise_for_status()
|
13 |
|
14 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
15 |
+
|
16 |
+
# Titre principal du podcast (pour filtrer les titres)
|
17 |
+
main_title = soup.find("h1").get_text(strip=True).lower()
|
18 |
+
|
19 |
+
# Section contenant les épisodes listés explicitement
|
20 |
episodes = []
|
21 |
+
for article in soup.select("article"):
|
22 |
+
title_tag = article.find("h3")
|
23 |
+
if not title_tag:
|
24 |
+
continue
|
25 |
+
title = title_tag.get_text(strip=True)
|
26 |
+
if not any(word in title.lower() for word in main_title.split()):
|
27 |
+
continue
|
28 |
|
29 |
+
# Cherche lien MP3 directement dans l'article
|
30 |
+
mp3_url = None
|
31 |
+
for a in article.find_all("a", href=True):
|
32 |
+
if a["href"].endswith(".mp3"):
|
33 |
+
mp3_url = a["href"]
|
34 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
if mp3_url:
|
37 |
+
episodes.append({"title": title, "url": mp3_url})
|
|
|
38 |
|
39 |
return episodes
|
40 |
|
41 |
+
def download_and_zip_episodes(url):
|
42 |
+
episodes = extract_podcast_episodes(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
if not episodes:
|
45 |
+
return "Aucun épisode valide trouvé", None
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
os.makedirs("downloads", exist_ok=True)
|
48 |
+
zip_filename = "episodes_radiofrance.zip"
|
49 |
+
zip_path = os.path.join("downloads", zip_filename)
|
|
|
50 |
|
51 |
+
with ZipFile(zip_path, "w") as zipf:
|
52 |
+
for i, episode in enumerate(episodes, start=1):
|
53 |
+
mp3_url = episode["url"]
|
54 |
+
title = episode["title"]
|
55 |
+
ext = os.path.splitext(urlparse(mp3_url).path)[1]
|
56 |
+
filename = f"{i:02d} - {title}{ext}".replace("/", "_")
|
57 |
|
58 |
+
try:
|
59 |
+
mp3_response = requests.get(mp3_url, stream=True)
|
60 |
+
mp3_response.raise_for_status()
|
61 |
+
local_path = os.path.join("downloads", filename)
|
62 |
+
with open(local_path, "wb") as f:
|
63 |
+
for chunk in mp3_response.iter_content(1024):
|
64 |
+
f.write(chunk)
|
65 |
+
zipf.write(local_path, arcname=filename)
|
66 |
+
os.remove(local_path)
|
67 |
+
except Exception as e:
|
68 |
+
print(f"Erreur lors du téléchargement de {mp3_url}: {e}")
|
69 |
|
70 |
+
return f"{len(episodes)} épisode(s) téléchargé(s)", zip_path
|
71 |
|
72 |
+
iface = gr.Interface(
|
73 |
+
fn=download_and_zip_episodes,
|
74 |
+
inputs=gr.Textbox(label="URL de la page podcast de France Culture"),
|
75 |
outputs=[
|
76 |
+
gr.Text(label="Résultat"),
|
77 |
+
gr.File(label="Fichier ZIP")
|
78 |
],
|
79 |
+
title="Téléchargeur de Podcast France Culture",
|
80 |
+
description="Saisissez l’URL d’une série sur France Culture (ex: https://www.radiofrance.fr/franceculture/podcasts/...) pour télécharger uniquement les bons épisodes listés sur la page."
|
|
|
81 |
)
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
+
iface.launch()
|