+#
+# XMLWriter.py
+#
+# Generate arbitrary XML files
+#
+#
+
+import time
+
+class PlanetItem:
+ def __init__(self, blog, item):
+ self.itemTitle = item.itemTitle
+ self.itemURL = item.itemURL
+ self.itemDate = item.itemDate
+ self.blogTitle = blog.blogTitle
+ self.blogURL = blog.blogURL
+ self.imageURL = blog.imageURL
+ self.imageLink = blog.imageLink
+ self.contents = item.contents
+
+class PlanetDate:
+ def __init__(self, date):
+ self.planetDate = date
+ self.items = []
+
+class Planet:
+ def __init__(self, bloglist):
+ self.__bloglist__ = bloglist
+ self.__tainted__ = True
+ self.dates = []
+
+ def append(self, blog):
+ self.__bloglist__.append(blog)
+ self.__tainted__ = True
+
+ def __getNext__(self, bloglist):
+ "Returns a PlanetItem reaped from a bloglist"
+ latestTime = 0
+ holdingBlog = None
+ for blog in bloglist:
+ if len(blog.items) > 0 and blog.items[0].itemDate > latestTime:
+ latestTime = blog.items[0].itemDate
+ holdingBlog = blog
+ if holdingBlog == None:
+ return None
+ item = holdingBlog.items.pop(0)
+ return PlanetItem(holdingBlog, item)
+
+ def sort(self):
+ if self.__tainted__:
+ # we need to sort the blogs into a single news feed
+ # copy the bloglist to a working symbol
+ bloglist = self.__bloglist__ + []
+ lastDate = -1
+ workingDate = None
+ self.dates = []
+ while True:
+ lastItem = self.__getNext__(bloglist)
+ if lastItem == None:
+ break
+ # this checks to see if it's a new day
+ if time.localtime(lastItem.itemDate) != lastDate:
+ workingDate = PlanetDate(lastItem.itemDate)
+ self.dates.append(workingDate)
+ # append the item to the current date
+ workingDate.items.append(lastItem)
+ self.__tainted__ = False
+ return self.dates
+
+
+class XMLWriter:
+ def __init__(self, doctype, bloglist):
+ self.planet = Planet(bloglist)
+ self.items = self.planet.sort()
+ # doctype should be something like XMLWriter.XHTMLWriter
+ self.writer = doctype(self.items)
+
+ def write(self):
+ output = self.writer.write()
+ return output
+
+class XHTMLWriter:
+ def __init__(self, planet):
+ self.planet = planet
+ self.maxitems = 100
+
+ def __write_item__(self, item):
+ output = ''
+ output += '<div id="item">\n'
+ output += '<h2>%s: %s</h2>\n' % (item.blogTitle, item.itemTitle)
+ output += '<p class="time">\n'
+ output += '(%s)\n' % time.strftime('%A %B %d, %Y %H:%M %Z', time.localtime(item.itemDate))
+ output += '</p>\n'
+ output += '<p class="body">\n'
+ output += item.contents
+ output += '\n</p>\n'
+ return output
+
+ def write(self):
+ itemcount = 0
+ output = ''
+ output += '<?xml version="1.0" encoding="UTF-8"?>\n'
+ output += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n'
+ output += '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >\n'
+ output += '<head>\n'
+ output += '<title>Planet UCC</title>\n'
+ # XXX: we'll want a style sheet in here
+ output += '</head>\n'
+ output += '<body>\n'
+ # XXX: we want some stuff in here, I'm sure
+ for date in self.planet:
+ output += '<h1>%s</h1>\n' % time.strftime('%A %B %d, %Y', time.localtime(date.planetDate))
+ for item in date.items:
+ output += self.__write_item__(item)
+ # see how many items we've written
+ itemcount += 1
+ if itemcount >= self.maxitems:
+ break
+ # again, check to see if we've written the maximum number of items
+ if itemcount >= self.maxitems:
+ break
+ # XXX: we want further stuff here
+ output += '</body>'
+ output += '</html>'
+ return output