Last active
August 3, 2016 06:04
-
-
Save mdauphin/c7b6a423355aec1b6b6b08421dfd3ba2 to your computer and use it in GitHub Desktop.
Python download RSS podcast
This file contains hidden or 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 feedparser | |
import sys | |
import urllib2 | |
import os.path | |
''' | |
Download all mp3 from rss podcast in current directory | |
program usage: | |
python download.py http://url/podcast.rss | |
''' | |
def download(url, filename): | |
if os.path.isfile(filename): | |
print("file %s already exist" % filename) | |
return | |
u = urllib2.urlopen(url) | |
meta = u.info() | |
filesize = int(meta.getheaders("Content-Length")[0]) | |
print "Downloading: %s Bytes: %s" % (filename, filesize) | |
dl = 0 | |
with open(filename, 'wb') as f: | |
while True: | |
buff = u.read(8192) | |
if not buff: | |
break | |
dl += len(buff) | |
f.write(buff) | |
status = r"\r%10d [%3.2f%%]" % (dl, dl * 100. / filesize) | |
status = status + chr(8)*(len(status)+1) | |
sys.stdout.write(status) | |
print("[ok]") | |
if __name__ == '__main__': | |
if len(sys.argv)<2: | |
print("Program use python download.py http://myurl/podcast.rss") | |
else: | |
d = feedparser.parse(sys.argv[1]) | |
for item in d.entries: | |
title = item['title'] | |
url = item.enclosures[0].href | |
download(url,title+".mp3") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment