news4 - RSS aggrigation system
Revision | 35706daeb11067ffac8035502714bda9c6752ef0 (tree) |
---|---|
Zeit | 2013-04-26 04:09:15 |
Autor | hylom <hylom@hylo...> |
Commiter | hylom |
fix: crash when RSS item is invalid
@@ -8,6 +8,12 @@ import dateutil.parser | ||
8 | 8 | from config import config as config |
9 | 9 | from logger import log |
10 | 10 | |
11 | +def _get_attr(obj, attr, default=""): | |
12 | + try: | |
13 | + return obj.__getattr__(attr) | |
14 | + except AttributeError: | |
15 | + return default | |
16 | + | |
11 | 17 | class FeedFetcher(object): |
12 | 18 | 'Feed fetching and parsing' |
13 | 19 | def __init__(self, feed): |
@@ -18,21 +24,26 @@ class FeedFetcher(object): | ||
18 | 24 | f = feedparser.parse(self._feed["source"]) |
19 | 25 | entries = [] |
20 | 26 | for e in f['entries']: |
21 | - try: | |
22 | - desc = e.description | |
23 | - except AttributeError: | |
24 | - desc = '' | |
27 | + | |
25 | 28 | entry = { |
26 | -# 'title': e.title.decode('utf8') if isinstance(e.title, str) else e.title, | |
27 | - 'title': e.title, | |
28 | - 'url': e.link, | |
29 | - 'body': desc, | |
30 | - 'date': dateutil.parser.parse(e.updated), | |
31 | 29 | 'feed': self._feed, |
32 | 30 | 'tags': [], |
33 | 31 | } |
32 | + entry["title"] = _get_attr(e, "title", "(no title)") | |
33 | + entry["url"] = _get_attr(e, "link") | |
34 | + entry["body"] = _get_attr(e, "description") | |
35 | + entry["date"] = _get_attr(e, "updated", None) | |
36 | + | |
37 | + if entry["date"] == None: | |
38 | + entry["date"] = _get_attr(e, "published", None) | |
39 | + | |
40 | + if entry["date"] == None: | |
41 | + continue | |
42 | + | |
43 | + entry["date"] = dateutil.parser.parse(entry["date"]) | |
34 | 44 | if entry['date'].tzinfo == None: |
35 | 45 | entry['date'] = entry['date'].replace(tzinfo=dateutil.tz.tzutc()) |
46 | + | |
36 | 47 | entries.append(entry) |
37 | 48 | return entries |
38 | 49 |