tweetcast/server-gevent/tweetcast.py
changeset 438 892c3d9f635c
parent 425 b346fd32fc34
child 442 8ea085e5e7d1
equal deleted inserted replaced
437:375773ebcee4 438:892c3d9f635c
    14 from server_setup import SQL_CONNECT, WEB_PORT
    14 from server_setup import SQL_CONNECT, WEB_PORT
    15 
    15 
    16 Base = declarative_base()
    16 Base = declarative_base()
    17 engine = create_engine(SQL_CONNECT)
    17 engine = create_engine(SQL_CONNECT)
    18 Session = sessionmaker(bind=engine)
    18 Session = sessionmaker(bind=engine)
       
    19 data = []
       
    20 lastid = 0L
    19 
    21 
    20 class TweetSource(Base):
    22 class TweetSource(Base):
    21     __tablename__ = 'tweet_tweet_source'
    23     __tablename__ = 'tweet_tweet_source'
    22     id = Column(Integer, primary_key=True, autoincrement=True)
    24     id = Column(Integer, primary_key=True, autoincrement=True)
    23     original_json = Column(String)
    25     original_json = Column(String)
   103             for key in user_keys_to_delete:
   105             for key in user_keys_to_delete:
   104                 if key in tweetdict['retweeted_status']['user']:
   106                 if key in tweetdict['retweeted_status']['user']:
   105                     del tweetdict['retweeted_status']['user'][key]
   107                     del tweetdict['retweeted_status']['user'][key]
   106             textids(tweetdict['retweeted_status'])
   108             textids(tweetdict['retweeted_status'])
   107         return tweetdict
   109         return tweetdict
   108         
   110 
       
   111 def refresh():
       
   112     print "refreshing"
       
   113     query = session.query(Tweet).order_by(asc(Tweet.id)).options(joinedload(Tweet.tweet_source)).filter(Tweet.id > lastid)
       
   114     for tweet in query:
       
   115         data.append(anyjson.serialize(tweet.jsondict()))
       
   116     gevent.sleep(2.)
       
   117     gevent.spawn(refresh)
       
   118     
       
   119 def wsstart():
       
   120     global WEB_PORT
       
   121     WSGIServer(('', WEB_PORT), webserver).serve_forever()
   109 
   122 
   110 def webserver(env, start_response):
   123 def webserver(env, start_response):
   111 	if env['PATH_INFO'] == '/':
   124     if env['PATH_INFO'] == '/':
   112 		httpquery = parse_qs(env['QUERY_STRING'])
   125         httpquery = parse_qs(env['QUERY_STRING'])
   113 		print "serving tweets to", env['REMOTE_ADDR'], httpquery
   126         print "serving tweets to", env['REMOTE_ADDR'], httpquery
   114 		query = session.query(Tweet).order_by(asc(Tweet.id)).options(joinedload(Tweet.tweet_source))
   127         frompos = 0
   115 		if "since_id" in httpquery:
   128         if "from" in httpquery:
   116 		    query = query.filter(Tweet.id >= long(httpquery["since_id"][0]))
   129             frompos = int(httpquery["from"][0])
   117 		if "after_id" in httpquery:
   130         result = '%s{"tweets" : [ %s ] }%s'%(
   118 		    query = query.filter(Tweet.id > long(httpquery["after_id"][0]))
   131             "%s("%httpquery["callback"][0] if "callback" in httpquery else "",
   119 		if "max_id" in httpquery:
   132             ",".join(data[frompos:]),
   120 		    query = query.filter(Tweet.id <= long(httpquery["max_id"][0]))
   133             ")" if "callback" in httpquery else ""
   121 		if "before_id" in httpquery:
   134         )
   122 		    query = query.filter(Tweet.id < long(httpquery["before_id"][0]))
   135         print "Sending response"
   123 		if "limit" in httpquery:
   136         start_response('200 OK', [('Content-Type', 'application/javascript' if "callback" in httpquery else 'application/json' )])
   124 			result = query[:int(httpquery["limit"][0])]
   137         return [result]
   125 		else:
   138     else:
   126 			result = query
   139         start_response('404 Not Found', [('Content-Type', 'text/html')])
   127 		start_response('200 OK', [('Content-Type', 'application/javascript' if "callback" in httpquery else 'application/json' )])
   140         return ['<h1>Not Found</h1>']
   128 		return ["%s%s%s"%(
       
   129 			"%s("%httpquery["callback"][0] if "callback" in httpquery else "",
       
   130 			anyjson.serialize({"tweets" : [t.jsondict() for t in result]}),
       
   131 			")" if "callback" in httpquery else ""
       
   132 		)]
       
   133 	else:
       
   134 		start_response('404 Not Found', [('Content-Type', 'text/html')])
       
   135 		return ['<h1>Not Found</h1>']
       
   136 
   141 
   137 session = Session()
   142 session = Session()
   138 
   143 
   139 if __name__ == "__main__":
   144 if __name__ == "__main__":
   140     WSGIServer(('', WEB_PORT), webserver).serve_forever()
   145     gevent.spawn(refresh)
       
   146     print "Starting Webserver"
       
   147     wsstart()