Guest User

0003 WIP solution PS5

a guest
Aug 13th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.45 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.  
  50. class NewsStory(object):
  51.    
  52.     def __init__(self, guid, title, subject, summary, link):
  53.         self.guid = guid
  54.         self.title = title
  55.         self.subject = subject
  56.         self.summary = summary
  57.         self.link = link
  58.        
  59.     def get_guid(self):
  60.         return self.guid
  61.        
  62.     def get_title(self):
  63.         return self.title
  64.    
  65.     def get_subject(self):
  66.         return self.subject
  67.    
  68.     def get_summary(self):
  69.         return self.summary
  70.        
  71.     def get_link(self):
  72.         return self.link
  73.  
  74.  
  75.  
  76. #======================
  77. # Part 2
  78. # Triggers
  79. #======================
  80.  
  81. class Trigger(object):
  82.     def evaluate(self, story):
  83.         """
  84.        Returns True if an alert should be generated
  85.        for the given news item, or False otherwise.
  86.        """
  87.         raise NotImplementedError
  88.  
  89. # Whole Word Triggers
  90. # Problems 2-5
  91. # problem 2
  92.  
  93. class WordTrigger(Trigger):
  94.     def __init__(self,word):
  95.         self.word = word
  96.    
  97.     def is_word_in(self, text):
  98.         text = text.lower()
  99.         word = self.word.lower()
  100.        
  101.         for each_punc in string.punctuation:
  102.             #print '1,', text, type(text)
  103.             text = text.replace(each_punc, " ")   #list
  104.             #print '1', 'SPLITTED LIST OF TEXT', text
  105.        
  106.        
  107.         text = text.split(" ")
  108.         #print "FINAL LIST OF TEXT: ", text
  109.        
  110.         for each_word in text:
  111.             #print each_word, self.word
  112.             if each_word == word:
  113.                 return True
  114.         return False
  115.        
  116. tiger = WordTrigger('tiger')  
  117. print tiger.is_word_in('there is a tiger here'), "'there is a tiger here' - Should be TRUE'"
  118. print tiger.is_word_in('there is a boy here'), "'there is a tiger here' = Should be FALSE'"
  119. # problem 3
  120.  
  121. class TitleTrigger(WordTrigger):
  122.     def evaluate(self, story):
  123.         return self.is_word_in(story.get_title())      
  124.        
  125.    
  126.  
  127. class SubjectTrigger(WordTrigger):
  128.     def evaluate(self,story):
  129.         return self.is_word_in(story.get_subject())
  130.        
  131. class SummaryTrigger(WordTrigger):
  132.     def evaluate(self,story):
  133.         return self.is_word_in(story.get_summary())
  134.  
  135.  
  136. # Composite Triggers
  137. # Problems 6-8
  138.  
  139. class NotTrigger(Trigger):
  140.     def __init__(self, trigger):
  141.         self.trigger = trigger
  142.     def evaluate(self,text):
  143.         return not self.trigger.is_word_in(text)
  144.        
  145. not_tiger = NotTrigger(WordTrigger('tiger'))
  146. print not_tiger.evaluate('there is a tiger here'), "'NOT there is a tiger here' - should be FALSE"
  147. print not_tiger.evaluate('there is a boy here'), "'NOT there is a boy here' - should be TRUE"
  148.      
  149. # TODO: AndTrigger
  150. # TODO: OrTrigger
  151.  
  152.  
  153. # Phrase Trigger
  154. # Question 9
  155.  
  156. # TODO: PhraseTrigger
  157.  
  158.  
  159. #======================
  160. # Part 3
  161. # Filtering
  162. #======================
  163.  
  164. def filter_stories(stories, triggerlist):
  165.     """
  166.    Takes in a list of NewsStory-s.
  167.    Returns only those stories for whom
  168.    a trigger in triggerlist fires.
  169.    """
  170.     # TODO: Problem 10
  171.     # This is a placeholder (we're just returning all the stories, with no filtering)
  172.     # Feel free to change this line!
  173.     return stories
  174.  
  175. #======================
  176. # Part 4
  177. # User-Specified Triggers
  178. #======================
  179.  
  180. def readTriggerConfig(filename):
  181.     """
  182.    Returns a list of trigger objects
  183.    that correspond to the rules set
  184.    in the file filename
  185.    """
  186.     # Here's some code that we give you
  187.     # to read in the file and eliminate
  188.     # blank lines and comments
  189.     triggerfile = open(filename, "r")
  190.     all = [ line.rstrip() for line in triggerfile.readlines() ]
  191.     lines = []
  192.     for line in all:
  193.         if len(line) == 0 or line[0] == '#':
  194.             continue
  195.         lines.append(line)
  196.  
  197.     # TODO: Problem 11
  198.     # 'lines' has a list of lines you need to parse
  199.     # Build a set of triggers from it and
  200.     # return the appropriate ones
  201.    
  202. import thread
  203.  
  204. def main_thread(p):
  205.     # A sample trigger list - you'll replace
  206.     # this with something more configurable in Problem 11
  207.     t1 = SubjectTrigger("Obama")
  208.     t2 = SummaryTrigger("MIT")
  209.     t3 = PhraseTrigger("Supreme Court")
  210.     t4 = OrTrigger(t2, t3)
  211.     triggerlist = [t1, t4]
  212.    
  213.     # TODO: Problem 11
  214.     # After implementing readTriggerConfig, uncomment this line
  215.     #triggerlist = readTriggerConfig("triggers.txt")
  216.  
  217.     guidShown = []
  218.    
  219.     while True:
  220.         print "Polling..."
  221.  
  222.         # Get stories from Google's Top Stories RSS news feed
  223.         stories = process("http://news.google.com/?output=rss")
  224.         # Get stories from Yahoo's Top Stories RSS news feed
  225.         stories.extend(process("http://rss.news.yahoo.com/rss/topstories"))
  226.  
  227.         # Only select stories we're interested in
  228.         stories = filter_stories(stories, triggerlist)
  229.    
  230.         # Don't print a story if we have already printed it before
  231.         newstories = []
  232.         for story in stories:
  233.             if story.get_guid() not in guidShown:
  234.                 newstories.append(story)
  235.        
  236.         for story in newstories:
  237.             guidShown.append(story.get_guid())
  238.             p.newWindow(story)
  239.  
  240.         print "Sleeping..."
  241.         time.sleep(SLEEPTIME)
  242.  
  243. SLEEPTIME = 60 #seconds -- how often we poll
  244. if __name__ == '__main__':
  245.     p = Popup()
  246.     thread.start_new_thread(main_thread, (p,))
  247.     p.start()
Advertisement
Add Comment
Please, Sign In to add comment