-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.py
More file actions
36 lines (32 loc) · 1.11 KB
/
rss.py
File metadata and controls
36 lines (32 loc) · 1.11 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
import requests
from lxml import etree
import time
import dateutil.parser
import rsstastic
class RssReader() :
def __init__(self,config) :
self.url = config
def get_items(self) :
response = requests.get(self.url)
tree = etree.fromstring(response.content)
root = tree.find('channel')
items = {}
for item in root.findall('item') :
guid = item.find('guid').text
summary = item.find('{http://purl.org/rss/1.0/modules/content/}encoded')
if summary is None :
summary = item.find('description').text
else :
summary = summary.text
pubDate = item.find('pubDate')
updated = int(time.time())
if pubDate is not None:
updated = int(dateutil.parser.parse(item.find('pubDate').text).timestamp())
items[guid] = (updated,summary)
return items
def retrieve(self,key,item) :
return item[1]
rsstastic.readermap['rss'] = RssReader
if __name__ == "__main__" :
x = RssReader("http://xkcd.com/rss.xml")
print(x.get_items())