# 6.00 Problem Set 8 # RSS Feed Reader, Part II ############################################################## ## ## Please duplicate your ps7 NewsStory class ## process, and printStories below. ## ############################################################## import feedparser import string import time from project_util import translate_html # TODO: Your PS7 code here #printStories(process("http://news.google.com/?output=rss", GOOGLE)) #printStories(process("http://rss.news.yahoo.com/rss/topstories", YAHOO)) #----------------------------------------------------------------------- # # Problem Set 8 #====================== # Part 1 # Trigger Definitions #====================== class Trigger: def evaluate(self, story): return True # Whole Word Triggers # Questions 1-3 # TODO: TitleTrigger # TODO: SubjectTrigger # TODO: SummaryTrigger # Composite Triggers # Questions 4-6 # TODO: NotTrigger # TODO: AndTrigger # TODO: OrTrigger # Phrase Trigger # Question 7 # TODO: PhraseTrigger #====================== # Part 2 # Tying it all together #====================== def filter(stories, triggerlist): """ Takes in a list of NewsStory-s. Returns ony those stories for whom a trigger in triggerlist fires. """ # TODO: Question 8 return [] def readTriggerConfig(filename): """ Returns a list of trigger objects that correspond to the rules set in the file filename """ # Here's some code that we give you # to read in the file and eliminate # blank lines and comments triggerfile = open(filename, "r") all = [ line.rstrip() for line in triggerfile.readlines() ] lines = [] for line in all: if len(line) == 0 or line[0] == '#': continue lines.append(line) # TODO: Question 10 # 'lines' has a list of lines you need to parse # Build a set of triggers from it and # return the appropriate ones SLEEPTIME = 60 #seconds -- how often we poll if __name__ == '__main__': # A sample trigger list - you'll replace # this with something more configurable in Question 10 t1 = SubjectTrigger("sports") t2 = SummaryTrigger("Obama") t3 = PhraseTrigger("Hillary Clinton") t4 = OrTrigger(t2, t3) triggerlist = [t1, t4] # TODO: Question 10 # After implementing readTriggerConfig, uncomment this line # triggerlist = readTriggerConfig("triggers.txt") while True: #print "Polling..." stories = process("http://news.google.com/?output=rss", GOOGLE) stories.extend(process("http://rss.news.yahoo.com/rss/topstories", YAHOO)) # Only select stories we're interested in # TODO: Question 8 # Don't print a story if we already have # TODO: Question 9 printStories(stories) #print "Sleeping..." time.sleep(SLEEPTIME)