RSS: Add support for ETag and Last-Modified.

This commit is contained in:
Valentin Lorentz 2014-08-01 01:12:19 +02:00
parent db50e65b86
commit f3b31e5a4b

View File

@ -63,6 +63,7 @@ announced_headlines_filename = \
class Feed:
__slots__ = ('url', 'name', 'data', 'last_update', 'entries',
'etag', 'modified',
'lock', 'announced_entries')
def __init__(self, name, url, plugin_is_loading=False, announced=None):
assert name, name
@ -76,6 +77,8 @@ class Feed:
# loaded (the bot could be starting, and thus already busy)
self.last_update = time.time() if plugin_is_loading else 0
self.entries = []
self.etag = None
self.modified = None
self.lock = threading.Lock()
self.announced_entries = announced or \
utils.structures.TruncatableSet()
@ -240,7 +243,13 @@ class RSS(callbacks.Plugin):
def update_feed(self, feed):
with feed.lock:
d = feedparser.parse(feed.url)
d = feedparser.parse(feed.url, etag=feed.etag,
modified=feed.modified)
if 'status' not in d or d.status != 304: # Not modified
if 'etag' in d:
feed.etag = d.etag
if 'modified' in d:
feed.modified = d.modified
feed.data = d.feed
feed.entries = d.entries
feed.last_update = time.time()