#!/bin/env python
#coding:utf-8

# rss1.0 parser.
#
# Author: dl
# 
# License under Creative Commons License
# http://creativecommons.org/licenses/by-nc-sa/2.5/
# 


class RSS10:
	RDF_HEADER = '''<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF 
	xmlns="http://purl.org/rss/1.0/"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"	
	xml:lang="ja">
	'''

	RDF_CHANNEL = ''' 
	<channel rdf:about="%s">
		<title><![CDATA[%s]]></title>
		<link>%s</link>
		<description><![CDATA[%s]]></description>
		<items>
			<rdf:Seq>
			%s
			</rdf:Seq>
		</items>
	</channel>
	'''

	RDF_LI = '''
			<rdf:li rdf:resource="%s"/>
	'''

	RDF_ITEM = '''
	<item rdf:about="%s">
		<title><![CDATA[%s]]></title>
		<link>%s</link>
		<description><![CDATA[%s]]></description>
	</item>
	'''

	RDF_FOOTER = '''
</rdf:RDF>
'''
	
	# initでchannel情報を登録
	def __init__(self, \
			channelAbout, \
			channelTitle, \
			channelLink, \
			channelDescription):
		
		# channel.about
		self.channelAbout = channelAbout
		# channel.title
		self.channelTitle = channelTitle
		# channel.link
		self.channelLink = channelLink
		# channel.description
		self.channelDescription = channelDescription
		
		# li list
		self.liList = []
	
	# liは配列で、[about, title, link, description]の形式で渡すこと
	def addLi(self, li):
		self.liList.append(li)
	
	# seq.liを取得
	def getSeqLi(self):
		buf = ""
		for li in self.liList:
			buf += self.RDF_LI % (li[0])
		return buf
	
	# liListからitem部分を生成
	def getItemList(self):
		ret = []
		for li in self.liList:
			ret.append(self.RDF_ITEM % (li[0], li[1], li[2], li[3]))
		return ret
	
	# 指定されている情報からRSS1.0を生成
	def getRSS(self):
		buf = ""
		buf += self.RDF_HEADER
		buf += self.RDF_CHANNEL % \
				(self.channelAbout, \
				self.channelTitle, \
				self.channelLink, \
				self.channelDescription, \
				self.getSeqLi())
		items = self.getItemList()
		for item in items:
			buf += item
		buf += self.RDF_FOOTER
		return buf

def main():
	rss = RSS10("about", "title", "link", "desc")
	rss.addLi(["about1", "title1", "link1", "desc1"])
	rss.addLi(["about2", "title2", "link2", "desc2"])
	rss.addLi(["about3", "title3", "link3", "desc3"])
	print rss.getRSS()

if __name__ == "__main__":
	main()


#end