-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps5.py
279 lines (236 loc) · 8.79 KB
/
ps5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#Unfinished, will complete in far off future.
import feedparser
import string
import time
import threading
from project_util import translate_html
from mtTkinter import *
from datetime import datetime
import pytz
# Code for retrieving and parsing Google and Yahoo News feeds
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
description = translate_html(entry.description)
pubdate = translate_html(entry.published)
try:
pubdate = datetime.strptime(pubdate, "%a, %d %b %Y %H:%M:%S %Z")
pubdate.replace(tzinfo=pytz.timezone("GMT"))
# pubdate = pubdate.astimezone(pytz.timezone('EST'))
# pubdate.replace(tzinfo=None)
except ValueError:
pubdate = datetime.strptime(pubdate, "%a, %d %b %Y %H:%M:%S %z")
newsStory = NewsStory(guid, title, description, link, pubdate)
ret.append(newsStory)
return ret
#======================
# Data structure design
#======================
# Problem 1
# TODO: NewsStory
class NewsStory(object):
def __init__(self, guid, title, description, link, pubdate):
self.guid = guid
self.title = title
self.description = description
self.link = link
self.pubdate = pubdate
def get_guid(self):
return self.guid
def get_title(self):
return self.title
def get_description(self):
return self.description
def get_link(self):
return self.link
def get_pubdate(self):
return self.pubdate
#======================
# Triggers
#======================
class Trigger(object):
def evaluate(self, story):
"""
Returns True if an alert should be generated
for the given news item, or False otherwise.
"""
# DO NOT CHANGE THIS!
raise NotImplementedError
# PHRASE TRIGGERS
# Problem 2
class PhraseTrigger(Trigger):
def __init__(self, phrase):
self.phrase = phrase
def is_phrase_in(self, text):
new_text_list = []
while " " in text: #If there's duplicate blanks, it gets rid of them
text = text.replace(" ", " ")
text = list(text)
for characters in text: #Gets rid of punctuation
if characters not in string.punctuation:
new_text_list.append(characters)
new_text_string = ''.join(new_text_list)
if self.phrase in new_text_string.lower():
return True
else:
return False
class TitleTrigger(PhraseTrigger):
def evaluate(self, story):
return self.is_phrase_in(story.get_title())
class DescriptionTrigger(PhraseTrigger):
def evaluate(self, story):
return self.is_phrase_in(story.get_description())
class TimeTrigger(Trigger): #Takes in date and time in format "DD MMM YYYY HH:MM:SS" E.g 03 Oct 2006 17:00:00
def __init__(self, time_str):
format_data = "%d %b %Y %H:%M:%S" #This is how you format the data for the datetime module
time = datetime.strptime(time_str, format_data).replace(tzinfo = pytz.timezone("EST")) #Strptime converts the time input into proper format. The .replace will change it to the timezone while also using the pytz module.
self.time = time
class BeforeTrigger(TimeTrigger): #Returns true if publication date was before the timestamp of the trigger
def __init__(self, time_str):
TimeTrigger.__init__(self, time_str)
def evaluate(self, time_str):
pub_date = self.pubdate
if self.time > datetime.strptime(time_str, "%d %b %Y %H:%M:%S").replace(tzinfo = pytz.timezone("EST")):
return True
class AfterTrigger(TimeTrigger):
def __init__(self, time_str):
TimeTrigger.__init__(self, time_str)
def evaluate(self, time_str):
pub_date = self.pubdate
if self.time < datetime.strptime(time_str, "%d %b %Y %H:%M:%S").replace(tzinfo = pytz.timezone("EST")):
return True
class NotTrigger(Trigger):
def __init__(self, trigger):
self.trigger = trigger
def evaluate(self, story):
return not self.trigger.evaluate(story)
class AndTrigger(Trigger):
def __init__(self, trigger1, trigger2):
self.trigger1 = trigger1
self.trigger2 = trigger2
def evaluate(self, story):
if self.trigger1.evaluate(story) == True and self.trigger2.evaluate(story) == True: #Should this be self.story or just story?
return True
class OrTrigger(Trigger):
def __init__(self, trigger1, trigger2):
self.trigger1 = trigger1
self.trigger2 = trigger2
def evaluate(self, story):
if self.trigger1.evaluate(story) == True or self.trigger2.evaluate(story) == True: #Should this be self.story or just story?
return True
def filter_stories(stories, triggerlist): #Returns only stories that have a trigger fired
triggered_stories = []
for story in stories:
for trigger in triggerlist:
if trigger.evaluate(story):
triggered_stories.append(story)
return triggered_stories
#
# #======================
# # User-Specified Triggers
# #======================
# # Problem 11
# def read_trigger_config(filename):
# """
# filename: the name of a trigger configuration file
#
# Returns: a list of trigger objects specified by the trigger configuration
# file.
# """
# # We give you the code to read in the file and eliminate blank lines and
# # comments. You don't need to know how it works for now!
# trigger_file = open(filename, 'r')
# lines = []
# for line in trigger_file:
# line = line.rstrip()
# if not (len(line) == 0 or line.startswith('//')):
# lines.append(line)
#
# # TODO: Problem 11
# # line is the list of lines that you need to parse and for which you need
# # to build triggers
#
# print(lines) # for now, print it so you see what it contains!
#
#
#
# SLEEPTIME = 120 #seconds -- how often we poll
#
# def main_thread(master):
# # A sample trigger list - you might need to change the phrases to correspond
# # to what is currently in the news
# try:
# t1 = TitleTrigger("election")
# t2 = DescriptionTrigger("Trump")
# t3 = DescriptionTrigger("Clinton")
# t4 = AndTrigger(t2, t3)
# triggerlist = [t1, t4]
#
# # Problem 11
# # TODO: After implementing read_trigger_config, uncomment this line
# # triggerlist = read_trigger_config('triggers.txt')
#
# # HELPER CODE - you don't need to understand this!
# # Draws the popup window that displays the filtered stories
# # Retrieves and filters the stories from the RSS feeds
# frame = Frame(master)
# frame.pack(side=BOTTOM)
# scrollbar = Scrollbar(master)
# scrollbar.pack(side=RIGHT,fill=Y)
#
# t = "Google & Yahoo Top News"
# title = StringVar()
# title.set(t)
# ttl = Label(master, textvariable=title, font=("Helvetica", 18))
# ttl.pack(side=TOP)
# cont = Text(master, font=("Helvetica",14), yscrollcommand=scrollbar.set)
# cont.pack(side=BOTTOM)
# cont.tag_config("title", justify='center')
# button = Button(frame, text="Exit", command=root.destroy)
# button.pack(side=BOTTOM)
# guidShown = []
# def get_cont(newstory):
# if newstory.get_guid() not in guidShown:
# cont.insert(END, newstory.get_title()+"\n", "title")
# cont.insert(END, "\n---------------------------------------------------------------\n", "title")
# cont.insert(END, newstory.get_description())
# cont.insert(END, "\n*********************************************************************\n", "title")
# guidShown.append(newstory.get_guid())
#
# while True:
#
# print("Polling . . .", end=' ')
# # Get stories from Google's Top Stories RSS news feed
# stories = process("http://news.google.com/news?output=rss")
#
# # Get stories from Yahoo's Top Stories RSS news feed
# stories.extend(process("http://news.yahoo.com/rss/topstories"))
#
# stories = filter_stories(stories, triggerlist)
#
# list(map(get_cont, stories))
# scrollbar.config(command=cont.yview)
#
#
# print("Sleeping...")
# time.sleep(SLEEPTIME)
#
# except Exception as e:
# print(e)
#
#
# if __name__ == '__main__':
# root = Tk()
# root.title("Some RSS parser")
# t = threading.Thread(target=main_thread, args=(root,))
# t.start()
# root.mainloop()