def _on_api_response(self, response): if response is None: logging.error("API request for %s failed." % self.gplus_user_id) self.write("Unable to fetch content for this Google+ ID; it may not be authenticated. See http://%s for more information." % self.request.host) self.set_status(401) return self.finish() if response.error: if response.code == 403: logging.error("API Request 403: %r" % (json.loads(response.body))) self.set_status(503) self.write("Unable to fulfill request at this time - Google+ API rate limit exceeded.") return self.finish() else: logging.error("AsyncHTTPRequest error: %r, %r" % (response.error, response)) return self.send_error(500) else: data = json.loads(response.body) headers = {'Content-Type': 'application/atom+xml'} params = { 'userid': self.gplus_page_id or self.gplus_user_id, 'baseurl': 'http://%s' % self.request.host, 'requesturi': 'http://%s%s' % (self.request.host, self.request.uri.split('?', 1)[0]), } if 'items' not in data or not data['items']: params['lastupdate'] = dateutils.to_atom_format(datetime.datetime.today()) return self._respond(headers, empty_feed_template.format(**params)) posts = data['items'] lastupdate = max(dateutils.from_iso_format(p['updated']) for p in posts) params['author'] = xhtml_escape(posts[0]['actor']['displayName']) params['lastupdate'] = dateutils.to_atom_format(lastupdate) headers['Last-Modified'] = dateutils.to_http_format(lastupdate) params['entrycontent'] = u''.join(entry_template.format(**get_post_params(p)) for p in posts) body = feed_template.format(**params) Cache.set(self.cache_key, {'headers': headers, 'body': body}, time=Config.getint('cache', 'stream-expire')) return self._respond(headers, body)
def on_fetch_person_complete(cls, response, callback): """Callback for the people/me API call in fetch_person_by_token.""" person = json.loads(response.body) Cache.set(cls.profile_cache_key_template % person['id'], person, time=Config.getint('cache', 'profile-expire')) return IOLoop.instance().add_callback(lambda: callback(person))
def _on_api_response(self, response): if response is None: logging.error("API request for %s failed." % self.gplus_user_id) self.write( "Unable to fetch content for this Google+ ID; it may not be authenticated. See http://%s for more information." % self.request.host) self.set_status(401) return self.finish() if response.error: if response.code == 403: logging.error("API Request 403: %r" % (json.loads(response.body))) self.set_status(503) self.write( "Unable to fulfill request at this time - Google+ API rate limit exceeded." ) return self.finish() else: logging.error("AsyncHTTPRequest error: %r, %r" % (response.error, response)) return self.send_error(500) else: data = json.loads(response.body) headers = {'Content-Type': 'application/atom+xml'} params = { 'userid': self.gplus_page_id or self.gplus_user_id, 'baseurl': 'http://%s' % self.request.host, 'requesturi': 'http://%s%s' % (self.request.host, self.request.uri.split('?', 1)[0]), } if 'items' not in data or not data['items']: params['lastupdate'] = dateutils.to_atom_format( datetime.datetime.today()) return self._respond(headers, empty_feed_template.format(**params)) posts = data['items'] lastupdate = max( dateutils.from_iso_format(p['updated']) for p in posts) params['author'] = xhtml_escape(posts[0]['actor']['displayName']) params['lastupdate'] = dateutils.to_atom_format(lastupdate) headers['Last-Modified'] = dateutils.to_http_format(lastupdate) params['entrycontent'] = u''.join( entry_template.format(**get_post_params(p)) for p in posts) body = feed_template.format(**params) Cache.set(self.cache_key, { 'headers': headers, 'body': body }, time=Config.getint('cache', 'stream-expire')) return self._respond(headers, body)
application = tornado.web.Application( route.get_routes(), gzip = True, static_path = 'static', template_path = 'templates', ) if __name__ == '__main__': logging.basicConfig( level=getattr(logging, Config.get('system', 'log-level')), format="%(asctime)-15s [%(process)d|%(threadName)s] %(message)s", ) pid_path = _expand_path(Config.get('system', 'pid-path')) pid.check(pid_path) time.sleep(1) # Give a little time for ports to be recognized as unbound pid.write(pid_path) try: db.init(_expand_path(Config.get('database', 'path'))) http_server = HTTPServer(application, xheaders=True) http_server.bind(Config.getint('network', 'port')) http_server.start() IOLoop.instance().start() finally: pid.remove(pid_path)