Advertisement
commandlinekid

Podcast: Python script to get all podcasts (.mp3) for a given RSS file

Dec 18th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. import os
  2. import requests
  3. import xml.etree.ElementTree as ET
  4. import subprocess
  5.  
  6. # Define the URL of the RSS feed
  7. rss_url = "https://feeds.buzzsprout.com/258846.rss"
  8.  
  9. # Define headers with a custom user-agent to avoid blocking
  10. headers = {
  11. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
  12. }
  13.  
  14. # Fetch the RSS feed with custom headers to avoid blocking
  15. try:
  16. response = requests.get(rss_url, headers=headers, timeout=10)
  17.  
  18. # Check if the request was successful
  19. if response.status_code != 200:
  20. print(f"Failed to fetch the RSS feed. HTTP Status Code: {response.status_code}")
  21. exit(1)
  22.  
  23. except requests.exceptions.RequestException as e:
  24. print(f"An error occurred while fetching the RSS feed: {e}")
  25. exit(1)
  26.  
  27. # Parse the XML content of the RSS feed
  28. root = ET.fromstring(response.content)
  29.  
  30. # Define the folder to save podcasts
  31. download_folder = "podcasts"
  32. if not os.path.exists(download_folder):
  33. os.makedirs(download_folder)
  34.  
  35. # Loop through each item in the RSS feed and extract the audio URL
  36. for item in root.findall(".//item"):
  37. # Look for the <enclosure> tag and extract the 'url' attribute
  38. enclosure = item.find("enclosure")
  39. if enclosure is not None:
  40. audio_url = enclosure.get("url")
  41. if audio_url and audio_url.endswith(".mp3"): # Check if it's an mp3 file
  42. # Get the podcast file name from the URL
  43. file_name = os.path.join(download_folder, audio_url.split("/")[-1])
  44.  
  45. # Download the file using wget
  46. print(f"Downloading {file_name}...")
  47. subprocess.run(["wget", audio_url, "-O", file_name])
  48.  
  49. print("Download complete.")
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement