Created conf.js file, shared between server & client
authorRaphael Velt <raph.velt@gmail.com>
Tue, 25 Oct 2011 14:48:22 +0200
changeset 335 5f83c21dee69
parent 334 b7f05d66b620
child 336 d60efd677b50
Created conf.js file, shared between server & client
tweetcast/nodejs/client/css/style.css
tweetcast/nodejs/client/index.html
tweetcast/nodejs/client/js/script.js
tweetcast/nodejs/conf.js
tweetcast/nodejs/node-direct.js
--- a/tweetcast/nodejs/client/css/style.css	Tue Oct 25 09:36:21 2011 +0200
+++ b/tweetcast/nodejs/client/css/style.css	Tue Oct 25 14:48:22 2011 +0200
@@ -158,22 +158,6 @@
     opacity: .4
 }
 
-.positive {
-	background: #c5e7cd;
-}
-
-.negative {
-	background: #f6ced0;
-}
-
-.reference {
-	background: #ecedc1;
-}
-
-.question {
-	background: #bfdbec;
-}
-
 /* timeline */
 
 #timeline, #timewindow {
--- a/tweetcast/nodejs/client/index.html	Tue Oct 25 09:36:21 2011 +0200
+++ b/tweetcast/nodejs/client/index.html	Tue Oct 25 14:48:22 2011 +0200
@@ -4,6 +4,7 @@
   <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>
--- a/tweetcast/nodejs/client/js/script.js	Tue Oct 25 09:36:21 2011 +0200
+++ b/tweetcast/nodejs/client/js/script.js	Tue Oct 25 14:48:22 2011 +0200
@@ -11,19 +11,6 @@
         "posIndex" : [],
         "tlChanged" : true
     },
