Created
January 22, 2013 16:45
-
-
Save thenormalsquid/4596158 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import feedparser | |
import string | |
import time | |
from project_util import translate_html | |
from news_gui import Popup | |
#----------------------------------------------------------------------- | |
# | |
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 | |
# Whole Word Triggers | |
# Problems 2-5 | |
# TODO: WordTrigger | |
class WordTrigger(Trigger): | |
def __init__(self, word): | |
self.word = word.lower() | |
def is_word_in(self, text): | |
for i in text: | |
if i in string.punctuation: | |
text.replace(i,'') | |
if self.word.lower() in text.lower(): | |
print self.word.lower() | |
return True | |
else: | |
return False | |
# TODO: TitleTrigger | |
class TitleTrigger(WordTrigger): | |
def __init__(self,word): | |
WordTrigger.__init__(self,word) | |
def evaluate(self, title): | |
WordTrigger.is_word_in(self,title.get_title()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment