Ameliorations
authorRaphael Velt <raph.velt@gmail.com>
Mon, 19 Dec 2011 11:22:55 +0100
changeset 438 892c3d9f635c
parent 437 375773ebcee4
child 440 aee91fdf9678
Ameliorations
tweetcast/server-gevent/tweetcast.py
web/res/js-tweetcast/live-polemic.js
web/sweet-tweet/style.css
--- a/tweetcast/server-gevent/tweetcast.py	Mon Dec 19 10:18:50 2011 +0100
+++ b/tweetcast/server-gevent/tweetcast.py	Mon Dec 19 11:22:55 2011 +0100
@@ -16,6 +16,8 @@
 Base = declarative_base()
 engine = create_engine(SQL_CONNECT)
 Session = sessionmaker(bind=engine)
+data = []
+lastid = 0L
 
 class TweetSource(Base):
     __tablename__ = 'tweet_tweet_source'
@@ -105,36 +107,41 @@
                     del tweetdict['retweeted_status']['user'][key]
             textids(tweetdict['retweeted_status'])
         return tweetdict
-        
+
+def refresh():
+    print "refreshing"
+    query = session.query(Tweet).order_by(asc(Tweet.id)).options(joinedload(Tweet.tweet_source)).filter(Tweet.id > lastid)
+    for tweet in query:
+        data.append(anyjson.serialize(tweet.jsondict()))
+    gevent.sleep(2.)
+    gevent.spawn(refresh)
+    
+def wsstart():
+    global WEB_PORT
+    WSGIServer(('', WEB_PORT), webserver).serve_forever()
 
 def webserver(env, start_response):
-	if env['PATH_INFO'] == '/':
-		httpquery = parse_qs(env['QUERY_STRING'])
-		print "serving tweets to", env['REMOTE_ADDR'], httpquery
-		query = session.query(Tweet).order_by(asc(Tweet.id)).options(joinedload(Tweet.tweet_source))
-		if "since_id" in httpquery:
-		    query = query.filter(Tweet.id >= long(httpquery["since_id"][0]))
-		if "after_id" in httpquery:
-		    query = query.filter(Tweet.id > long(httpquery["after_id"][0]))
-		if "max_id" in httpquery:
-		    query = query.filter(Tweet.id <= long(httpquery["max_id"][0]))
-		if "before_id" in httpquery:
-		    query = query.filter(Tweet.id < long(httpquery["before_id"][0]))
-		if "limit" in httpquery:
-			result = query[:int(httpquery["limit"][0])]
-		else:
-			result = query
-		start_response('200 OK', [('Content-Type', 'application/javascript' if "callback" in httpquery else 'application/json' )])
-		return ["%s%s%s"%(
-			"%s("%httpquery["callback"][0] if "callback" in httpquery else "",
-			anyjson.serialize({"tweets" : [t.jsondict() for t in result]}),
-			")" if "callback" in httpquery else ""
-		)]
-	else:
-		start_response('404 Not Found', [('Content-Type', 'text/html')])
-		return ['<h1>Not Found</h1>']
+    if env['PATH_INFO'] == '/':
+        httpquery = parse_qs(env['QUERY_STRING'])
+        print "serving tweets to", env['REMOTE_ADDR'], httpquery
+        frompos = 0
+        if "from" in httpquery:
+            frompos = int(httpquery["from"][0])
+        result = '%s{"tweets" : [ %s ] }%s'%(
+            "%s("%httpquery["callback"][0] if "callback" in httpquery else "",
+            ",".join(data[frompos:]),
+            ")" if "callback" in httpquery else ""
+        )
+        print "Sending response"
+        start_response('200 OK', [('Content-Type', 'application/javascript' if "callback" in httpquery else 'application/json' )])
+        return [result]
+    else:
+        start_response('404 Not Found', [('Content-Type', 'text/html')])
+        return ['<h1>Not Found</h1>']
 
 session = Session()
 
 if __name__ == "__main__":
-    WSGIServer(('', WEB_PORT), webserver).serve_forever()
\ No newline at end of file
+    gevent.spawn(refresh)
+    print "Starting Webserver"
+    wsstart()
\ No newline at end of file
--- a/web/res/js-tweetcast/live-polemic.js	Mon Dec 19 10:18:50 2011 +0100
+++ b/web/res/js-tweetcast/live-polemic.js	Mon Dec 19 11:22:55 2011 +0100
@@ -1070,10 +1070,10 @@
                 if (!twCx.tweets) {
                     return;
                 }
-                $.getJSON( source_address + '/?after_id=' + twCx.tweets[twCx.tweets.length - 1].id + '&callback=?', function(data) {
+                $.getJSON( source_address + '/?from=' + twCx.tweets.length + '&callback=?', function(data) {
                     loadTweets(data.tweets, true);
                 });
-            }, 1000);
+            }, 5000);
             break;
         
         case "standalone" :
--- a/web/sweet-tweet/style.css	Mon Dec 19 10:18:50 2011 +0100
+++ b/web/sweet-tweet/style.css	Mon Dec 19 11:22:55 2011 +0100
@@ -40,7 +40,7 @@
 }
 
 p.tweet_text {
-    color: #666;
+    color: #333;
 }
 
 #ptimg {