Python 在googleappengine下生成RSS提要

Python 在googleappengine下生成RSS提要,python,google-app-engine,rss,Python,Google App Engine,Rss,我想在googleappengine/python下提供rss提要 我尝试使用常用的请求处理程序并生成xml响应。当我直接访问提要url时,我可以正确地看到提要,然而,当我试图在google reader中订阅提要时,它说 '找不到请求的源。' 我不知道这种做法是否正确。我正在考虑使用一个静态xml文件,并由cron jobs对其进行更新。但是,虽然GAE不支持文件i/o,但这种方法似乎不起作用 如何解决这个问题?谢谢 我的博客有一个Atom提要生成器,它运行在AppEngine/Python上

我想在googleappengine/python下提供rss提要

我尝试使用常用的请求处理程序并生成xml响应。当我直接访问提要url时,我可以正确地看到提要,然而,当我试图在google reader中订阅提要时,它说

'找不到请求的源。'

我不知道这种做法是否正确。我正在考虑使用一个静态xml文件,并由cron jobs对其进行更新。但是,虽然GAE不支持文件i/o,但这种方法似乎不起作用


如何解决这个问题?谢谢

我的博客有一个Atom提要生成器,它运行在AppEngine/Python上。我使用Django 1.2模板引擎构建提要。我的模板如下所示:

<?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>
不要担心self.context['updated']之类的东西。这就是我的框架为设置模板变量提供快捷方式的方式。导入部分是,我对我想要使用
rfc339
函数的日期进行编码。另外,我将响应对象的
content\u type
属性设置为
application/atom+xml

唯一缺少的另一点是模板使用名为
的方法将
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
def to_atom(self):
“创建表示此帖子的ATOM条目块。”
从rfc3339导入rfc3339
url\u for=self.url\u for()
atom\u out=“\n\t%s\n\t\n\t%s\n\t%s\n\t%s\n”%(self.title,url\u for,self.slug\u text,self.summary\u for(),rfc3339(self.updated(),utc=True))
返回原子

据我所知,这就是所需的全部内容,这段代码确实为我的博客生成了一个非常好的工作提要。现在,如果您真的想使用RSS而不是Atom,您需要更改提要模板、帖子模板和内容类型的格式,但我认为这是从AppEngine/Python应用程序生成提要所需做的基本工作。

我建议有两种解决方案:

  • 您可以只添加到您的项目并进行配置,它将为您制作RSS,但该项目很旧,不再维护

  • 像我一样,使用模板编写列表,这样我就可以成功生成RSS(Georgs),可以通过google reader读取,其中模板是:

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


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

    与HTML相比,生成XML没有什么特别之处——只要您正确设置了内容类型。将feed传递给位于的验证器,它将告诉您它有什么问题


    如果没有帮助,您需要向我们展示您的源代码-如果您不向我们展示,我们无法为您调试代码。

    我猜您可能需要在响应标题中设置正确的内容类型,以便浏览器将其识别为rss提要。但是我现在太懒了,无法查看内容类型来给你一个正式的答案。谢谢!这很有帮助!
    <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>
    
    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)