Skip to content
TheChatArchiveThe Chat Archive
GLMGLM-5 / Pony Alpha· unknownHugging Face Frontier Reasoning Traces

Write a Python script to download and organize podcasts.

  1. User

    Write a Python script to download and organize podcasts.

  2. GLM Assistant

    The user wants a Python script to download and organize podcasts.

    1. Understand the Requirements:

    • Functionality:
      • Download podcast episodes (from RSS feeds).
      • Organize the files (renaming, folder structure).
    • Language: Python.
    • Key Libraries: feedparser (for reading RSS), requests (for downloading), os/pathlib (for file management), mutagen (optional but highly recommended for metadata/ID3 tags).

    2. Core Features needed in the script:

    • Input: A list of RSS feed URLs.
    • Parsing: Extract episode title, published date, audio URL, and show title from the feed.
    • Directory Structure: Create a folder for each podcast, maybe subfolders by year or just keep it flat with good filenames.
    • Downloading: Stream the audio file to disk to handle large files without crashing memory.
    • Naming Convention: PodcastName - Date - EpisodeTitle.mp3.
    • ID3 Tags (Metadata): This is crucial for "organizing" so that music players recognize the files correctly.

    3. Step-by-Step Implementation Plan:

    • Step 0: Imports & Setup. Need feedparser, requests, os, re (for cleaning filenames).
    • Step 1: Define a class or function for the Downloader. Let's use a class structure for better state management (e.g., download directory).
    • Step 2: Helper function for cleaning filenames. OSs don't like characters like :, /, ? in filenames.
    • Step 3: Parsing the Feed. Use feedparser.parse. Extract specific fields (entries, title, links).
    • Step 4: Downloading Logic. Use requests.get(stream=True) to download chunks. Check if file already exists (to skip re-downloading).
    • Step 5: Organization Logic. Construct the file path: BaseDir/PodcastName/Filename.mp3.
    • Step 6: (Optional but good) Metadata. Adding ID3 tags makes the script much more useful. I'll use mutagen or eyed3. mutagen is very common. Let's keep it simple first, maybe add metadata as an advanced option or comment it out if it complicates the dependencies too much, but actually, good organization requires metadata. I will include basic metadata tagging using mutagen because standard music players sort by ID3 tags, not just filenames.

    4. Drafting the Code (Mental or Scratchpad):

    • Imports: feedparser, requests, os, re, datetime.
    • Function download_podcast(feed_url, base_dir):
      • Parse feed.
      • Loop through entries.
      • Get audio link (check type starts with audio).
      • Sanitize title.
      • Create path.
      • Download.

    5. Refining the Logic:

    • Sanitization: re.sub(r'[\\/*?:"<>|]', "", string)
    • Audio Link: In RSS feeds, the audio is usually in entry.enclosures. Need to check href.
    • Progress Bar: A simple print statement is enough for a basic script.

    6. Structuring the Output Script: I will provide a complete, runnable script. I should also explain how to install dependencies (pip install feedparser requests mutagen).

    7. Writing the Code (Iterative refinement):

    • Class PodcastDownloader:

      • __init__(self, save_dir)
      • add_feed(url)
      • download_new_episodes()
    • Handling "Already Downloaded": Check if file exists. This is vital.

    • Metadata: I'll include a try-except block for metadata writing. If mutagen isn't installed, it won't crash the download, it just skips tagging. Or just make mutagen a requirement. It's better to include it for a "proper" solution.

    8. Final Code Structure:

    import os
    import re
    import feedparser
    import requests
    from mutagen.easyid3 import EasyID3
    from datetime import datetime
    
    # ... sanitize filename function ...
    # ... download function ...
    # ... main execution block ...
    

    9. Adding Explanations:

    • Prerequisites (libraries).
    • How to use (edit the podcast_feeds list).
    • Customization options.

    10. Self-Correction/Review during drafting:

    • Issue: Some feeds use .m4a or .ogg. The extension should be extracted from the URL or the mime type.
    • Fix: Parse the enclosure URL to get the extension (e.g., .mp3).
    • Issue: Large files.
    • Fix: Use response.iter_content.

    11. Final Polish: Ensure the code is readable, commented, and robust (error handling for network issues).

    (Self-Correction on Metadata): mutagen can be a bit tricky for beginners to install sometimes (compiling C extensions), but EasyID3 is standard. I will include it but make the script resilient if it fails.

    Let's assemble the final response. I will provide:

    1. Prerequisites command.
    2. The Python script.
    3. Explanation of how it works.

    (Self-Correction on filename cleanup): Need to remove newlines too. (Self-Correction on entry date): published_parsed is a time tuple. Need to convert to datetime.

    12. Final Output Generation (Proceeding to generate the response based on these steps). To create a robust podcast downloader, you need a script that parses RSS feeds, handles file naming (to avoid illegal characters), manages folders, and downloads files efficiently.

    Here is a complete, ready-to-run Python script.

    Prerequisites

    You will need to install two libraries: feedparser (to read the RSS feeds) and requests (to download the files). mutagen is optional but recommended if you want to ensure the metadata (Artist, Album, Title) is correct in your music player.

    Open your terminal and run:

    pip install feedparser requests mutagen
    

    The Python Script

    Save the following code as podcast_downloader.py.

    import os
    import re
    import time
    import requests
    import feedparser
    from mutagen.easyid3 import EasyID3
    from mutagen.id3 import ID3, APIC, error
    from datetime import datetime
    
    # --- CONFIGURATION ---
    # Add the RSS feeds of the podcasts you want to download here
    PODCAST_FEEDS = [
        "https://feeds.npr.org/510289/podcast.xml",  # Example: Planet Money
        # "https://feeds.simplecast.com/qm_9xx0g",   # Example: Morbid
    ]
    
    # Where you want to save the podcasts
    # On Windows, you might use r"C:\Music\Podcasts"
    # On Mac/Linux, use "/Users/yourname/Music/Podcasts"
    BASE_DOWNLOAD_DIR = "Podcasts"
    
    # How many recent episodes to download per feed
    # Set to None to download everything (be careful with old feeds!)
    LIMIT_EPISODES = 3 
    # ---------------------
    
    def sanitize_filename(name):
        """
        Removes characters that are invalid in file names 
        (Windows/Linux compatible).
        """
        return re.sub(r'[\\/*?:"<>|]', "", name).strip()
    
    def get_file_extension(url):
        """
        Extracts the file extension from a URL.
        Defaults to .mp3 if not found.
        """
        ext = os.path.splitext(url)[1]
        # Handle cases like .mp3?source=feed or query parameters
        if ext and len(ext) <= 5:
            return ext
        return ".mp3"
    
    def download_file(url, folder_path, filename):
        """
        Downloads a file with a progress bar display.
        """
        full_path = os.path.join(folder_path, filename)
        
        # Skip if already exists
        if os.path.exists(full_path):
            print(f"  - [Skipping] {filename} already exists.")
            return full_path
    
        print(f"  - [Downloading] {filename}")
        
        try:
            response = requests.get(url, stream=True)
            response.raise_for_status()
            
            # Write in chunks to handle large files
            with open(full_path, 'wb') as f:
                for chunk in response.iter_content(chunk_size=8192):
                    f.write(chunk)
            
            return full_path
        except Exception as e:
            print(f"  ! Error downloading {filename}: {e}")
            return None
    
    def add_metadata(file_path, title, artist, album, cover_url):
        """
        Adds ID3 tags (Title, Artist, Album) and Album Art to the MP3.
        """
        try:
            audio = EasyID3(file_path)
        except error:
            # If file has no tags, create new ID3 object
            audio = EasyID3()
        
        audio['title'] = title
        audio['artist'] = artist
        audio['album'] = album
        audio.save(file_path)
        
        # Handle Album Art (Optional)
        if cover_url:
            try:
                audio_tags = ID3(file_path)
                img_response = requests.get(cover_url, stream=True)
                if img_response.status_code == 200:
                    audio_tags.add(APIC(
                        encoding=3,
                        mime='image/jpeg',
                        type=3, # 3 is for cover image
                        desc=u'Cover',
                        data=img_response.content
                    ))
                    audio_tags.save(file_path)
            except Exception:
                pass # Silently fail if cover art doesn't work
    
    def process_feed(feed_url):
        print(f"\nProcessing feed: {feed_url}")
        
        # Parse the RSS feed
        feed = feedparser.parse(feed_url)
        
        if feed.bozo: # Check for feed parsing errors
            print(f"  ! Warning: Could not parse feed correctly. {feed.bozo_exception}")
        
        podcast_title = feed.feed.get('title', 'Unknown Podcast')
        clean_podcast_title = sanitize_filename(podcast_title)
        podcast_dir = os.path.join(BASE_DOWNLOAD_DIR, clean_podcast_title)
        
        # Create directory
        os.makedirs(podcast_dir, exist_ok=True)
        
        print(f"Podcast: {podcast_title}")
        
        # Get entries
        entries = feed.entries
        if LIMIT_EPISODES:
            entries = entries[:LIMIT_EPISODES]
            
        for entry in entries:
            # Extract data
            episode_title = entry.get('title', 'Untitled')
            
            # Find the audio link
            audio_url = None
            if 'links' in entry:
                for link in entry.links:
                    if 'audio' in link.type or 'video' in link.type:
                        audio_url = link.href
                        break
            
            if not audio_url:
                continue
    
            # Prepare filename
            # Format: 2023-01-01 - Episode Title.mp3
            pub_date = entry.get('published_parsed', time.gmtime())
            date_str = time.strftime("%Y-%m-%d", pub_date)
            ext = get_file_extension(audio_url)
            
            filename = f"{date_str} - {sanitize_filename(episode_title)}{ext}"
            
            # Download
            file_path = download_file(audio_url, podcast_dir, filename)
            
            # Tagging (if download successful and it's an mp3)
            if file_path and ext == '.mp3':
                cover_url = None
                if 'image' in entry:
                    cover_url = entry.image.href
                elif 'feed' in feed and 'image' in feed.feed:
                    cover_url = feed.feed.image.href
                    
                add_metadata(file_path, episode_title, podcast_title, podcast_title, cover_url)
    
    def main():
        print("--- Podcast Downloader Started ---")
        if not os.path.exists(BASE_DOWNLOAD_DIR):
            os.makedirs(BASE_DOWNLOAD_DIR)
            
        for url in PODCAST_FEEDS:
            try:
                process_feed(url)
            except Exception as e:
                print(f"Critical error processing {url}: {e}")
    
        print("\n--- All Done! ---")
    
    if __name__ == "__main__":
        main()
    

    How it works

    1. Setup: You paste your RSS feed URLs into the PODCAST_FEEDS list.
    2. Directory Creation: It creates a main folder named "Podcasts" and a subfolder for each specific show (e.g., Podcasts/Planet Money/).
    3. Parsing: It uses feedparser to look at the RSS XML. It looks for the title, publication date, and the specific link that points to an audio file (like an MP3 or M4A).
    4. Sanitization: It strips illegal characters (like ? or :) from the episode titles so your operating system doesn't crash when saving the file.
    5. Downloading: It checks if the file already exists. If not, it downloads it in chunks (which is memory efficient).
    6. Organization & Tagging:
      • It names files by date (e.g., 2023-10-27 - My Episode.mp3) so they sort chronologically in your file explorer.
      • It injects ID3 tags (Artist, Album, Title) using mutagen. This is crucial if you plan to put these files on a generic MP3 player or into iTunes/Music app, ensuring they show up under the correct artist name.

    Customization

    • Download Everything: Change LIMIT_EPISODES = 3 to LIMIT_EPISODES = None.
    • Change Location: Edit BASE_DOWNLOAD_DIR = "Podcasts" to your desired path.
    • File Naming: In the process_feed function, look for the line filename = f"{date_str} - ... to change how files are named (e.g., if you prefer just the title).

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.