tweetcast/nodejs/node-direct.js
changeset 335 5f83c21dee69
parent 334 b7f05d66b620
child 338 60dff8a71024
--- 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;