-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodcast_downloader.py
34 lines (25 loc) · 1.16 KB
/
podcast_downloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import spotipy
import requests
import urllib.parse
from spotipy.oauth2 import SpotifyClientCredentials
class SpotifyPodcast:
def __init__(self, client_id, client_secret):
self.sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=client_id,
client_secret=client_secret))
def download_episode(self, podcast_url):
# Parse the podcast ID from the URL
parsed_url = urllib.parse.urlparse(podcast_url)
podcast_id = parsed_url.path.split("/")[-1]
print(f'Podcast ID: {podcast_id}')
episode = self.sp.episode(podcast_id, market="US")
# Save the audio file
audio_url = episode["audio_preview_url"]
response = requests.get(audio_url)
filename = f"{podcast_id}.mp3"
with open(filename, "wb") as f:
f.write(response.content)
print(f"Episode downloaded: {filename}")
return filename
if __name__=="__main__":
sp = SpotifyPodcast("CLIENT_ID", "CLIENT_SECRET")
audio_filename = sp.download_episode("https://open.spotify.com/episode/6oUTyFPLPvJE1jB50hm37l")