Created
November 25, 2016 08:04
-
-
Save mikeckennedy/7245e9b839ca968143d8bcffa923f43a to your computer and use it in GitHub Desktop.
Python script to download MP3s from Python Bytes
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
# requires request and Python 3 | |
import requests | |
import sys | |
import os | |
from xml.etree import ElementTree | |
def main(): | |
rss_url = 'https://pythonbytes.fm/episodes/rss' | |
resp = requests.get(rss_url) | |
if resp.status_code != 200: | |
print("ERROR: {}".format(resp.status_code)) | |
return | |
xml_text = resp.text | |
xml = ElementTree.fromstring(xml_text) | |
items = xml.findall('channel/item') | |
print("Downloading {} episodes...".format(len(items))) | |
for item in items: | |
url = item.find('enclosure').attrib.get('url') | |
title = item.find('title').text | |
print(title + " ... ", end='') | |
sys.stdout.flush() | |
# print(url) | |
name = url.split('/')[-1] | |
if os.path.exists(name): | |
print(" already downloaded, skipping.") | |
continue | |
resp = requests.get(url, stream=True) | |
with open(name, 'wb') as fout: | |
for chunk in resp.iter_content(1024*32): | |
fout.write(chunk) | |
print("done.") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment