feedlist: add new blog
[planet-ucc.git] / XMLWriter.py
1 #
2 # XMLWriter.py
3 #
4 # Generate arbitrary XML files
5 #
6 # (c) 2004, Davyd Madeley <[email protected]>
7 #
8
9 import time
10
11 class PlanetItem:
12         def __init__(self, blog, item):
13                 self.itemTitle  = item.itemTitle
14                 self.itemURL    = item.itemURL
15                 self.itemDate   = item.itemDate
16                 self.blogTitle  = blog.blogTitle
17                 self.blogName   = blog.blogName
18                 self.blogURL    = blog.blogURL
19                 self.imageURL   = blog.imageURL
20                 self.imageLink  = blog.imageLink
21                 self.contents   = item.contents
22                 self.commentsURL = item.commentsURL
23
24 class PlanetDate:
25         def __init__(self, date):
26                 self.planetDate = date
27                 self.items      = []
28
29 class Planet:
30         def __init__(self, bloglist):
31                 self.__bloglist__       = bloglist
32                 self.__tainted__        = True
33                 self.dates              = []
34                 
35         def append(self, blog):
36                 self.__bloglist__.append(blog)
37                 self.__tainted__        = True
38         
39         def __getNext__(self, bloglist):
40                 "Returns a PlanetItem reaped from a bloglist"
41                 latestTime              = 0
42                 holdingBlog             = None
43                 for blog in bloglist:
44                         if len(blog.items) > 0 and blog.items[0].itemDate > latestTime:
45                                 latestTime      = blog.items[0].itemDate
46                                 holdingBlog     = blog
47                 if holdingBlog == None:
48                         return None
49                 item    = holdingBlog.items.pop(0)
50                 return PlanetItem(holdingBlog, item)
51         
52         def sort(self):
53                 if self.__tainted__:
54                         # we need to sort the blogs into a single news feed
55                         # copy the bloglist to a working symbol
56                         bloglist        = self.__bloglist__ + []
57                         lastDate        = -1
58                         workingDate     = None
59                         self.dates      = []
60                         while True:
61                                 lastItem        = self.__getNext__(bloglist)
62                                 if lastItem == None:
63                                         break
64                                 # this checks to see if it's a new day
65                                 if time.localtime(lastItem.itemDate)[6] != lastDate:
66                                         lastDate        = time.localtime(lastItem.itemDate)[6]
67                                         workingDate     = PlanetDate(lastItem.itemDate)
68                                         self.dates.append(workingDate)
69                                 # append the item to the current date
70                                 workingDate.items.append(lastItem)      
71                         self.__tainted__        = False
72                 return self.dates
73                         
74
75 class XMLWriter:
76         def __init__(self, bloglist):
77                 self.blogs      = bloglist
78                 self.planet     = Planet(bloglist)
79                 self.items      = self.planet.sort()
80         
81         def write(self, doctype):
82                 # doctype should be something like XHTMLWriter.XHTMLWriter
83                 writer          = doctype(self.items)
84                 writer.parent   = self
85                 output          = writer.write()
86                 return output
87

UCC git Repository :: git.ucc.asn.au