#! /usr/bin/python

# --------------------------------------------------------------- #
# pcast.py -- A script to listen to podcasts                      #
# Copyright (C) 2005, Tejas Dinkar <tejasdinkar AT gmail DOT com  #
#                                                                 #
# This program is free software; you can redistribute it and/or   #
# modify it under the terms of the GNU General Public License as  #
# published by the Free Software Foundation; either version 2 of  #
# the License, or (at your option) any later version.             #
#                                                                 #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of  #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the    #
# GNU General Public License for more details.                    #
#                                                                 #
# You should have received a copy of the GNU General Public       #
# License along with this program; if not, write to the Free      #
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,     #
# Boston, MA  02110-1301  USA                                     #
# --------------------------------------------------------------- #

#
# This is the Common Section, this area is useful even if this is an
# imported module
#

# The Following is will represent a podcast Stream
# It declared as stream(values)
# values is a list with values = [title, link]
# this may be changed later to include html as well
#
class stream:
	def __init__(self, values):
		self.title = values[0]
		self.link = values[1]

def get_pcast_file():
	'''This Gets the name of the pcast file'''
	import sys
	try:
		pcast_file_name = sys.argv[1]
		pcast_file = open(pcast_file_name)
	except IndexError:
		print "Please Pass a File to the Script"
		sys.exit(1)
	except IOError:
		import urllib
		try:
			pcast_file = urllib.urlopen(pcast_file_name)
		except IOError:
			print "Cannot Open File"
			sys.exit(1)
	return pcast_file
	

def get_rss_feed(pcast_file):
	'''This Gets the location of the RSS feed from the given .pcast file'''
	from xml.dom import minidom
	xmldoc = minidom.parse(pcast_file)
	link = xmldoc.getElementsByTagName("link")[0]
	rss_feed = link.getAttribute("href")
	return rss_feed

def get_xmldoc(rss_feed):
	'''This Opens the Main RSS feed and parses it'''
	import urllib
	from xml.dom import minidom
	rss_site = urllib.urlopen(rss_feed)
	xmldoc = minidom.parse(rss_site)
	return xmldoc

def get_headers(xmldoc):
	'''This Gets the title and description of the channel
	This Function is Really not needed for this, but is useful
	if this Module is imported somewhere else'''
	headers = {}
	headers_list = ["title","link"]
	channel = xmldoc.getElementsByTagName("channel")[0]
	for i in channel.childNodes:
		if i.nodeName in headers_list:
			headers[i.nodeName] = i.firstChild.data
	return headers
		
def get_items(xmldoc):
	'''This returns a list of items of class stream... each representing
	a stream which can be played'''
	items_list = xmldoc.getElementsByTagName("item")
	items = []
	for i in items_list:
		try:
			title = i.getElementsByTagName("title")[0].firstChild.data
			link = i.getElementsByTagName("enclosure")[0].getAttribute("url")
			items.append(stream([title,link]))
		except IndexError: #Item is not a stream
			pass
	return items	
	

# The Next Block is useful even if this is an imported module
pcast_file = get_pcast_file()
rss_feed = get_rss_feed(pcast_file)
xmldoc = get_xmldoc(rss_feed)
headers = get_headers(xmldoc)	# Not REALLY needed if xmms will auto play files
items = get_items(xmldoc)



#
# The Following Block is if this is not imported.
# It only has the function play_all
#

if __name__ == "__main__":	
	
	def play_all(play_command,items):
		import os
		string = " ".join([i.link for i in items])
		print string
		os.system("%s %s" % (play_command,string))

	play_all("xmms -p",items)
