# -*- coding: utf-8 -*- from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext import webapp from google.appengine.api import urlfetch from appengine_utilities import sessions from appengine_utilities import cache import logging, cgi, datetime import templates import feedparser class GetFeed(webapp.RequestHandler): def get(self): self.cache = cache.Cache() finaloutput = None try: finaloutput = self.cache['javascriptoutput'] logging.debug("Javascript output cache hit") except: logging.debug("Javascript output cache miss") finaloutput = "" feed_url = "http://www.centrical.com/feeds/posts/default?alt=rss" try: feedinput = self.cache['feedinput'] logging.debug("Feed input cache hit") except: logging.debug("Feed input cache miss") try: feedinput = urlfetch.fetch(feed_url) self.cache['feedinput'] = feedinput except: raise Exception("Can not retrieve feedinput URL.") d = feedparser.parse(feedinput.content) if d.bozo == 1: raise Exception("Can not parse given URL.") contents = "" for entry in d.entries[:5]: contents += templates.contents % dict(link=cgi.escape(entry.link), title=cgi.escape(entry.title)) finaloutput += templates.read_html % dict(feed_url=cgi.escape(feed_url), contents=contents) self.cache['javascript'] = finaloutput expires_date = datetime.datetime.utcnow() + datetime.timedelta(1) self.response.headers["Content-Type"] = 'application/x-javascript' self.response.headers.add_header("Expires", expires_date.strftime("%d %b %Y %H:%M:%S GMT")) self.response.out.write(self.cache['javascript']) application = webapp.WSGIApplication([('/', GetFeed)], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main()