Problem Set 5 makes use of Google’s RSS feed to search for keywords and return stories matching those words. The exercise I believe is the first to begin using classes and inheritance. In order to avoid code duplication, a generic WordTrigger is created to look for for a “word” inside a “text.” Then, we create subclasses of triggers to look at the title, summary, or subject fields. Later on, it creates AND/OR functions to make more useful searches.
My functions passed the ps6_test.py, but when I try using provided main_thread’s implementation that pulls RSS stories from google (stories = process(“http://news.google.com/?output=rss”)), only the title trigger seemed to work. I suspect the provided feedparser.py may not be up to date with the RSS feed. On the other hand, feedparser.py looks like it is mapping the description element to “summary”, so the error is not obvious to me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
if element == 'description': element = 'summary' # 6.00 Problem Set 5 # RSS Feed Filter import feedparser import string import time from project_util import translate_html from news_gui import Popup #----------------------------------------------------------------------- # # Problem Set 5 #====================== # Code for retrieving and parsing # Google and Yahoo News feeds # Do not change this code #====================== def process(url): """ Fetches news items from the rss url and parses them. Returns a list of NewsStory-s. """ feed = feedparser.parse(url) entries = feed.entries ret = [] for entry in entries: guid = entry.guid title = translate_html(entry.title) link = entry.link summary = translate_html(entry.summary) try: subject = translate_html(entry.tags[0]['term']) except AttributeError: subject = "" newsStory = NewsStory(guid, title, subject, summary, link) ret.append(newsStory) return ret #====================== # Part 1 # Data structure design #====================== # Problem 1 # TODO: NewsStory class NewsStory(): def __init__(self, guid, title, subject, summary, link): self.guid = guid self.title = title self.subject = subject self.summary = summary self.link = link def get_guid(self): return self.guid def get_title(self): return self.title def get_subject(self): return self.subject def get_summary(self): return self.summary def get_link(self): return self.link #====================== # Part 2 # Triggers #====================== class Trigger(object): def evaluate(self, story): """ Returns True if an alert should be generated for the given news item, or False otherwise. """ raise NotImplementedError """ It must implement the evaluate method that takes a news item (NewsStory object) as an input and returns True if an alert should be generated for that item. We will not use the implementation of the Trigger class """ # Whole Word Triggers # Problems 2-5 # TODO: WordTrigger # It returns True if the whole word word is present class WordTrigger(Trigger): def is_word_in(self, word, text): text = text.replace("'", " ") alphabet = list('abcdefghijkl mnopqrstuvwxyz') text = "".join(letter for letter in text if letter in alphabet) text = text.split() for a in text: if word == a: return True else: continue return False # TODO: TitleTrigger # it should treat Intel and intel as being equal class TitleTrigger(WordTrigger): def __init__(self, word): self.word = word def evaluate(self, story): return self.is_word_in(self.word.lower(), story.title.lower()) # TODO: SubjectTrigger class SubjectTrigger(WordTrigger): def __init__(self, word): self.word = word def evaluate(self, story): return self.is_word_in(self.word.lower(), story.subject.lower()) # TODO: SummaryTrigger class SummaryTrigger(WordTrigger): def __init__(self, word): self.word = word def evaluate(self, story): return self.is_word_in(self.word.lower(), story.summary.lower()) # Composite Triggers # Problems 6-8 # TODO: NotTrigger # given a trigger T (any trigger) and news item x # Not trigger returns the equivalent of not T.evaluate(x) # n = NotTrigger(self.tt) # b = NewsStory("guid", "title", "subj", "summary", "link") # self.assertFalse(n.evaluate(b), "Expected False. Was not was False!") class NotTrigger(Trigger): def __init__(self, otherTrigger): self.otherTrigger = otherTrigger def evaluate(self, story): return not self.otherTrigger.evaluate(story) # why is self.otherTrigger a Trigger and otherTrigger is a NewsStory? # Is a NewsStory part of a Trigger? Where does it reside? # TODO: AndTrigger class AndTrigger(Trigger): def __init__(self, T1, T2): self.T1 = T1 self.T2 = T2 def evaluate(self, story): return self.T1.evaluate(story) and self.T2.evaluate(story) # TODO: OrTrigger class OrTrigger(Trigger): def __init__(self, T1, T2): self.T1 = T1 self.T2 = T2 def evaluate(self, story): return self.T1.evaluate(story) or self.T2.evaluate(story) # Phrase Trigger # Question 9 # TODO: PhraseTrigger # constructor takes the phrase as arg # method takes the story as arg class PhraseTrigger(Trigger): def __init__(self, phrase): self.phrase = phrase def evaluate(self, story): title = self.phrase in story.title subject = self.phrase in story.subject summary = self.phrase in story.summary return title or subject or summary #====================== # Part 3 # Filtering #====================== def filter_stories(stories, triggerlist): """ Takes in a list of NewsStory-s. Returns only those stories for whom a trigger in triggerlist fires. """ # TODO: Problem 10 # This is a placeholder (we're just returning all the stories, with no filtering) # return stories matches = [] for trigs in triggerlist: for stors in stories: if trigs.evaluate(stors): matches.append(stors) return matches #====================== # Part 4 # User-Specified Triggers #====================== 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: Problem 11 # 'lines' has a list of lines you need to parse # Build a set of triggers from it and # return the appropriate ones # print lines # ['t1 TITLE nfl', 't2 SUBJECT Japan', 't3 PHRASE Supreme Court', 't4 AND t2 t3', 'ADD t1 t4'] allTriggers ={ "TITLE": TitleTrigger, "SUBJECT": SubjectTrigger, "SUMMARY": SummaryTrigger, "NOT": NotTrigger, "AND": AndTrigger, "OR": OrTrigger, "PHRASE": PhraseTrigger } triggers = {} results = [] for line in lines: line = line.split() if line[0] == "ADD": for i in line[1:]: results.append(triggers[i]) return results name = line[0] trigger = line[1] if trigger == "AND" or trigger == "OR": arg1 = triggers[line[2]] arg2 = triggers[line[3]] triggers[name] = allTriggers[trigger](arg1, arg2) #pass the objects, not the string else: arg = "".join(line[2:]) triggers[name] = allTriggers[trigger](arg) import thread def main_thread(p): # A sample trigger list - you'll replace # this with something more configurable in Problem 11 t1 = SubjectTrigger("Obama") t2 = SummaryTrigger("MIT") t3 = PhraseTrigger("Supreme Court") t4 = OrTrigger(t2, t3) triggerlist = [t1, t4] # TODO: Problem 11 # After implementing readTriggerConfig, uncomment this line triggerlist = readTriggerConfig("triggers.txt") guidShown = [] while True: print "Polling..." # Get stories from Google's Top Stories RSS news feed stories = process("http://news.google.com/?output=rss") # Get stories from Yahoo's Top Stories RSS news feed stories.extend(process("http://rss.news.yahoo.com/rss/topstories")) # Only select stories we're interested in stories = filter_stories(stories, triggerlist) # Don't print a story if we have already printed it before newstories = [] for story in stories: if story.get_guid() not in guidShown: newstories.append(story) for story in newstories: guidShown.append(story.get_guid()) p.newWindow(story) print "Sleeping..." time.sleep(SLEEPTIME) SLEEPTIME = 60 #seconds -- how often we poll if __name__ == '__main__': p = Popup() thread.start_new_thread(main_thread, (p,)) p.start() |