Added alternate configuration
authorRaphael Velt <raph.velt@gmail.com>
Wed, 26 Oct 2011 18:29:46 +0200
changeset 338 60dff8a71024
parent 337 54aea4996241
child 339 6a073c4a8578
Added alternate configuration
tweetcast/nodejs/client/css/style.css
tweetcast/nodejs/client/index.html
tweetcast/nodejs/conf-pol.js
tweetcast/nodejs/node-direct.js
--- a/tweetcast/nodejs/client/css/style.css	Tue Oct 25 18:27:26 2011 +0200
+++ b/tweetcast/nodejs/client/css/style.css	Wed Oct 26 18:29:46 2011 +0200
@@ -27,8 +27,12 @@
 
 /* Liste de Tweets */
 
+#container {
+    position: absolute; left: 50%; top: 50%; margin-left: -236px; margin-top: -300px;
+}
+
 #tweetlist {
-	position: absolute; left: 50px; top: 50px; width: 250px; height: 600px; list-style: none; border: 1px solid #999;
+	position: absolute; left: 0; top: 0; width: 250px; height: 600px; list-style: none; border: 1px solid #999;
 }
 
 li.tweet, li.placeholder {
@@ -161,7 +165,7 @@
 /* timeline */
 
 #timeline, #timewindow {
-    position: absolute; top: 50px; left: 301px; height: 600px; width: 220px;  border: 1px solid #999;
+    position: absolute; top: 0; left: 251px; height: 600px; width: 220px;  border: 1px solid #999;
 }
 
 #timeline {
--- a/tweetcast/nodejs/client/index.html	Tue Oct 25 18:27:26 2011 +0200
+++ b/tweetcast/nodejs/client/index.html	Wed Oct 26 18:29:46 2011 +0200
@@ -1,20 +1,22 @@
 <!doctype html>
 <html>
- <head>
-  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-  <title>Polemic Tweet “TweetCast”</title>
-  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
-  <script type="text/javascript" src="/conf.js"></script>
-  <script type="text/javascript" src="/socket.io/socket.io.js"></script>
-  <script type="text/javascript" src="js/Raphael-min.js"></script>
-  <script type="text/javascript" src="js/jquery.mousewheel.min.js"></script>
-  <script type="text/javascript" src="js/script.js"></script>
-  <link rel="stylesheet" href="css/style.css" type="text/css" />
- </head>
- <body>
-    <ul id="tweetlist">
-    </ul>
-    <div id="timeline"></div>
-    <div id="timewindow"></div>
- </body>
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+        <title>Polemic Tweet “TweetCast”</title>
+        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
+        <script type="text/javascript" src="/config"></script>
+        <script type="text/javascript" src="/socket.io/socket.io.js"></script>
+        <script type="text/javascript" src="js/raphael-min.js"></script>
+        <script type="text/javascript" src="js/jquery.mousewheel.min.js"></script>
+        <script type="text/javascript" src="js/script.js"></script>
+        <link rel="stylesheet" href="css/style.css" type="text/css" />
+    </head>
+    <body>
+        <div id="container">
+            <ul id="tweetlist">
+            </ul>
+            <div id="timeline"></div>
+            <div id="timewindow"></div>
+        </div>
+    </body>
 </html>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tweetcast/nodejs/conf-pol.js	Wed Oct 26 18:29:46 2011 +0200
@@ -0,0 +1,48 @@
+/* CONFIGURATION FILE USED BY BOTH CLIENT AND SERVER */
+
+var app_port = 8000,
+    sqlfile = "politique.sqlite",
+    tracking_keyword = "Hervé Morin,Eva Joly,Hollande,Sarkozy,Mélenchon,Bayrou,Villepin,Le Pen",
+    annotations = {
+        "default" : {
+            "colors" : {
+                "tweet" : "#ffffff",
+                "timeline" : "#808080"
+            }
+        },
+        "droite" : {
+            "keywords" : [ /\bump\b/i, /sarkozy/i ],
+            "colors" : {
+                "tweet" : "#ddddff",
+                "timeline" : "#000080"
+            }
+        },
+        "gauche" : {
+            "keywords" : [ /hollande/i, /mélenchon/i, /melenchon/i, /gauche/i, /\bps\b/i, /socialiste/i ],
+            "colors" : {
+                "tweet" : "#ffdddd",
+                "timeline" : "#800000"
+            }
+        },
+        "centre" : {
+            "keywords" : [ /modem/i, /centre/i, /morin/i, /bayrou/i ],
+            "colors" : {
+                "tweet" : "#ffeedd",
+                "timeline" : "#ff8000"
+            }
+        },
+        "verts" : {
+            "keywords" : [ /eelv/i, /joly/i, /écologiste/i, /ecologiste/i, /les verts/i ],
+            "colors" : {
+                "tweet" : "#ddffdd",
+                "timeline" : "#008000"
+            }
+        },
+        "extremeDroite" : {
+            "keywords" : [ /le pen/i, /extreme\-droite/i, /extrême\-droite/i, /front national/i, /\bfn\b/i, /marine/i ],
+            "colors" : {
+                "tweet" : "#e0d8d0",
+                "timeline" : "#402000"
+            }
+        }
+    }
\ No newline at end of file
--- a/tweetcast/nodejs/node-direct.js	Tue Oct 25 18:27:26 2011 +0200
+++ b/tweetcast/nodejs/node-direct.js	Wed Oct 26 18:29:46 2011 +0200
@@ -1,19 +1,37 @@
+/* DEFAULT CONFIGURATION */
+
+var TWITTER_USER = 'materiauxnum',
+    TWITTER_PASS = 'm473r14ux7w337',
+    RECORD_NEW_TWEETS = true;
+
 /* CALLING COMMON CONFIGURATION FILE */
 
