2011-11-19 91 views
0

我是GAE的新手,並且正在努力研究如何基於存儲在服務上的數據託管一個RSS訂閱源。 到目前爲止,我無法通過閱讀文檔來找到明顯的方法。 任何人都可以給我一些指針,我應該使用什麼API?主持一個動態的RSS訂閱

回答

1

爲什麼你需要一個API?您可以使用常規查詢類查詢更新的數據,並使用標準Python XML工具(如ElementTree)輸出該提要。

0

得到一個快速的解決方案,最簡單的方法是gae-rest,但它是作爲老在2008年你會發現我的解決方案有幫助的呈現我的數據到的GeoRSS模板,所以這裏是我的解決方案:

<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"> 

<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 ad in ads %} 

<entry> 

<title><![CDATA[{{ad.title}}]]></title> 
<link href="http://{{host}}/vi/{{ad.key.id}}"/> 
<id>http://{{host}}/vi/{{ad.key.id}}</id> 
<updated>{{ad.modified.isoformat}}Z</updated> 
<author><name>{{ad.title|escape}}</name></author> 
<georss:point>{{ad.geopt.lon|floatformat:2}},{{ad.geopt.lat|floatformat:2}}</georss:point> 
<published>{{ad.added}}</published> 
<summary type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml">{{ad.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: 
     ads = memcache.get('ads') 
    except KeyError: 
     ads = Ad.all().filter('modified >', 
          start).filter('published =', 
       True).order('-modified').fetch(count) 
    memcache.set('ads', ads) 
     template_values = {'ads': ads, '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

這與提供動態HTML頁面沒有任何區別。完成與提供HTML模板完全相同的方式,但生成RSS或Atom而不是HTML。

+0

我明白了!出於某種原因,我認爲你只能使用它的網頁,除非你使用了一些特殊的API。 – BenBtg

+0

@BenBtg不,App Engine不關心你提供的MIME類型,或者響應的組成部分。 –