tweetcast/nodejs/server/node-direct.js
author Raphael Velt <raph.velt@gmail.com>
Tue, 18 Oct 2011 16:19:08 +0200
changeset 326 c28048fb63b4
parent 325 7d9c576bfaac
child 331 03c69425efa6
permissions -rw-r--r--
Added visual timeline feature to node client
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
     1
READ_OLD_TWEETS = true;
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
     2
RECORD_NEW_TWEETS = true;
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
     3
TWEET_FILE_DIR = './';
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
     4
TWEET_FILE_START = 'tweets-';
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
     5
TWEET_FILE_EXT = '.txt';
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
     6
TRACKING_KEYWORD = '#p2';
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
     7
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     8
var fs = require('fs'),
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     9
    https = require('https'),
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    10
    io = require('socket.io')
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    11
        .listen(8000),
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    12
    tweets = [],
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    13
    arcs = [],
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    14
    tweet_ids = [],
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    15
    date_struct = [],
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    16
    date_levels = [
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    17
        3600 * 1000,
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    18
        15 * 60 * 1000,
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    19
        5 * 60 * 1000,
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    20
        60 * 1000
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    21
    ],
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    22
    annkw = {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    23
        'positive' : '++',
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    24
        'negative' : '--',
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    25
        'reference' : '==',
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    26
        'question' : '??'
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    27
    };
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    28
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    29
function populateDateStruct(level, start) {
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    30
    if (typeof start == "object") {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    31
        start = start.valueOf();
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    32
    }
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    33
    var end = start + date_levels[level],
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    34
        struct = {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    35
            "level" : level,
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    36
            "start" : new Date(start),
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    37
            "end" : new Date(end)
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    38
        };
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    39
    if (level < date_levels.length - 1) {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    40
        struct.slices = [];
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    41
        var newstart = start;
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    42
        while (newstart < end) {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    43
            struct.slices.push(populateDateStruct(level + 1, newstart));
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    44
            newstart += date_levels[level + 1];
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    45
        }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    46
    } else {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    47
        struct.tweets = [];
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    48
        struct.annotations = {};
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    49
    }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    50
    return struct;
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    51
}
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    52
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    53
function insertIntoDateStruct(slices, tweet) {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    54
    var creadate = new Date(tweet.created_at);
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    55
    for (var i in slices) {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    56
        if (creadate < slices[i].end) {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    57
            if (slices[i].slices) {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    58
                insertIntoDateStruct(slices[i].slices, tweet);
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    59
            } else {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    60
                slices[i].tweets.push(tweet.pos);
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    61
                if (tweet.annotations.length) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    62
                    var ann = tweet.annotations[0];
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    63
                    if (slices[i].annotations[ann]) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    64
                        slices[i].annotations[ann].push(tweet.pos);
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    65
                    } else {
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    66
                        slices[i].annotations[ann] = [ tweet.pos ];
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    67
                    }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    68
                }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    69
            }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    70
            break;
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    71
        }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    72
    }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    73
}
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
    74
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    75
function getSliceContent(slice) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    76
    if (slice.slices) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    77
        var twz = [],
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    78
            annotations = {};
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    79
        for (var i in slice.slices) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    80
            var data = getSliceContent(slice.slices[i]);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    81
            twz = twz.concat(data.tweets);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    82
            for (var j in data.annotations) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    83
                if (annotations[j]) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    84
                    annotations[j] = annotations[j].concat(data.annotations[j]);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    85
                } else {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    86
                    annotations[j] = data.annotations[j];
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    87
                }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    88
            }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    89
        }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    90
    } else {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    91
        twz = slice.tweets;
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    92
        annotations = slice.annotations;
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    93
    }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    94
    return { "tweets" : twz, "annotations" : annotations }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    95
};
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    96
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    97
function flattenDateStruct(slices, target_level) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    98
    var current_level = slices[0].level,
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
    99
        result = [];
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   100
    if (current_level < target_level) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   101
        if (slices[0].slices) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   102
            for (var i in slices) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   103
                result = result.concat(flattenDateStruct(slices[i].slices, target_level));
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   104
            }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   105
        }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   106
    }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   107
    else {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   108
        for (var i in slices) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   109
            var data = getSliceContent(slices[i]);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   110
            result.push({
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   111
                "start" : slices[i].start,
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   112
                "end" : slices[i].end,
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   113
                "tweets" : data.tweets,
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   114
                "annotations" : data.annotations
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   115
            });
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   116
        }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   117
    }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   118
    return result;
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   119
}
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   120
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   121
function trimFDS(slices) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   122
    while (slices[0].tweets.length == 0) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   123
        slices.splice(0,1);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   124
    }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   125
    while (slices[slices.length - 1].tweets.length == 0) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   126
        slices.pop();
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   127
    }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   128
    return slices;
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   129
}
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   130
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   131
function addToList(tweet) {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   132
    if (tweet_ids.indexOf(tweet.id) != -1) {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   133
        console.log("Error: Tweet already in list");
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   134
        return;
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   135
    }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   136
    tweet.pos = tweets.length;
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   137
    tweets.push(tweet);
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   138
    tweet_ids.push(tweet.id);
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   139
    var creadate = new Date(tweet.created_at);
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   140
    if (!date_struct.length) {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   141
        date_struct = [ populateDateStruct(0, date_levels[0] * parseInt(creadate / date_levels[0])) ]
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   142
    }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   143
    while (creadate > date_struct[date_struct.length - 1].end) {
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   144
        date_struct.push( populateDateStruct(0, date_struct[date_struct.length - 1].end.valueOf()) );
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   145
    }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   146
    insertIntoDateStruct(date_struct, tweet);
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   147
    if (tweet.in_reply_to_status_id) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   148
        var ref = tweet_ids.indexOf(tweet.in_reply_to_status_id)
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   149
        if (ref != -1) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   150
            arcs.push({
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   151
                "from" : tweet.pos,
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   152
                "to" : ref,
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   153
                "type" : "reply"
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   154
            });
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   155
        }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   156
    }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   157
    if (tweet.retweeted_status) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   158
        var ref = tweet_ids.indexOf(tweet.retweeted_status.id_str)
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   159
        if (ref != -1) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   160
            arcs.push({
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   161
                "from" : tweet.pos,
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   162
                "to" : ref,
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   163
                "type" : "retweet"
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   164
            });
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   165
        }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   166
    }
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   167
}
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   168
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   169
function textids(object) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   170
    for (var key in object) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   171
        if (key.substr(-2) == 'id') {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   172
            object[key] = object[key + '_str'];
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   173
            delete object[key + '_str'];
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   174
        }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   175
    }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   176
}
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   177
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   178
function readTweetsFromFile() {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   179
    var dir = fs.readdirSync(TWEET_FILE_DIR),
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   180
        fileName = null;
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   181
    for (var i in dir) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   182
        if ( dir[i].indexOf( TWEET_FILE_START + encodeURIComponent(TRACKING_KEYWORD) ) == 0 ) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   183
            var oldtweets = fs.readFileSync(dir[i], 'utf8').split('\n'),
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   184
                tweetscopied = 0;
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   185
            for (var j in oldtweets) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   186
                if (oldtweets[j].length > 1) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   187
                    addToList(JSON.parse(oldtweets[j]));
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   188
                    tweetscopied++;
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   189
                }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   190
            }
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   191
            console.log(tweetscopied, "tweets copied from", dir[i]);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   192
            console.log(arcs);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   193
        }
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   194
    }
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   195
}
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   196
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   197
function callBackNewTweets(chunk) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   198
    var newdata = chunk.split('\r\n');
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   199
    for (var i in newdata) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   200
        if (newdata[i].length > 0) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   201
            var tweet = JSON.parse(newdata[i]),
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   202
                annotations = [];
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   203
            for (var a in annkw) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   204
                if (tweet.text.indexOf(annkw[a]) != -1) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   205
                    annotations.push(a);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   206
                }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   207
            }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   208
            tweet.annotations = annotations;
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   209
            textids(tweet);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   210
            textids(tweet.user);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   211
            
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   212
            addToList(tweet);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   213
            var txt = JSON.stringify(tweet)+'\n';
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   214
            writestream.write(txt);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   215
        }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   216
    }
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   217
    console.log("New tweets received. We now have", tweets.length, "tweets in memory");
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   218
    io.sockets.emit('tweetSummary', { tweetcount : tweets.length });
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   219
}
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   220
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   221
/* MAIN CODE */
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   222
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   223
if (READ_OLD_TWEETS) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   224
    readTweetsFromFile();
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   225
}
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   226
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   227
if (RECORD_NEW_TWEETS) {
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   228
    var now = new Date(),
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   229
        fileTimestamp = (1900+now.getYear())+"-"+("0"+(1+now.getMonth())).slice(-2)+"-"+("0"+now.getDate()).slice(-2)+"."+("0"+now.getHours()).slice(-2)+"-"+("0"+now.getMinutes()).slice(-2),
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   230
        fileName = TWEET_FILE_DIR + TWEET_FILE_START + encodeURIComponent(TRACKING_KEYWORD) + '-' + fileTimestamp + TWEET_FILE_EXT,
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   231
        writestream = fs.createWriteStream(fileName, { flags: 'w', encoding: 'utf-8' });
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   232
        console.log('Writing to',fileName);
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   233
    var req = https.request({
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   234
        host: "stream.twitter.com",
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   235
        path: "/1/statuses/filter.json",
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   236
        method: "POST",
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   237
        headers: {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   238
            'Authorization': 'Basic cmFwaHY6N3czMzdMZkMyM2dF',
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   239
            'Content-Type': 'application/x-www-form-urlencoded'
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   240
        }
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   241
    }, function(res) {
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   242
        console.log('STATUS: ' + res.statusCode);
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   243
        console.log('HEADERS: ' + JSON.stringify(res.headers));
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   244
        res.setEncoding('utf8');
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   245
        res.on('data', callBackNewTweets);
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   246
    });
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   247
    
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   248
    req.write('track=' + encodeURIComponent(TRACKING_KEYWORD));
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   249
    req.end();
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   250
}
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   251
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   252
io.set('log level', 0);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   253
io.sockets.on('connection', function(socket) {
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   254
    console.log("New connection from", socket.handshake.address.address,"id :", socket.id);
325
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   255
    socket.emit('tweetSummary', { tweetcount : tweets.length });
7d9c576bfaac Some changes - added multi-zoom level on client
Raphael Velt <raph.velt@gmail.com>
parents: 314
diff changeset
   256
    socket.on('getTweets', function(data) {
326
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   257
        console.log('getTweets from',socket.id);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   258
        data.tweets = tweets.slice(Math.max(0, data.from), data.to);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   259
        socket.emit('tweets', data);
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   260
    });
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   261
    socket.on('getTimeline', function(data) {
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   262
        socket.emit('timeline', { "timeline" : trimFDS(flattenDateStruct(date_struct, data.level)), "arcs" : arcs });
c28048fb63b4 Added visual timeline feature to node client
Raphael Velt <raph.velt@gmail.com>
parents: 325
diff changeset
   263
        console.log('getTimeline from',socket.id);
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   264
    });
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   265
});