2011-12-14 58 views
2

我想在Google應用程序引擎/ python下提供rss訂閱源。在Google App Engine下生成RSS訂閱源

我試過使用通常的請求處理程序並生成xml響應。當我直接訪問供稿網址時,我可以正確看到供稿,但是,當我嘗試訂閱Google閱讀器中的供稿時,它說

'找不到要求的供稿。

我想知道這種方法是否正確。我正在考慮使用靜態xml文件並通過cron作業進行更新。但是,雖然GAE不支持文件I/O,但這種方法似乎不起作用。

如何解決這個問題?謝謝!

+1

我的猜測是,您需要在響應標題中設置正確的內容類型,以便瀏覽器將其識別爲rss提要。但我現在懶得看看內容類型是什麼給你一個正式的答案。 –

回答

6

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

我希望這對你有一些作用,兩種方式都適合我。

+1

謝謝!設置內容類型的作品! – tagtraum

2

我的博客上有一個Atom feed生成器,它運行在AppEngine/Python上。我使用Django 1.2模板引擎來構建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>[email protected]</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節點。

更新後的節點應該包含上次更新Feed的內容的時間和日期,格式爲rfc 3339。幸運的是,Python有一個庫可以幫你處理這個問題。從生成提要控制器的摘錄:

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,則需要更改Feed模板,Post模板和content_type的格式,但我認爲這是獲取Feed所需做的本質從AppEngine/Python應用程序生成。

+0

謝謝!這非常有幫助! – tagtraum

2

與HTML相比,生成XML沒什麼特別 - 只要您正確設置內容類型即可。通過http://validator.w3.org/feed/將驗證信息傳遞給驗證器,它會告訴你它有什麼問題。

如果這沒有幫助,您需要向我們顯示您的源代碼 - 如果您不顯示給我們,我們無法爲您調試代碼。

+0

謝謝!設置內容類型的作品! – tagtraum