diff -r 526d3e411736 -r 13702105c5ee tweetcast/server/tweetcast-gevent.py --- a/tweetcast/server/tweetcast-gevent.py Mon Oct 10 15:24:28 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,99 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -from gevent import monkey; monkey.patch_all() -# Importer d'abord, sinon exception -import anyjson, gevent, psycopg2 -from sqlalchemy import (Boolean, Column, BigInteger, Integer, String, - ForeignKey, DateTime, create_engine, func) -from sqlalchemy.orm import relationship, sessionmaker -from sqlalchemy.ext.declarative import declarative_base -from gevent.pywsgi import WSGIServer -from urlparse import parse_qs - -Base = declarative_base() -engine = create_engine('postgresql://postgres:doiteshimashite@localhost/tweet_live') -Session = sessionmaker(bind=engine) - -class Tweet(Base): - __tablename__ = 'tweet_tweet' - - id = Column(BigInteger, primary_key=True, autoincrement=False) - created_at = Column(DateTime) - text = Column(String) - user_id = Column(Integer, ForeignKey('tweet_user.id')) - user = relationship("User", backref="tweets") - - def jsondict(self): - return { - "id" : str(self.id), - "created_at" : str(self.created_at), - "text" : self.text, - "user" : self.user.jsondict() - } - - def __repr__(self): - return anyjson.serialize(self.jsondict()) - -class User(Base): - __tablename__ = "tweet_user" - - id = Column(BigInteger, primary_key=True, autoincrement=False) - screen_name = Column(String, index=True) - profile_image_url = Column(String) - - def jsondict(self): - return { - "id" : str(self.id), - "screen_name" : self.screen_name, - "profile_image_url" : self.profile_image_url - } - - def __repr__(self): - return anyjson.serialize(self.jsondict()) - -class TweetCast(object): - - def __init__(self): - self.session = Session() - a = self.session.query(func.max(Tweet.id)) - self.lastid = long(a[0][0]) - print self.lastid - self.tweets = [] - self.clients = [] - self.clientpos = [] - print "__init__" - gevent.spawn_later(1,self.getLastTweets) - - def getLastTweets(self): - result = self.session.query(Tweet).filter(Tweet.id > self.lastid).all() - if result: - self.lastid = result[len(result)-1].id - for tweet in result: - self.tweets.append(tweet) - print "getLastTweets - LastId = %d"%self.lastid - gevent.spawn_later(1,self.getLastTweets) - - def webserver(self, env, start_response): - print env - if env['PATH_INFO'] == '/': - query = parse_qs(env['QUERY_STRING']) - start_response('200 OK', [('Content-Type', 'text/html')]) - cookie = env['HTTP_COOKIE'] - if cookie not in self.clients: - self.clients.append(cookie) - self.clientpos.append(0) - clientid = self.clients.index(cookie) - pos = self.clientpos[clientid] - self.clientpos[clientid] = len(self.tweets) - return ["%s[%s]%s"%( - "%s("%query["callback"][0] if "callback" in query else "", - ",".join([str(tweet) for tweet in self.tweets[pos:]]), - ")" if "callback" in query else "" - )] - else: - start_response('404 Not Found', [('Content-Type', 'text/html')]) - return ['