Skip to content

Instantly share code, notes, and snippets.

@zachwlewis
Created February 13, 2024 05:10
Show Gist options
  • Save zachwlewis/7ad4182e21768dae98cabd17b1185a68 to your computer and use it in GitHub Desktop.
Save zachwlewis/7ad4182e21768dae98cabd17b1185a68 to your computer and use it in GitHub Desktop.
Podcast RSS Downloader

Bulk Podcast Downloader

An interactive python script that downloads all the podcasts for a given RSS feed. It also allows you to preview all the files that will be downloaded.

Requirements

This requires requests.

pip install requests
import requests
import xml.etree.ElementTree as ET
import urllib.request
import os
def download_podcast_episodes_interactive():
# Namespace definition for parsing iTunes-specific tags
ns = {'itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd'}
# Prompt the user for the RSS feed URL
rss_url = input("Enter the RSS feed URL of the podcast: ")
# Fetch the RSS feed content
response = requests.get(rss_url)
if response.status_code == 200:
rss_content = response.content
# Parse the XML content of the RSS feed
root = ET.fromstring(rss_content)
# Attempt to fetch podcast title
channel = root.find('.//channel')
podcast_title = channel.find('title').text if channel is not None else "Unknown Podcast"
episodes = root.findall('.//item')
total_episodes = len(episodes)
print(f"Podcast Name: {podcast_title}")
print(f"Total Episodes: {total_episodes}")
# Ask the user what they wish to do next
action = input("Enter 'yes' to download all episodes, 'preview' to see filenames, or 'no' to cancel: ").lower()
podcast_dir = podcast_title.replace(" ", "_").replace("/", "_") # Simplify the title for the folder name
if not os.path.exists(podcast_dir):
os.makedirs(podcast_dir)
# Preview or download each episode
for item in episodes:
title = item.find('title').text
episode_number = item.find('itunes:episode', ns)
if episode_number is not None:
episode_number = episode_number.text
else:
episode_number = "Unknown"
audio_url = item.find('.//enclosure').attrib['url']
# Format filename to include episode number and title
filename = f"{episode_number} - {title}".replace("/", "-") # Replace disallowed characters
filename = "".join([c for c in filename if c.isalpha() or c.isdigit() or c==' ' or c=='-' or c=='.']).rstrip()
filepath = os.path.join(podcast_dir, filename + '.mp3')
if action == "preview":
print(f"Will download: {filename}")
elif action == "yes":
# Download the audio file if it doesn't already exist
if not os.path.exists(filepath):
print(f"Downloading: {filename}")
urllib.request.urlretrieve(audio_url, filepath)
print(f"Downloaded: {filepath}")
else:
print(f"Already exists: {filepath}")
if action == "no":
print("Download canceled.")
else:
print("Failed to fetch RSS feed.")
# Start the interactive downloading process
download_podcast_episodes_interactive()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment