1 from .models import Feed, Entry
6 def _rate_content(content):
7 if content.type == 'application/xhtml+xml':
9 elif content.type == 'text/html':
11 elif content.type == 'text/plain':
17 def _choose_content(contents):
18 limited_contents = [content for content in contents if content.type in ('application/xhtml+xml', 'text/html', 'text/plain')]
19 limited_contents.sort(key=_rate_content)
20 return limited_contents[0] if len(limited_contents) > 0 else None
23 def _parse_date(date):
25 return datetime.datetime(*(date[0:6]))
30 def _add_entry(feed, parsed_entry):
31 title = parsed_entry.get('title', 'Untitled')
32 link = parsed_entry.get('link', feed.link)
33 published = _parse_date(parsed_entry.get('published_parsed', parsed_entry.get('created_parsed', parsed_entry.get('updated_parsed', None))))
35 published = datetime.datetime.now()
36 updated = _parse_date(parsed_entry.get('updated_parsed', None))
39 contents = parsed_entry.get('content', None)
41 content = _choose_content(contents).value
44 summary = parsed_entry.get('summary', None)
46 if summary or content:
47 entry, created = feed.entries.get_or_create(uri=parsed_entry.id, defaults={
50 'published': published,
58 entry.published = published
59 entry.updated = updated
60 entry.summary = summary
61 entry.content = content
65 def _add_entries(feed, parsed):
66 for parsed_entry in parsed.entries:
67 _add_entry(feed, parsed_entry)
70 def refresh_feed(feed):
72 parsed = feedparser.parse(feed.url, etag=feed.etag, modified=(feed.modified.timetuple() if feed.modified else None))
73 if parsed.get('status', None) == 304:
75 if parsed.get('status', None) == 301 and parsed.has_key('href'):
76 feed.url = parsed.href
77 if parsed.get('status', None) == 410:
79 if parsed.has_key('etag'):
80 feed.etag = parsed.etag
81 if parsed.has_key('modified'):
82 feed.modified = datetime.datetime(*(parsed.modified[0:6]))
83 feed.title = parsed.feed.get('title', feed.url)
84 feed.updated = _parse_date(parsed.feed.get('updated_parsed', datetime.datetime.now().timetuple())[0:6])
85 feed.link = parsed.feed.get('link', feed.url)
88 _add_entries(feed, parsed)
91 def refresh_all_feeds():
92 for feed in Feed.objects.all():