Package toon :: Module rss
[hide private]
[frames] | no frames]

Source Code for Module toon.rss

  1  #!/usr/bin/env python 
  2  # 
  3  # Toonloop for Python 
  4  # 
  5  # Copyright 2008 Alexandre Quessy & Tristan Matthews 
  6  # http://toonloop.com 
  7  #  
  8  # Original idea by Alexandre Quessy 
  9  # http://alexandre.quessy.net 
 10  # 
 11  # Toonloop is free software: you can redistribute it and/or modify 
 12  # it under the terms of the GNU General Public License as published by 
 13  # the Free Software Foundation, either version 3 of the License, or 
 14  # (at your option) any later version. 
 15  # 
 16  # Toonloop is distributed in the hope that it will be useful, 
 17  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 18  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 19  # GNU General Public License for more details. 
 20  # 
 21  # You should have received a copy of the gnu general public license 
 22  # along with Toonloop.  If not, see <http://www.gnu.org/licenses/>. 
 23  # 
 24  """ 
 25  Media RSS Feed Generation 
 26  """ 
 27  import os 
 28  import glob 
 29  import datetime 
 30  import pprint 
 31   
 32  from nevow import rend 
 33  from nevow.inevow import IRequest 
 34   
 35  VERBOSE = True 
 36   
37 -def _format_date(dt):
38 """ 39 Converts a datetime into an RFC 822 formatted date 40 41 Input date must be in GMT. 42 43 Source : PyRSS2Gen - A Python library for generating RSS 2.0 feeds. 44 :author: Andrew Dalke <dalke@dalkescientific.com> 45 46 Looks like: 47 Sat, 07 Sep 2002 00:00:01 GMT 48 Can't use strftime because that's locale dependent 49 50 Isn't there a standard way to do this for Python? The 51 rfc822 and email.Utils modules assume a timestamp. The 52 following is based on the rfc822 module. 53 """ 54 return "%s, %02d %s %04d %02d:%02d:%02d GMT" % ( 55 ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()], 56 dt.day, 57 ["Jan", "Feb", "Mar", "Apr", "May", "Jun", 58 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][dt.month - 1], 59 dt.year, dt.hour, dt.minute, dt.second)
60
61 -class Item(object):
62 """ 63 Generates a RSS item 64 """
65 - def __init__(self, **kwargs):
66 self.title = '' 67 self.link = '' 68 self.description = '' 69 self.enclosure_url = '' 70 self.enclosure_length = 0 71 self.enclosure_type = 'text/txt' 72 self.guid = '' 73 self.pub_date = datetime.datetime.now() 74 #print "pub_date", self.pub_date 75 self.__dict__.update(kwargs)
76
77 - def __str__(self):
78 return """ 79 <item> 80 <title>%s</title> 81 <link>%s</link> 82 <description>%s</description> 83 <enclosure url="%s" length="%s" type="%s"> 84 </enclosure> 85 <guid isPermaLink="true">%s</guid> 86 <pubDate>%s</pubDate> 87 </item> 88 """ % ( 89 self.title, 90 self.link, 91 self.description, 92 self.enclosure_url, 93 self.enclosure_length, 94 self.enclosure_type, 95 self.guid, 96 _format_date(self.pub_date))
97
98 -class Channel(object):
99 - def __init__(self, **kwargs):
100 self.title = 'Toonloop' 101 self.link = 'http://localhost/' 102 self.description = 'Stop Motion Animation Software' 103 self.last_build_date = datetime.datetime.now() 104 # print "pub_date", self.last_build_date 105 self.generator = 'Toonloop' 106 self.docs = 'http://toonloop.com' 107 self.items = [] 108 self.__dict__.update(kwargs)
109
110 - def __str__(self):
111 items = "" 112 for item in self.items: 113 items += str(item) 114 return """<?xml version="1.0" encoding="us-ascii"?> 115 <rss version="2.0"> 116 <channel> 117 <title>%s</title> 118 <link>%s</link> 119 <description>%s</description> 120 <lastBuildDate>%s</lastBuildDate> 121 <generator>%s</generator> 122 <docs>%s</docs> 123 %s 124 </channel> 125 </rss> 126 """ % ( 127 self.title, 128 self.link, 129 self.description, 130 _format_date(self.last_build_date), 131 self.generator, 132 self.docs, 133 items)
134
135 -class RSSPage(rend.Page): # page.Element):
136 - def __init__(self, **kwargs):
137 self.root = os.path.expanduser("~/Documents/toonloop") 138 self.port = 8000 139 self.__dict__.update(kwargs)
140
141 - def renderHTTP(self, context):
142 """ 143 Renders the XML RSS text. 144 Lists the MOV files whose name starts with "movie_". 145 """ 146 global VERBOSE 147 request = IRequest(context) 148 server_address = "http://%s" % (request.getHeader('host')) 149 150 channel = Channel() 151 patt = "%s/*/*.mov" % (self.root) 152 print 'PATH: ', patt 153 movie_files = glob.glob(patt) 154 if VERBOSE: 155 print 'RSS enclosures :', movie_files 156 157 for f in movie_files: 158 file_name = os.path.split(f)[1] 159 project_name = os.path.dirname(f).split("/")[-1] 160 #print "project:", project_name 161 link = "%s/files/%s/%s" % (server_address, project_name, file_name) 162 channel.items.append(Item(title=file_name, enclosure_url=link, link=link, guid=file_name, enclosure_type="video/quicktime")) 163 return str(channel)
164 165 if __name__ == '__main__': 166 channel = Channel() 167 channel.items.append(Item(title='Hello', link='http://localhost/1', guid='1', enclosure_url='http://localhost/download/1')) 168 print str(channel) 169