""" Python module for use in the SoCal Linux Expo exhibit. project builtin functions. """ import string, urllib2, random from PsychePostProcessor import PsychePostProcessor from RSS import ns, CollectionChannel, TrackingChannel class PsycheRss( PsychePostProcessor ): """ Psyche object to get an RSS feed and return the desired number of news items. If no number is specified, use the default number, 3. """ phrases = [ 'well', 'in detail', 'specifically', 'basically', 'what happened was', 'umm', 'ah', 'see' ] def __init__( self, args ): """ Run the command get_rss(), and save its result using the inherired Set_Result(). args is a list input """ try: num_entries = int(args[1]) except IndexError: num_entries = 3 self.set_result(self.get_rss(args[0], num_entries)) def get_rss( self, rssUrl, num_entries ): """ Get the rss feed and format it. """ self.tc = TrackingChannel() try: self.tc.parse(rssUrl) except (OSError, urllib2.URLError): return 'I could not find the RSS feed, %s.\n'%rssUrl RSS10_TITLE = (ns.rss10, 'title') RSS10_DESC = (ns.rss10, 'description') items = self.tc.listItems() output = [] for i in range(num_entries): try: item_data = self.tc.getItem(items[i]) except IndexError: break t= item_data.get(RSS10_TITLE, "(none)") d= item_data.get(RSS10_DESC, "(none)") intro = self.phrases[random.randint(0,len(self.phrases)-1)] if i == 0: lead = '' else: lead = 'next, ' output.append('%sheadline %d: %s:\n %s, %s\n'%(lead,i+1,t,intro,d)) return string.join(output) ################################################################################ if __name__ == '__main__': f = ['file:///home/flatline/documents/psyche/sample_data/slashdot.rss'] rss = PsycheRss(f) print rss.get_result()