Advertisement
Guest User

Pset7_ta_1

a guest
Mar 2nd, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. # 6.00.1x Problem Set 7
  2. # RSS Feed Filter
  3.  
  4. import feedparser
  5. import string
  6. import time
  7. from project_util import translate_html
  8. from Tkinter import *
  9.  
  10.  
  11. #-----------------------------------------------------------------------
  12. #
  13. # Problem Set 7
  14.  
  15. #======================
  16. # Code for retrieving and parsing RSS 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. #======================
  43. # Part 1
  44. # Data structure design
  45. #======================
  46.  
  47. # Problem 1
  48.  
  49. class NewsStory(object):
  50. def __init__(self,vguid,vtitle,vsubject,vsummary,vlink):
  51. self.vguid=vguid
  52. self.vtitle=vtitle
  53. self.vsubject=vsubject
  54. self.vsummary=vsummary
  55. self.vlink=vlink
  56. def getGuid(self):
  57. return self.vguid
  58. def getTitle(self):
  59. return self.vtitle
  60. def getSubject(self):
  61. return self.vsubject
  62. def getSummary(self):
  63. return self.vsummary
  64. def getLink(self):
  65. return self.vlink
  66.  
  67. #======================
  68. # Part 2
  69. # Triggers
  70. #======================
  71.  
  72. class Trigger(object):
  73. def evaluate(self, story):
  74. """
  75. Returns True if an alert should be generated
  76. for the given news item, or False otherwise.
  77. """
  78. raise NotImplementedError
  79.  
  80. # Whole Word Triggers
  81. # Problems 2-5
  82.  
  83. # TODO: WordTrigger
  84. class WordTrigger(Trigger):
  85. def __init__(self,vword):
  86. self.vword=vword
  87.  
  88. def isWordIn(self,vtext):
  89. for each_pnc in string.punctuation:
  90. vtext = vtext.replace(each_pnc," ")
  91. vtext = vtext.lower().split(" ")
  92. self.vword = self.vword.lower()
  93. return self.vword in vtext
  94.  
  95. # TODO: TitleTrigger
  96. class TitleTrigger(WordTrigger):
  97. def evaluate(self, story):
  98. return self.isWordIn(story.getTitle())
  99.  
  100. # TODO: SubjectTrigger
  101. class SubjectTrigger(WordTrigger):
  102. def evaluate(self, story):
  103. return self.isWordIn(story.getSubject())
  104.  
  105. # TODO: SummaryTrigger
  106. class SummaryTrigger(WordTrigger):
  107. def evaluate(self, story):
  108. return self.isWordIn(story.getSummary())
  109.  
  110.  
  111. # Composite Triggers
  112. # Problems 6-8
  113.  
  114. # TODO: NotTrigger
  115. class NotTrigger(Trigger):
  116. def __init__(self, vtrigger):
  117. self.vtrigger = vtrigger
  118.  
  119. def evaluate(self, story):
  120. return not self.vtrigger.evaluate(story)
  121.  
  122. # TODO: AndTrigger
  123. class AndTrigger(Trigger):
  124. def __init__(self, vtrigger1,vtrigger2):
  125. self.vtrigger1 = vtrigger1
  126. self.vtrigger2 = vtrigger2
  127.  
  128. def evaluate(self, story):
  129. return self.vtrigger1.evaluate(story) and self.vtrigger2.evaluate(story)
  130.  
  131. # TODO: OrTrigger
  132. class OrTrigger(Trigger):
  133. def __init__(self, vtrigger1,vtrigger2):
  134. self.vtrigger1 = vtrigger1
  135. self.vtrigger2 = vtrigger2
  136.  
  137. def evaluate(self, story):
  138. return self.vtrigger1.evaluate(story) or self.vtrigger2.evaluate(story)
  139.  
  140.  
  141. # Phrase Trigger
  142. class PhraseTrigger(Trigger):
  143. def __init__(self,vphrase):
  144. self.vphrase=vphrase
  145. def evaluate(self, story):
  146. vtitle = story.getTitle()
  147. vsubject = story.getSubject()
  148. vsummary = story.getSummary()
  149. return self.vphrase in vtitle or self.vphrase in vsubject or self.vphrase in vsummary
  150.  
  151.  
  152. # TODO: PhraseTrigger
  153.  
  154.  
  155. #======================
  156. # Part 3
  157. # Filtering
  158. #======================
  159.  
  160. def filterStories(stories, triggerlist):
  161. """
  162. Takes in a list of NewsStory instances.
  163.  
  164. Returns: a list of only the stories for which a trigger in triggerlist fires.
  165. """
  166. # TODO: Problem 10
  167. # This is a placeholder (we're just returning all the stories, with no filtering)
  168.  
  169. def filterStories(stories, triggerlist):
  170. """
  171. Takes in a list of NewsStory instances.
  172.  
  173. Returns: a list of only the stories for which a trigger in triggerlist fires.
  174. """
  175. new_stories = []
  176. for story in stories:
  177. for vtrigger in triggerlist:
  178. if vtrigger.evaluate(story) == True:
  179. if story not in new_stories:
  180. new_stories.append(story)
  181. stories = new_stories
  182. return stories
  183.  
  184. #======================
  185. # Part 4
  186. # User-Specified Triggers
  187. #======================
  188.  
  189. def makeTrigger(triggerMap, triggerType, params, name):
  190. """
  191. Takes in a map of names to trigger instance, the type of trigger to make,
  192. and the list of parameters to the constructor, and adds a new trigger
  193. to the trigger map dictionary.
  194.  
  195. triggerMap: dictionary with names as keys (strings) and triggers as values
  196. triggerType: string indicating the type of trigger to make (ex: "TITLE")
  197. params: list of strings with the inputs to the trigger constructor (ex: ["world"])
  198. name: a string representing the name of the new trigger (ex: "t1")
  199.  
  200. Modifies triggerMap, adding a new key-value pair for this trigger.
  201.  
  202. Returns a new instance of a trigger (ex: TitleTrigger, AndTrigger).
  203. """
  204. # TODO: Problem 11
  205.  
  206.  
  207. def readTriggerConfig(filename):
  208. """
  209. Returns a list of trigger objects
  210. that correspond to the rules set
  211. in the file filename
  212. """
  213.  
  214. # Here's some code that we give you
  215. # to read in the file and eliminate
  216. # blank lines and comments
  217. triggerfile = open(filename, "r")
  218. all = [ line.rstrip() for line in triggerfile.readlines() ]
  219. lines = []
  220. for line in all:
  221. if len(line) == 0 or line[0] == '#':
  222. continue
  223. lines.append(line)
  224.  
  225. triggers = []
  226. triggerMap = {}
  227.  
  228. # Be sure you understand this code - we've written it for you,
  229. # but it's code you should be able to write yourself
  230. for line in lines:
  231.  
  232. linesplit = line.split(" ")
  233.  
  234. # Making a new trigger
  235. if linesplit[0] != "ADD":
  236. trigger = makeTrigger(triggerMap, linesplit[1],
  237. linesplit[2:], linesplit[0])
  238.  
  239. # Add the triggers to the list
  240. else:
  241. for name in linesplit[1:]:
  242. triggers.append(triggerMap[name])
  243.  
  244. return triggers
  245.  
  246. import thread
  247.  
  248. SLEEPTIME = 60 #seconds -- how often we poll
  249.  
  250.  
  251. def main_thread(master):
  252. # A sample trigger list - you'll replace
  253. # this with something more configurable in Problem 11
  254. try:
  255. # These will probably generate a few hits...
  256. t1 = TitleTrigger("Obama")
  257. t2 = SubjectTrigger("Romney")
  258. t3 = PhraseTrigger("Election")
  259. t4 = OrTrigger(t2, t3)
  260. triggerlist = [t1, t4]
  261.  
  262. # TODO: Problem 11
  263. # After implementing makeTrigger, uncomment the line below:
  264. # triggerlist = readTriggerConfig("triggers.txt")
  265.  
  266. # **** from here down is about drawing ****
  267. frame = Frame(master)
  268. frame.pack(side=BOTTOM)
  269. scrollbar = Scrollbar(master)
  270. scrollbar.pack(side=RIGHT,fill=Y)
  271.  
  272. t = "Google & Yahoo Top News"
  273. title = StringVar()
  274. title.set(t)
  275. ttl = Label(master, textvariable=title, font=("Helvetica", 18))
  276. ttl.pack(side=TOP)
  277. cont = Text(master, font=("Helvetica",14), yscrollcommand=scrollbar.set)
  278. cont.pack(side=BOTTOM)
  279. cont.tag_config("title", justify='center')
  280. button = Button(frame, text="Exit", command=root.destroy)
  281. button.pack(side=BOTTOM)
  282.  
  283. # Gather stories
  284. guidShown = []
  285. def get_cont(newstory):
  286. if newstory.getGuid() not in guidShown:
  287. cont.insert(END, newstory.getTitle()+"\n", "title")
  288. cont.insert(END, "\n---------------------------------------------------------------\n", "title")
  289. cont.insert(END, newstory.getSummary())
  290. cont.insert(END, "\n*********************************************************************\n", "title")
  291. guidShown.append(newstory.getGuid())
  292.  
  293. while True:
  294.  
  295. print "Polling . . .",
  296. # Get stories from Google's Top Stories RSS news feed
  297. stories = process("http://news.google.com/?output=rss")
  298.  
  299. # Get stories from Yahoo's Top Stories RSS news feed
  300. stories.extend(process("http://rss.news.yahoo.com/rss/topstories"))
  301.  
  302. # Process the stories
  303. stories = filterStories(stories, triggerlist)
  304.  
  305. map(get_cont, stories)
  306. scrollbar.config(command=cont.yview)
  307.  
  308.  
  309. print "Sleeping..."
  310. time.sleep(SLEEPTIME)
  311.  
  312. except Exception as e:
  313. print e
  314.  
  315.  
  316. if __name__ == '__main__':
  317.  
  318. root = Tk()
  319. root.title("Some RSS parser")
  320. thread.start_new_thread(main_thread, (root,))
  321. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement