check author node before assigned

This commit is contained in:
hillerliao
2022-05-15 21:29:22 +08:00
parent e6f43b2ae3
commit 32ede981ed
2 changed files with 9 additions and 10 deletions

View File

@@ -10,25 +10,23 @@ def parse(post):
item['description'] = post.summary item['description'] = post.summary
item['pubDate'] = post.published item['pubDate'] = post.published
item['link'] = post.link item['link'] = post.link
item['author'] = post.author if post.has_key('author') else ''
return item return item
def ctx(feed_url=''): def ctx(feed_url=''):
# tree = fetch(feed_url,headers=DEFAULT_HEADERS) res = requests.get(feed_url,headers=DEFAULT_HEADERS)
# title = tree.css('channel').css('title::text').get() feed = feedparser.parse(res.text)
# description = tree.css('channel').css('description').get()
# posts = tree.css('item')
feed = feedparser.parse(feed_url)
title = feed.feed.title title = feed.feed.title
description = feed.feed.subtitle description = feed.feed.subtitle
author = feed.feed.author if feed.feed.has_key('author') \
else feed.feed.generator if feed.feed.has_key('generator') \
else title
posts = feed.entries posts = feed.entries
return { return {
'title': title, 'title': title,
'link': feed_url, 'link': feed_url,
'description': description, 'description': description,
'author': feed.feed.author, 'author': author,
'items': list(map(parse, posts)) 'items': list(map(parse, posts))
} }

View File

@@ -14,6 +14,7 @@
<entry> <entry>
<id>{{item.link}}</id> <id>{{item.link}}</id>
<title><![CDATA[{{item.title|safe}}]]></title> <title><![CDATA[{{item.title|safe}}]]></title>
<author><![CDATA[{{item.author|safe}}]]></author>
<published>{{item.pubDate|default(now)}}</published> <published>{{item.pubDate|default(now)}}</published>
<updated>{{item.pubDate|default(now)}}</updated> <updated>{{item.pubDate|default(now)}}</updated>
<link href="{{item.link}}"/> <link href="{{item.link}}"/>