Skip to content

Instantly share code, notes, and snippets.

@lispandfound
Created September 8, 2017 08:20
Show Gist options
  • Save lispandfound/827ce712385a16180f1a8cec80f6599c to your computer and use it in GitHub Desktop.
Save lispandfound/827ce712385a16180f1a8cec80f6599c to your computer and use it in GitHub Desktop.
Download all songs from musicforprogramming.net
from collections import namedtuple
import xml.etree.ElementTree as et
import requests
import sys
from pathlib import Path
Song = namedtuple('Song', ['title', 'filename', 'url', 'size'])
RSS_URL = 'http://musicforprogramming.net/rss.php'
def parse_episode(xml_repr):
''' Return an episode from a given xml item. '''
title = xml_repr.find('title').text
enclosure = xml_repr.find('enclosure')
link = enclosure.get('url')
filename = Path(link.split('/')[-1])
size = int(enclosure.get('length'))
return Song(title, filename, link, size)
def download_episode(local_filename, song):
''' Download episode url. '''
url = song.url
with requests.get(url, stream=True) as r:
with open(local_filename, 'wb') as f:
for index, chunk in enumerate(r.iter_content(chunk_size=1024)):
if chunk:
f.write(chunk)
percentage = (((index + 1) * 1024) / song.size) * 100
print(f'{song.title} -> {local_filename.name}: {percentage:.2f}%', end='\r')
def get_feed():
with requests.get(RSS_URL) as r:
return et.fromstring(r.text)
def main():
save_directory = Path(sys.argv[1])
feed = get_feed()
for item in feed.iter('item'):
song = parse_episode(item)
local_path = save_directory / song.filename
if local_path.exists():
print(f'{song.title} already exists, skipping...')
else:
download_episode(local_path, song)
sys.stdout.write("\033[K")
print(f'Downloaded {song.title}')
sys.stdout.flush()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment