# 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(object): 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 # Whole Word Triggers # Problems 2-5 # TODO: WordTrigger class WordTrigger(Trigger): def __init__(self, word): self.word = word def is_word_in(self, text): word = self.word.lower() text = text.lower() for punc in string.punctuation: text = text.replace(punc, " ") splittext = text.split(" ") return word in splittext # TODO: TitleTrigger class TitleTrigger(WordTrigger): def __init__(self, word): self.word = word