Last active
December 30, 2024 03:41
-
-
Save robertnicjoo/76e07f98ddb068305b06828d32267cef to your computer and use it in GitHub Desktop.
This python file will download all `MP3` files listed in `XML` (RSS feed) file. you can customize file extensions and XML structure based on your XML files. File names will based on title tag in XML file and spaces will be replaced with underscore.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
import xml.etree.ElementTree as ET | |
# Load and parse the XML file | |
xml_file = 'feed.xml' | |
tree = ET.parse(xml_file) | |
root = tree.getroot() | |
# Define the namespace (if any) | |
namespace = {'itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd'} | |
# Create a directory to store downloaded files | |
download_dir = 'downloads' | |
os.makedirs(download_dir, exist_ok=True) | |
# Extract items and download MP3s | |
for idx, item in enumerate(root.findall('channel/item'), start=1): | |
title = item.find('title').text.strip() | |
enclosure = item.find('enclosure') | |
if enclosure is not None and enclosure.get('url'): | |
mp3_url = enclosure.get('url') | |
filename = f"{idx}-{title}.mp3".replace(" ", "_").replace("/", "_") | |
filepath = os.path.join(download_dir, filename) | |
# Download the MP3 file | |
print(f"Downloading {filename}...") | |
response = requests.get(mp3_url) | |
if response.status_code == 200: | |
with open(filepath, 'wb') as file: | |
file.write(response.content) | |
print(f"Saved: {filepath}") | |
else: | |
print(f"Failed to download {filename}. HTTP Status Code: {response.status_code}") | |
else: | |
print(f"No enclosure found for item {idx}: {title}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment