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

标签 python google-app-engine rss

我想在 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/

相关文章:

python - 如何在不删除所有数据的情况下将列表转换为数据框?

python - 如何使用 pandas 模块将带有标题的表格导入数据框

python - 在 Windows 上使用 Apache/mod_wsgi 从 virtualenv 运行 Python

java - 如何取消 Google App Engine 插件的身份验证?

java - 配置 appengine-web.xml

java - 如何正确利用 doinBackground() 方法来检索 RSS 项目

python - Pandas 空数据框

python - 为什么这个指令不起作用?

javascript - 从 Google API RSS Feed 加载更多内容

php - 如何使用 PHP+mySQL 从 XML 中获取所有节点?