-console.log('Reading Configuration from conf.js');
+var conf_file = flagOption('-c', 'conf.js');
+
+myLog('Reading Configuration from ' + conf_file);
 
 var fs = require('fs');
-eval(fs.readFileSync(__dirname + '/conf.js','utf8'));
-
-/* SERVER-SIDE ONLY CONFIGURATION */
+eval(fs.readFileSync(conf_file,'utf8'));
 
-sqlfile = __dirname + '/tweets-' + encodeURIComponent(tracking_keyword) + '.sqlite';
-TWITTER_USER = 'materiauxnum';
-TWITTER_PASS = 'm473r14ux7w337';
-RECORD_NEW_TWEETS = true;
+if (typeof sqlfile == "undefined") {
+    sqlfile = __dirname + '/tweets-' + encodeURIComponent(tracking_keyword) + '.sqlite'
+    }
 
 /* FUNCTIONS */
 
+function flagOption(flag, defaultValue) {
+    var flagPos = process.argv.indexOf(flag);
+    return ( flagPos != -1 && flagPos < process.argv.length - 1) ? process.argv[flagPos + 1] : defaultValue;
+}
+
+function myLog() {
+    var args = ["\033[1;34m["+new Date().toLocaleTimeString()+"]\033[0m"];
+    for (var i in arguments) {
+        args.push(arguments[i]);
+    }
+    console.log.apply(console, args);
+}
+
 function annotationMap(callback, options) {
     var includeDefault = ( options && options.includeDefault ? options.includeDefault : false );
     var returnObject = ( options && options.returnObject ? options.returnObject : false );
@@ -108,14 +126,14 @@
             commitTweet(newdata[i]);
         }
     }
-    console.log("New tweets received");
+    myLog("New tweets received");
 }
 
 function getSendLastPos() {
     db.execute("SELECT MAX(pos) lastpos FROM tweets", function (err, results) {
         if (err) throw err;
         lastpos = results[0].lastpos ? results[0].lastpos : 0;
-        console.log("Broadcasting last pos = ",lastpos);
+        myLog("Broadcasting last pos = ",lastpos);
         io.sockets.emit('tweetSummary', {
             tweetcount : lastpos
         });
@@ -124,7 +142,7 @@
 
 
 function getSendTweets(posList, socket) {
-    console.log("request for tweets ("+posList.join(',')+") from "+socket.id);
+    myLog("request for tweets ("+posList.join(',')+") from "+socket.id);
     db.execute("SELECT * FROM tweets WHERE pos IN ( " + posList.join(',') + " )", function (err, results) {
         if (err) throw err;
         socket.emit('tweets',
@@ -138,7 +156,7 @@
 }
 
 function getSendTimeline(level, socket) {
-    console.log("request for timeline ("+level+") from "+socket.id);
+    myLog("request for timeline ("+level+") from "+socket.id);
     var lvl = date_levels[level],
         requete = "SELECT COUNT(*) AS nb, "
         + lvl
@@ -184,11 +202,11 @@
 }
 
 function httpHandler(req, res) {
-    console.log("HTTP Request for URL "+req.url);
-    var url = __dirname + ( req.url == "/conf.js" ? "" : "/client" ) + req.url + ( req.url[req.url.length - 1] == "/" ? "index.html" : "" );
+    myLog("HTTP Request for URL "+req.url);
+    var url = ( req.url == "/config" ? conf_file : __dirname + "/client" + req.url + ( req.url[req.url.length - 1] == "/" ? "index.html" : "" ) );
     fs.readFile( url, function(err, data) {
         if (err) {
-            console.log("Error 404");
+            myLog("Error 404");
             res.writeHead(404);
             return res.end('File not found');
         }
@@ -271,15 +289,15 @@
 /* MAIN CODE */
 
 app.listen(app_port);
-console.log("Listening on port: "+app_port);
-console.log("Opening SQLITE file: "+sqlfile);
+myLog("Listening on port: "+app_port);
+myLog("Opening SQLITE file: "+sqlfile);
 db.open(sqlfile , function(err) {
     if (err) throw err;
     createTables();
 });
 
 if (RECORD_NEW_TWEETS) {
-    console.log("Requesting Twitter to track keyword(s): "+tracking_keyword);
+    myLog("Requesting Twitter to track keyword(s): "+tracking_keyword);
     var req = https.request({
         host: "stream.twitter.com",
         path: "/1/statuses/filter.json",
@@ -289,8 +307,8 @@
             'Content-Type': 'application/x-www-form-urlencoded'
         }
     }, function(res) {
-        console.log('Reply from stream.twitter.com: ' + res.statusCode);
-        console.log('Headers: ' + JSON.stringify(res.headers));
+        myLog('Reply from stream.twitter.com: ' + res.statusCode);
+        myLog('Headers: ' + JSON.stringify(res.headers));
         res.setEncoding('utf8');
         res.on('data', callBackNewTweets);
     });
@@ -301,8 +319,8 @@
 
 io.set('log level', 0);
 io.sockets.on('connection', function(socket) {
-    console.log("New connection from" + socket.handshake.address.address + " with id=" + socket.id);
-    socket.emit('tweetSummary', { tweetcount : tweets.length });
+    myLog("New connection from", socket.handshake.address.address, "with id=", socket.id);
+    socket.emit('tweetSummary', { tweetcount : lastpos });
     socket.on('updateTweets', function(data) {
         if (data.tweets.length) {
             getSendTweets(data.tweets, socket);