Advertisement
Guest User

Untitled

a guest
Dec 10th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. # 6.00 Problem Set 5
  2. # RSS Feed Filter
  3.  
  4. import feedparser
  5. import string
  6. import time
  7. from project_util import translate_html
  8. from news_gui import Popup
  9.  
  10. #-----------------------------------------------------------------------
  11. #
  12. # Problem Set 5
  13.  
  14. #======================
  15. # Code for retrieving and parsing
  16. # Google and Yahoo News feeds
  17. # Do not change this code
  18. #======================
  19.  
  20. def process(url):
  21.     """
  22.    Fetches news items from the rss url and parses them.
  23.    Returns a list of NewsStory-s.
  24.    """
  25.     feed = feedparser.parse(url)
  26.     entries = feed.entries
  27.     ret = []
  28.     for entry in entries:
  29.         guid = entry.guid
  30.         title = translate_html(entry.title)
  31.         link = entry.link
  32.         summary = translate_html(entry.summary)
  33.         try:
  34.             subject = translate_html(entry.tags[0]['term'])
  35.         except AttributeError:
  36.             subject = ""
  37.         newsStory = NewsStory(guid, title, subject, summary, link)
  38.         ret.append(newsStory)
  39.     return ret
  40.  
  41. #======================
  42. # Part 1
  43. # Data structure design
  44. #======================
  45.  
  46. # Problem 1
  47.  
  48. # TODO: NewsStory
  49. class NewsStory(object):
  50.     def __init__(self, GUID, title, subject, summary, link):
  51.         self.GUID = GUID
  52.         self.title = title
  53.         self.subject = subject
  54.         self.summary = summary
  55.         self.link = link
  56.        
  57.     def get_guid(self):
  58.         return self.GUID
  59.        
  60.     def get_title(self):
  61.         return self.title
  62.        
  63.     def get_subject(self):
  64.         return self.subject
  65.        
  66.     def get_summary(self):
  67.         return self.summary
  68.        
  69.     def get_link(self):
  70.         return self.link
  71.  
  72. #======================
  73. # Part 2
  74. # Triggers
  75. #======================
  76.  
  77. class Trigger(object):
  78.     def evaluate(self, story):
  79.         """
  80.        Returns True if an alert should be generated
  81.        for the given news item, or False otherwise.
  82.        """
  83.         raise NotImplementedError
  84.  
  85. # Whole Word Triggers
  86. # Problems 2-5
  87.  
  88. # TODO: WordTrigger
  89. class WordTrigger(Trigger):
  90.     def __init__(self, word):
  91.         self.word = word
  92.        
  93.     def is_word_in(self, text):
  94.         word = self.word.lower()
  95.         text = text.lower()
  96.         for punc in string.punctuation:
  97.             text = text.replace(punc, " ")
  98.         splittext = text.split(" ")
  99.         return word in splittext
  100.  
  101. # TODO: TitleTrigger
  102. class TitleTrigger(WordTrigger):
  103.     def __init__(self, word):
  104.         self.word = word
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement