在Google App Engine下生成RSS源

tag*_*aum 3 python rss google-app-engine

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

我试图使用通常的请求处理程序并生成xml响应.当我直接访问Feed网址时,我可以正确看到Feed,但是,当我尝试在Google阅读器中订阅Feed时,它会说

"无法找到所请求的Feed."

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

怎么解决这个?谢谢!

Nik*_*ntz 6

我建议有两种解决方案:

  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>
    
    Run Code Online (Sandbox Code Playgroud)

我的处理程序是(你也可以用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)
Run Code Online (Sandbox Code Playgroud)

我希望其中一些对你有用,两种方式都适合我.