-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps5.py
More file actions
362 lines (288 loc) · 9.89 KB
/
ps5.py
File metadata and controls
362 lines (288 loc) · 9.89 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# 6.0001/6.00 Problem Set 5 - RSS Feed Filter
# Name:neelima-j
import feedparser
import string
import time
import threading
from project_util import translate_html
from mtTkinter import *
from datetime import datetime
import pytz
import re
import requests
#-----------------------------------------------------------------------
#======================
# Code for retrieving and parsing
# Google and Yahoo News feeds
# Do not change this code
#======================
def process(url):
"""
Fetches news items from the rss url and parses them.
Returns a list of NewsStory-s.
"""
feed_content = requests.get(url)
feed = feedparser.parse(feed_content.text)
# 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():
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
# TODO: PhraseTrigger
class PhraseTrigger(Trigger):
def __init__(self,phrase):
self.phrase = phrase.lower().strip()
self.phrase = "".join((char if char.isalpha() else "") for char in self.phrase)
def is_phrase_in(self,text):
regexString = self.phrase+'(?!s)(?!!)'
phraseRegEx = re.compile(regexString)
text = text.lower()
text = "".join((char if char.isalpha() else "") for char in text)
text = text+' '
repeatRegEx = re.compile(r"(.+?)\1+")
if (phraseRegEx.findall(text) and repeatRegEx.match(text) == None):
return True
else:
return False
# Problem 3
# TODO: TitleTrigger
class TitleTrigger(PhraseTrigger):
def evaluate(self,story):
if self.is_phrase_in(story.title):
return True
else:
return False
# Problem 4
# TODO: DescriptionTrigger
class DescriptionTrigger(PhraseTrigger):
def evaluate(self, story):
if self.is_phrase_in(story.description):
return True
else:
return False
# TIME TRIGGERS
# Problem 5
# Constructor:
# Input: Time has to be in EST and in the format of "%d %b %Y %H:%M:%S".
# Convert time from string to a datetime before saving it as an attribute.
class TimeTrigger(Trigger):
def __init__(self,time):
self.time = datetime.strptime(time,r'%d %b %Y %H:%M:%S')
'''
est = pytz.timezone('US/Eastern')
self.time = est.localize(self.time)
'''
# Problem 6
class BeforeTrigger(TimeTrigger):
def evaluate(self,story):
'''
Fires when a story is published strictly before the trigger’s time
'''
story.pubdate = story.pubdate.replace(tzinfo = None)
'''I have no idea why it didnt work when everythin was in EST. So i removed TZinfo from everywhere
if story.pubdate.tzinfo == None:
est = pytz.timezone('US/Eastern')
story.pubdate = est.localize(story.pubdate)
'''
if story.pubdate < self.time:
return True
else:
return False
class AfterTrigger(TimeTrigger):
def evaluate(self,story):
'''
fires when a story is published strictly after the trigger’s time
'''
story.pubdate = story.pubdate.replace(tzinfo = None)
'''
if story.pubdate.tzinfo == None:
est = pytz.timezone('US/Eastern')
story.pubdate = est.localize(story.pubdate)
'''
if story.pubdate > self.time:
return True
else:
return False
# COMPOSITE TRIGGERS
# Problem 7
class NotTrigger(Trigger):
def __init__(self,T):
self.T = T
def evaluate(self,story):
return not self.T.evaluate(story)
# Problem 8
class AndTrigger(Trigger):
def __init__(self,t1,t2):
self.t1 = t1
self.t2 = t2
def evaluate(self,story):
return self.t1.evaluate(story) and self.t2.evaluate(story)
# Problem 9
class OrTrigger(Trigger):
def __init__(self,t1,t2):
self.t1 = t1
self.t2 = t2
def evaluate(self,story):
return self.t1.evaluate(story) or self.t2.evaluate(story)
#======================
# Filtering
#======================
# Problem 10
def filter_stories(stories, tlist):
"""
Takes in a list of NewsStory instances.
Returns: a list of only the stories for which a trigger in triggerlist fires.
"""
i=0
ret_stories = []
for story in stories:
for trigger in tlist:
if trigger.evaluate(story):
ret_stories.append(story)
break
return ret_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.
"""
trigger_file = open(filename, 'r')
lines = []
triggerlist = []
ret = []
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
i = 0
for line in lines:
t,tname,*arg = line.split(',')
if t != 'ADD':
tname = tname.capitalize()+'Trigger'
tobject = globals()[tname]
if len(arg)==1:
triggerlist.append(tobject(arg[0]))
else:
if tname == 'AND' or 'OR':
triggerlist.append(tobject(triggerlist[int(arg[0][1])-1],triggerlist[int(arg[1][1])-1]))
else:
triggerlist.append(tobject(arg[0],arg[1]))
elif t == 'ADD':
ret.append(triggerlist[int(tname[1])-1])
ret.append(triggerlist[int(arg[0][1])-1])
return ret
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("The")
t2 = DescriptionTrigger("Covid")
t3 = DescriptionTrigger("India")
t4 = AndTrigger(t2, t3)
triggerlist = [t1, t4]
'''
# Problem 11
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 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()