-    colors = {
-        'positive' : "#1D973D",
-        'reference' : "#C5A62D",
-        'negative' : "#CE0A15",
-        'question' : "#036AAE",
-        'neutre' : "#585858"
-    },
-    annotations = {
-        'positive' : '++',
-        'negative' : '--',
-        'reference' : '==',
-        'question' : '??'
-    },
     displaySplitting = [
         {
             positions : [ 10, 20, 22, 24, 25, 26, 27, 28, 29, 31, 33, 43, 53 ],
@@ -51,7 +38,7 @@
 	if (tweet.annotations.length) {
 	    html += '<div class="annotations">';
 	    for (var i in tweet.annotations) {
-    		html += '<div class="annotation ' + tweet.annotations[i] + '" style="width :' + (100/tweet.annotations.length) + '%"></div>';
+    		html += '<div class="annotation" style="width:' + (100/tweet.annotations.length) + '%; background:' + annotations[tweet.annotations[i]].colors.tweet + '"></div>';
     	}
     	html += '</div>';
 	}
@@ -240,16 +227,16 @@
             tlPaper.text(165, posY, new Date(tweetData.timeline[i].start).toLocaleTimeString()).attr({ "text-anchor" : "start", "font-size": "12px" });
         }
         var anz = {
-            "neutre" :tweetData.timeline[i].tweets
+            "default" :tweetData.timeline[i].tweets
         };
         for (var j in tweetData.timeline[i].annotations) {
-            anz.neutre -= tweetData.timeline[i].annotations[j];
+            anz.default -= tweetData.timeline[i].annotations[j];
             anz[j] = tweetData.timeline[i].annotations[j];
         }
         var posX = 0;
         for (var j in anz) {
             var largX = scaleX * anz[j];
-            tlPaper.rect(posX, 600 - scaleY * (i+1), largX, scaleY).attr({"stroke": "none", "fill": colors[j]});
+            tlPaper.rect(posX, 600 - scaleY * (i+1), largX, scaleY).attr({"stroke": "none", "fill": annotations[j].colors.timeline});
             posX += largX;
         }
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tweetcast/nodejs/conf.js	Tue Oct 25 14:48:22 2011 +0200
@@ -0,0 +1,40 @@
+/* CONFIGURATION FILE USED BY BOTH CLIENT AND SERVER */
+
+var app_port = 8000,
+    tracking_keyword = "Obama,Bieber",
+    annotations = {
+        "default" : {
+            "colors" : {
+                "tweet" : "#ffffff",
+                "timeline" : "#585858"
+            }
+        },
+        "positive" : {
+            "keywords" : [ /\+\+/ ],
+            "colors" : {
+                "tweet" : "#c5e7cd",
+                "timeline" : "#1D973D"
+            }
+        },
+        "negative" : {
+            "keywords" : [ /\-\-/ ],
+            "colors" : {
+                "tweet" : "#f6ced0",
+                "timeline" : "#CE0A15"
+            }
+        },
+        "reference" : {
+            "keywords" : [ /\=\=/ ],
+            "colors" : {
+                "tweet" : "#ecedc1",
+                "timeline" : "#C5A62D"
+            }
+        },
+        "question" : {
+            "keywords" : [ /\?\?/ ],
+            "colors" : {
+                "tweet" : "#bfdbec",
+                "timeline" : "#036AAE"
+            }
+        }
+    }
\ No newline at end of file
--- a/tweetcast/nodejs/node-direct.js	Tue Oct 25 09:36:21 2011 +0200
+++ b/tweetcast/nodejs/node-direct.js	Tue Oct 25 14:48:22 2011 +0200
@@ -1,21 +1,39 @@
-/* CONFIGURATION */
+/* CALLING COMMON CONFIGURATION FILE */
+
+console.log('Reading Configuration from conf.js');
 
-RECORD_NEW_TWEETS = true;
-DEFAULT_SIO_PORT = 8000;
-/* Overriden par the "-p" parameter, e.g. node tweetcast.js -p 8080 */
-SQLITE_FILE_DIR = __dirname + '/';
-SQLITE_FILE_START = 'tweets-';
-SQLITE_FILE_EXT = '.sqlite';
-DEFAULT_TRACKING_KEYWORD = 'Bieber';
-/* Overriden par the "-T" parameter, e.g. node tweetcast.js -T "Bieber" */
+var fs = require('fs');
+eval(fs.readFileSync(__dirname + '/conf.js','utf8'));
+
+/* SERVER-SIDE ONLY CONFIGURATION */
+
+sqlfile = __dirname + '/tweets-' + encodeURIComponent(tracking_keyword) + '.sqlite';
 TWITTER_USER = 'materiauxnum';
 TWITTER_PASS = 'm473r14ux7w337';
+RECORD_NEW_TWEETS = true;
 
 /* FUNCTIONS */
 
+function annotationMap(callback, options) {
+    var includeDefault = ( options && options.includeDefault ? options.includeDefault : false );
+    var returnObject = ( options && options.returnObject ? options.returnObject : false );
+    res = (returnObject ? {} : []);
+    for (var i in annotations) {
+        if (i != "default" || includeDefault) {
+            var el = callback(i, annotations[i])
+            if (returnObject) {
+                res[i] = el;
+            } else {
+                res.push(el);
+            }
+        }
+    }
+    return res;
+}
+
 function createTables() {
 
-    var requete = "CREATE TABLE IF NOT EXISTS tweets ( pos INTEGER PRIMARY KEY, tweet_id TEXT UNIQUE, created_at INTEGER, json TEXT" + annotations.map(function(a) { return ', a_' + a + ' INTEGER' }).join("") + " )";
+    var requete = "CREATE TABLE IF NOT EXISTS tweets ( pos INTEGER PRIMARY KEY, tweet_id TEXT UNIQUE, created_at INTEGER, json TEXT" + annotationMap(function(a) { return ', a_' + a + ' INTEGER' }).join("") + " )";
     db.execute(requete, function(err) {
         if (err) throw err;
         db.execute("CREATE INDEX IF NOT EXISTS idx_created_at ON tweets ( created_at )", function(err) { if (err) throw err; });
@@ -52,14 +70,14 @@
             delete tweet.retweeted_status[keys_to_delete[j]];
         }
     }
-    for (var i in annotations_keywords) {
-        for (var j in annotations_keywords[i]) {
-            if (tweet.text.indexOf(annotations_keywords[i][j]) != -1) {
-                ann.push(annotations[i]);
+    annotationMap(function(i, annotation) {
+        for (var j in annotation.keywords) {
+            if (tweet.text.search(annotation.keywords[j]) != -1) {
+                ann.push(i);
                 break;
             }
         }
-    }
+    });
     tweet.annotations = ann;
     tweet.created_at = new Date(tweet.created_at);
     
@@ -71,11 +89,11 @@
     }
     db.execute(
         "INSERT INTO tweets ( tweet_id, created_at, json "
-        + annotations.map(function(a) { return ', a_' + a }).join("")
+        + annotationMap(function(a) { return ', a_' + a }).join("")
         + " ) VALUES ( ?, ?, ? "
-        + annotations.map(function(a) { return ', ?' }).join("")
+        + annotationMap(function(a) { return ', ?' }).join("")
         + " )",
-        [ tweet.id, tweet.created_at.valueOf(), JSON.stringify(tweet) ].concat(annotations.map(function(a) { return ann.indexOf(a) == -1 ? 0 : 1 })),
+        [ tweet.id, tweet.created_at.valueOf(), JSON.stringify(tweet) ].concat(annotationMap(function(a) { return ann.indexOf(a) == -1 ? 0 : 1 })),
         function(err) {
             if (err) throw err;
             getSendLastPos();
@@ -127,7 +145,7 @@
         + "*ROUND(created_at/"
         + lvl
         + ") AS tranche"
-        + annotations.map(function (a) { return " , SUM(a_" + a + ") AS s_" + a }).join("")
+        + annotationMap(function (a) { return " , SUM(a_" + a + ") AS s_" + a }).join("")
         + " FROM tweets GROUP BY tranche ORDER BY tranche DESC LIMIT 0,50";
     db.execute(requete, function (err, results) {
         if (err) throw err;
@@ -146,10 +164,9 @@
                 "start" : start,
                 "end" : lastend,
                 "tweets" : results[i].nb,
-                "annotations" : {}
-            }
-            for (var j in annotations) {
-                struct.annotations[annotations[j]] = results[i]['s_' + annotations[j]];
+                "annotations" : annotationMap(function (a) {
+                    return results[i]['s_'+a];
+                },{returnObject: true})
             }
             tbl.push(struct);
         }
@@ -168,8 +185,8 @@
 
 function httpHandler(req, res) {
     console.log("HTTP Request for URL "+req.url);
-    var url = req.url + ( req.url[req.url.length - 1] == "/" ? "index.html" : "" );
-    fs.readFile(__dirname + "/client" + url, function(err, data) {
+    var url = __dirname + ( req.url == "/conf.js" ? "" : "/client" ) + req.url + ( req.url[req.url.length - 1] == "/" ? "index.html" : "" );
+    fs.readFile( url, function(err, data) {
         if (err) {
             console.log("Error 404");
             res.writeHead(404);
@@ -182,8 +199,7 @@
 
 /* Initialization */
 
-var fs = require('fs'),
-    http = require('http'),
+var http = require('http'),
     https = require('https'),
     sqlite = require('sqlite'),
     socketio = require('socket.io'),
@@ -199,14 +215,6 @@
         60 * 1000,
         15 * 1000
     ],
-    annotations = [ 'positive', 'negative', 'reference', 'question' ],
-    annotations_keywords = [ [ '++' ], [ '--' ], [ '==' ], [ '??' ] ],
-    annkw = {
-        'positive' : '++',
-        'negative' : '--',
-        'reference' : '==',
-        'question' : '??'
-    },
     keys_to_delete = [
         'in_reply_to_screen_name',
         'in_reply_to_user_id',
@@ -257,19 +265,13 @@
         'utc_offset'
     ],
     app = http.createServer(httpHandler),
-    port_flag = process.argv.indexOf("-p"),
-    sio_port = ( port_flag != -1 && port_flag < process.argv.length - 1 && parseInt(process.argv[port_flag + 1]) ? parseInt(process.argv[port_flag + 1]) : DEFAULT_SIO_PORT )
     io = socketio.listen(app),
-    track_flag = process.argv.indexOf("-T"),
-    tracking_keyword = ( track_flag != -1 && track_flag < process.argv.length - 1 ? process.argv[track_flag + 1] : DEFAULT_TRACKING_KEYWORD ),
-    sqlfile = SQLITE_FILE_DIR + SQLITE_FILE_START + encodeURIComponent(tracking_keyword) + SQLITE_FILE_EXT,
     db = new sqlite.Database();
 
 /* MAIN CODE */
 
-app.listen(sio_port);
-
-console.log("Listening on port: "+sio_port);
+app.listen(app_port);
+console.log("Listening on port: "+app_port);
 console.log("Opening SQLITE file: "+sqlfile);
 db.open(sqlfile , function(err) {
     if (err) throw err;