incubators
Version:
/人◕‿‿◕人\「僕と契約して、魔法少女になってよ!」
118 lines (102 loc) • 4.12 kB
JavaScript
var util = require('util'),
twitter = require('twitter'),
Reply = require('./class/Reply.js'),
Tweet = require('./class/Tweet.js'),
fs = require('fs'),
path = require('path'),
io = require('socket.io').listen(8888).set("log level", 1);
var twit = new twitter({
consumer_key: 'qzBzxOJr1Aklzl9GIeQLIw',
consumer_secret: 'jXJiV8senY4kDU96VuMYfcZpOANAJxscpzlRBaGFuM',
access_token_key: '713419453-KRhxusYVvRlxrnX5mfZPbIZH3oDqEZnS9ry81umU',
access_token_secret: 'mHkdVUBeIgVr7VLeOKdvgR60TszujfgYF8zBXDx1ops'
});
var incubators = io
.of('/incubators')
.on('connection', function (socket) {
socket.emit('last_tweets', get_last_tweets( tweets, false));
socket.on('last_tweets_10', function ( timestamp) {
socket.emit('last_tweets_10', get_last_tweets( tweets, true, timestamp));
});
socket.on('msg', function ( text, timestamp) {
if ( timestamp == null ) timestamp = last_tweet_timestamp;
socket.emit('msg', tweets[timestamp].reply( new Reply( { text: text, parent: timestamp})));
});
});
var get_last_tweets = function( tweets, inc_order, timestamp){
if ( typeof timestamp == "undefined" ) timestamp = last_tweet_timestamp;
console.log(tweet_id_queue);
var target_timestamp_index = 0;
for ( var i in tweet_id_queue ) if ( tweet_id_queue[i] == timestamp ){ target_timestamp_index = i; break;}
var sliceMin = target_timestamp_index - 8;
var sliceMax = target_timestamp_index;
if ( sliceMin < 0 ) sliceMin = 0;
if ( sliceMax < 0 ) sliceMax = 0;
if ( !inc_order ) sliceMax += 1;
var target_tweet_id_array = tweet_id_queue.slice( sliceMin, sliceMax);
var obj = {};
if ( inc_order )
for ( var i = target_tweet_id_array.length - 1 ; i >= 0 ; i-- )
obj[target_tweet_id_array[i]] = tweets[target_tweet_id_array[i]];
else
for ( var i = 0 ; i < target_tweet_id_array.length ; i++ )
obj[target_tweet_id_array[i]] = tweets[target_tweet_id_array[i]];
return obj;
};
var friends = {};
var tweets = {};
var last_tweet_timestamp;
var tweet_id_queue = [];
twit.stream('user', function(stream) {
stream.on('data', function( data) {
if ( data.friends ){ friends = data.friends; return; }
if ( typeof data.text == "undefined" ) return;
var trimmed_source = {};
trimmed_source.text = data.text;
trimmed_source.entities = data.entities;
console.log( trimmed_source);
var new_tweet = new Tweet( {source:trimmed_source});
incubators.emit('tweet', tweets[new_tweet.timestamp] = new_tweet);
last_tweet_timestamp = new_tweet.timestamp;
tweet_id_queue.push( new_tweet.timestamp);
console.log(
fs.writeFile( file_tweets, JSON.stringify( tweets), 'utf8'));
fs.writeFile( file_last_tweet_timestamp, last_tweet_timestamp, 'utf8');
fs.writeFile( file_tweet_id_queue, JSON.stringify( tweet_id_queue), 'utf8');
});
});
var file_tweets = "./data/tweets.txt";
var file_last_tweet_timestamp = "./data/last_tweet_timestamp.txt";
var file_tweet_id_queue = "./data/tweet_index.txt";
path.exists( file_tweets, function(exists) {
if (exists)
{
fs.readFile(file_tweets, 'utf8', function(err, data) {
tweets = JSON.parse( data);
for ( var i in tweets )
{
tweets[i] = new Tweet( {
source: tweets[i].source,
replies: tweets[i].replies,
timestamp: tweets[i].timestamp}
);
}
});
}
});
path.exists( file_last_tweet_timestamp, function(exists) {
if (exists)
{
fs.readFile( file_last_tweet_timestamp, 'utf8', function(err, data) {
last_tweet_timestamp = data;
});
}
});
path.exists( file_tweet_id_queue, function(exists) {
if (exists)
{
fs.readFile( file_tweet_id_queue, 'utf8', function(err, data) {
tweet_id_queue = JSON.parse( data);
});
}
});