我试过使用常规请求处理程序并生成XML响应。当我直接访问订阅源网址时,可以正确地看到订阅源,但是当我尝试在Google Reader中订阅该订阅源时,它会说
“无法找到所请求的订阅源。”
我想知道这种方法是否正确。我考虑使用静态XML文件,并通过Cron作业更新它。但是,由于GAE不支持文件I/O,因此这种方法似乎行不通。
如何解决这个问题?谢谢!
我建议两种解决方案:
GAE-REST you can just add to your project and configure and it will make RSS for you but the project is old and no longer maintained.
Do like I do, use a template to write a list to and like this I could succeed generating RSS (GeoRSS) that can be read via google reader where template is:
<title>{{host}}</title>
<link href="http://{{host}}" rel="self"/>
<id>http://{{host}}/</id>
<updated>2011-09-17T08:14:49.875423Z</updated>
<generator uri="http://{{host}}/">{{host}}</generator>
{% for entity in entities %}
<entry>
<title><![CDATA[{{entity.title}}]]></title>
<link href="http://{{host}}/vi/{{entity.key.id}}"/>
<id>http://{{host}}/vi/{{entity.key.id}}</id>
<updated>{{entity.modified.isoformat}}Z</updated>
<author><name>{{entity.title|escape}}</name></author>
<georss:point>{{entity.geopt.lon|floatformat:2}},{{entity.geopt.lat|floatformat:2}}</georss:point>
<published>{{entity.added}}</published>
<summary type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml">{{entity.text|escape}}</div>
</summary>
</entry>
{% endfor %}
</feed>
我的处理程序是(您也可以使用Python 2.7作为仅函数的处理程序之外的更简单的解决方案):
class GeoRSS(webapp2.RequestHandler):
def get(self):
start = datetime.datetime.now() - timedelta(days=60)
count = (int(self.request.get('count'
)) if not self.request.get('count') == '' else 1000)
try:
entities = memcache.get('entities')
except KeyError:
entity = Entity.all().filter('modified >',
start).filter('published =',
True).order('-modified').fetch(count)
memcache.set('entities', entities)
template_values = {'entities': entities, 'request': self.request,
'host': os.environ.get('HTTP_HOST',
os.environ['SERVER_NAME'])}
dispatch = 'templates/georss.html'
path = os.path.join(os.path.dirname(__file__), dispatch)
output = template.render(path, template_values)
self.response.headers['Cache-Control'] = 'public,max-age=%s' \
% 86400
self.response.headers['Content-Type'] = 'application/rss+xml'
self.response.out.write(output)
生成XML与HTML没有什么特别之处-只要正确设置内容类型即可。将您的feed传递给验证器http://validator.w3.org/feed/,它会告诉您问题所在。
如果这不能解决问题,您需要向我们展示您的源代码-如果您不向我们展示代码,我们无法为您调试代码。
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
xml:lang="en"
xml:base="http://www.example.org">
<id>urn:uuid:4FC292A4-C69C-4126-A9E5-4C65B6566E05</id>
<title>Adam Crossland's Blog</title>
<subtitle>opinions and rants on software and...things</subtitle>
<updated>{{ updated }}</updated>
<author>
<name>Adam Crossland</name>
<email>adam@adamcrossland.net</email>
</author>
<link href="http://blog.adamcrossland.net/" />
<link rel="self" href="http://blog.adamcrossland.net/home/feed" />
{% for each_post in posts %}{{ each_post.to_atom|safe }}
{% endfor %}
</feed>
注意:如果您使用此代码,需要创建自己的 uuid 并放入 id 节点中。
更新后的节点应包含内容上次更新的时间和日期,格式为 rfc 3339。幸运的是,Python 有一个库可以帮您处理这个问题。下面是生成 feed 的控制器的一段摘录:
from rfc3339 import rfc3339
posts = Post.get_all_posts()
self.context['posts'] = posts
# Initially, we'll assume that there are no posts in the blog and provide
# an empty date.
self.context['updated'] = ""
if posts is not None and len(posts) > 0:
# But there are posts, so we will pick the most recent one to get a good
# value for updated.
self.context['updated'] = rfc3339(posts[0].updated(), utc=True)
response.content_type = "application/atom+xml"
不用担心 self.context['updated']
这个东西。那只是我的框架提供了一种设置模板变量的快捷方式。重要的是,我使用 rfc3339
函数对要使用的日期进行编码。此外,我将 Response 对象的 content_type
属性设置为 application/atom+xml
。
唯一缺失的部分是模板使用名为 to_atom
的方法将 Post
对象转换为格式化为 Atom 的数据:
def to_atom(self):
"Create an ATOM entry block to represent this Post."
from rfc3339 import rfc3339
url_for = self.url_for()
atom_out = "<entry>\n\t<title>%s</title>\n\t<link href=\"http://blog.adamcrossland.net/%s\" />\n\t<id>%s</id>\n\t<summary>%s</summary>\n\t<updated>%s</updated>\n </entry>" % (self.title, url_for, self.slug_text, self.summary_for(), rfc3339(self.updated(), utc=True))
return atom_out
据我所知,这就是所需的全部内容。这段代码可以为我的博客生成一个完美且正常工作的订阅源。现在,如果你真的想使用RSS而不是Atom,你需要改变订阅源模板、文章模板和内容类型,但我认为这就是从AppEngine/Python应用程序生成订阅源所需做的核心。