Last active
December 17, 2015 18:18
-
-
Save jetsanix/5651874 to your computer and use it in GitHub Desktop.
podcast downloader (with password)
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
# edit from https://github.com/hrktir/download_podcast_mp3/blob/master/download_podcast_mp3.py | |
# echo "http://podcasturl/xml podname" >> urls.txt # one podcast rss in each line | |
# python podcast_downloader.py urls.txt | |
import urllib2 | |
from xml.dom.minidom import parse | |
import time | |
import os | |
import sys | |
user="podcastuser" | |
password="password" | |
def getPodcastXmlDom(url): | |
response = urllib2.urlopen(url) | |
dom1 = parse(response) | |
return dom1 | |
def getItemList(dom): | |
items = dom.getElementsByTagName('item') | |
if len(items) > 0: | |
return parseItemsAsRSS(items) | |
else: | |
raise Exception("no item element found") | |
def parseItemsAsRSS(items): | |
results = [] | |
for item in items: | |
idict = dict() | |
enc = item.getElementsByTagName('enclosure') | |
if len(enc) == 1: | |
idict["url"] = enc[0].getAttribute("url") | |
ititle = item.getElementsByTagName('title') | |
if len(ititle) == 1: | |
idict["title"] = ititle[0].firstChild.wholeText | |
results.append(idict) | |
return results | |
def getFilename4mp3(idict): | |
ret = idict["title"] | |
ret = ret + ".mp3" | |
return ret | |
def downloadFile(url, filename, folder): | |
print "download " + url + " as " + filename | |
os.system( "mkdir -p "+folder ) | |
filename = filename.encode("utf-8") | |
command = 'wget --http-user='+user+' --http-password='+password+' -c "' + url +'" -O '+ folder +'/' | |
command += unicode(filename,"utf-8") | |
os.system( command.encode("utf-8") ) | |
def main(): | |
fileuri = sys.argv[1] | |
f1 = open(fileuri, "r") | |
urls = f1.readlines() | |
for url in urls: | |
ul = url.split() | |
items = getItemList(getPodcastXmlDom( ul[0] )) | |
for item in items: | |
downloadFile(item["url"], getFilename4mp3(item), ul[1]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment