Skip to content

Instantly share code, notes, and snippets.

@bulkan
Created December 24, 2009 12:31
Show Gist options
  • Save bulkan/263167 to your computer and use it in GitHub Desktop.
Save bulkan/263167 to your computer and use it in GitHub Desktop.
import time
import os
import cherrypy
import jinja2
from filter_daemon import *
try:
import json
except:
import simplejson as json
from simplejson import JSONEncoder
encoder = JSONEncoder()
def jsonify_tool_callback(*args, **kwargs):
response = cherrypy.response
response.headers['Content-Type'] = 'application/json'
response.body = encoder.iterencode(response.body)
cherrypy.tools.jsonify = cherrypy.Tool('before_finalize', jsonify_tool_callback, priority=30)
root_path = os.path.dirname(__file__)
# jinja2 template renderer
env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.join(root_path, 'templates')))
def render_template(template,**context):
global env
template = env.get_template(template+'.jinja')
return template.render(context)
class Questions(object):
_cp_config = {
'tools.encode.on':True,
'tools.encode.encoding':'utf8',
}
fr = FilterRedis()
@cherrypy.expose()
def index(self):
tweets = self.fr.tweets(since=0)
return render_template('index', tweets=tweets)
@cherrypy.expose()
@cherrypy.tools.jsonify()
def latest(self, since, nt):
if not since:
since = 0
tweets = self.fr.tweets(limit=5, since=float(since))
return render_template('tweets', tweets=tweets)
if __name__ == '__main__':
#cherrypy.config.update({'server.socket_host': '0.0.0.0',
# 'server.socket_port': 8085})
cherrypy.quickstart(Questions())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment