tweetcast/nodejs/server/node-direct.js
author Raphael Velt <raph.velt@gmail.com>
Fri, 14 Oct 2011 17:35:49 +0200
changeset 314 0f1e6ce19b6d
child 325 7d9c576bfaac
permissions -rw-r--r--
Ajout d une branche NodeJS
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
314
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     1
var fs = require('fs'),
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     2
    https = require('https'),
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     3
    io = require('socket.io')
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     4
        .listen(8000)
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     5
    keyword = "Bieber",
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     6
    tweets = [],
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     7
    tweet_ids = [],
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     8
    annkw = {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     9
        'positive' : '++',
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    10
        'negative' : '--',
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    11
        'reference' : '==',
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    12
        'question' : '??'
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    13
    }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    14
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    15
function textids(object) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    16
    for (var key in object) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    17
        if (key.substr(-2) == 'id') {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    18
            object[key] = object[key + '_str'];
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    19
            delete object[key + '_str'];
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    20
        }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    21
    }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    22
}
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    23
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    24
var fd = fs.createWriteStream('tweets.txt');
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    25
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    26
req = https.request({
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    27
    host: "stream.twitter.com",
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    28
    path: "/1/statuses/filter.json",
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    29
    method: "POST",
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    30
    headers: {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    31
        'Authorization': 'Basic cmFwaHY6N3czMzdMZkMyM2dF',
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    32
        'Content-Type': 'application/x-www-form-urlencoded'
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    33
    }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    34
}, function(res) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    35
    console.log('STATUS: ' + res.statusCode);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    36
    console.log('HEADERS: ' + JSON.stringify(res.headers));
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    37
    res.setEncoding('utf8');
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    38
    res.on('data', function(chunk) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    39
        newdata = chunk.split('\r\n');
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    40
        try {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    41
            for (var i in newdata) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    42
                if (newdata[i].length > 0) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    43
                    tweet = JSON.parse(newdata[i]);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    44
                    annotations = [];
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    45
                    for (var a in annkw) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    46
                        if (tweet.text.indexOf(annkw[a]) != -1) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    47
                            annotations.push(a);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    48
                        }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    49
                    }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    50
                    tweet.annotations = annotations;
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    51
                    tweets.push(tweet);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    52
                    textids(tweet);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    53
                    tweet_ids.push(tweet.id_str);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    54
                    io.sockets.emit('newtweet', tweet);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    55
                }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    56
            }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    57
            fd.write(chunk); 
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    58
        }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    59
        catch(err) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    60
            console.log(err);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    61
        }
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    62
    });
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    63
});
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    64
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    65
req.write('track=' + encodeURIComponent(keyword));
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    66
req.end();
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    67
io.set('log level', 0);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    68
io.sockets.on('connection', function(socket) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    69
    socket.emit('tweets', tweets.slice(-10));
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    70
    socket.on('tweetsbefore', function(data) {
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    71
        tweetpos = tweet_ids.indexOf(data);
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    72
        socket.emit('oldtweets', tweets.slice(0, tweetpos).slice(-10));
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    73
    });
0f1e6ce19b6d Ajout d une branche NodeJS
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    74
});