Spaces:
Sleeping
Sleeping
Download app.py from lionelg/Spotify_Podcast_Scraper: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/spaces/lionelg/Spotify_Podcast_Scraper/resolve/main/app.py
- Command line
-
hf download hf://spaces/lionelg/Spotify_Podcast_Scraper/app.py
-
curl -L -o app.py https://huggingface.co/spaces/lionelg/Spotify_Podcast_Scraper/resolve/main/app.py
3.52 kB
| import gradio as gr | |
| import requests | |
| import base64 | |
| import pandas as pd | |
| from io import StringIO | |
| def get_spotify_token(client_id, client_secret): | |
| auth_str = f"{client_id}:{client_secret}" | |
| b64_auth_str = base64.b64encode(auth_str.encode("utf-8")).decode("utf-8") | |
| headers = {"Authorization": f"Basic {b64_auth_str}"} | |
| data = {"grant_type": "client_credentials"} | |
| response = requests.post("https://accounts.spotify.com/api/token", headers=headers, data=data) | |
| response.raise_for_status() | |
| return response.json()["access_token"] | |
| def get_all_episodes(show_id, token): | |
| headers = {"Authorization": f"Bearer {token}"} | |
| url = f"https://api.spotify.com/v1/shows/{show_id}/episodes" | |
| params = {"limit": 50, "offset": 0} | |
| episodes = [] | |
| while True: | |
| response = requests.get(url, headers=headers, params=params) | |
| response.raise_for_status() | |
| data = response.json() | |
| if not data.get("items"): | |
| raise ValueError("Aucun épisode récupéré — Show ID invalide ou token expiré.") | |
| for ep in data.get("items", []): | |
| if ep is None: | |
| continue | |
| episodes.append({ | |
| "id": ep.get("id", ""), | |
| "name": ep.get("name", ""), | |
| "release_date": ep.get("release_date", ""), | |
| "duration_ms": ep.get("duration_ms", 0), | |
| "language": ep.get("language", ""), | |
| "spotify_url": ep.get("external_urls", {}).get("spotify", ""), | |
| "preview_url": ep.get("audio_preview_url", ""), | |
| "description": ep.get("description", "") | |
| }) | |
| if not data.get("next"): | |
| break | |
| params["offset"] += params["limit"] | |
| df = pd.DataFrame(episodes) | |
| df["duration_min"] = (df["duration_ms"] / 60000).round(2) | |
| return df.drop(columns=["duration_ms"]) | |
| def scrape_podcast(client_id, client_secret, show_id): | |
| try: | |
| token = get_spotify_token(client_id, client_secret) | |
| df = get_all_episodes(show_id, token) | |
| csv_buffer = StringIO() | |
| df.to_csv(csv_buffer, index=False, encoding="utf-8-sig") | |
| return f"✅ Succès : {len(df)} épisodes récupérés", csv_buffer.getvalue() | |
| except Exception as e: | |
| import traceback | |
| print(traceback.format_exc()) # log dans les logs Hugging Face | |
| return f"❌ Erreur : {str(e)}", "" | |
| with gr.Blocks() as demo: | |
| gr.Image(value="logo.png", label=None, show_label=False, width=200) | |
| gr.Markdown("# 🎧 Spotify Podcast Scraper") | |
| gr.Markdown("Entrez vos identifiants Spotify Developer et l'ID du podcast.") | |
| with gr.Row(): | |
| client_id = gr.Textbox(label="Client ID", type="password") | |
| client_secret = gr.Textbox(label="Client Secret", type="password") | |
| show_id = gr.Textbox(label="Show ID du Podcast", placeholder="ex: 6jQV8khPNxJy3cBiG8v0hX") | |
| scrape_btn = gr.Button("Scraper les épisodes") | |
| status = gr.Textbox(label="Statut") | |
| csv_file = gr.File(label="📥 Télécharger les épisodes en CSV", file_types=[".csv"]) | |
| def run_scraper(cid, csec, sid): | |
| message, csv_data = scrape_podcast(cid, csec, sid) | |
| if csv_data: | |
| path = "episodes.csv" | |
| with open(path, "w", encoding="utf-8-sig") as f: | |
| f.write(csv_data) | |
| return message, path | |
| else: | |
| return message, "" | |
| scrape_btn.click(fn=run_scraper, inputs=[client_id, client_secret, show_id], outputs=[status, csv_file]) | |
| demo.launch() | |