gpt4 book ai didi

python - 在 Google App Engine 下生成 RSS 提要

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:52 25 4
gpt4 key购买 nike

我想在 google app engine/python 下提供 rss feed。

我尝试使用通常的请求处理程序并生成 xml 响应。当我直接访问提要 url 时,我可以正确看到提要,但是,当我尝试在谷歌阅读器中订阅提要时,它说

'The feed being requested cannot be found.'

不知这种做法是否正确。我正在考虑使用静态 xml 文件并通过 cron 作业更新它。但是,虽然 GAE 不支持文件 i/o,但这种方法似乎行不通。

如何解决?谢谢!

最佳答案

我建议有 2 个解决方案:

  1. GAE-REST您可以只添加到您的项目并进行配置,它会为您生成 RSS,但该项目已经过时且不再维护。

  2. 像我一样,使用模板编写列表,这样我可以成功生成 RSS (GeoRSS),可以通过模板所在的谷歌阅读器阅读:

    <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)

我希望其中一些对您有用,这两种方式都对我有用。

关于python - 在 Google App Engine 下生成 RSS 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8507301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com