-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.py
35 lines (31 loc) · 1.13 KB
/
rss.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
import feedparser
import threading
from time import sleep
class RssPoller(threading.Thread):
def __init__(self, feed, callback, poll_time=120):
super(RssPoller, self).__init__()
self.daemon = True
self.feed_url = feed
self.callback = callback
self.poll_time = poll_time
self.start()
def run(self):
self.feed = feedparser.parse(self.feed_url)
self.id_set = set([e.id for e in self.feed.entries])
while True:
sleep(self.poll_time)
self.poll()
def poll(self):
print("Polling " + self.feed_url)
parse_kwargs = {}
if hasattr(self.feed, "modified"):
parse_kwargs["modified"] = self.feed.modified
if hasattr(self.feed, "etag"):
parse_kwargs["etag"] = self.feed.etag
self.feed = feedparser.parse(self.feed_url, **parse_kwargs)
if len(self.feed.entries) > 0:
for e in self.feed.entries:
if e.id not in self.id_set:
print("New feed item")
self.callback(e)
self.id_set.add(e.id)