Instantly share code, notes, and snippets.

Embed
What would you like to do?
magic kaito and detective conan tv tweet correcting script
/* magic kaito and detective conan live tweet correcting script */
/*jslint node: true */
'use strict';
var Twitter = require('twitter');
var fs = require('fs');
var util = require('util');
require('date-utils');
var today = new Date();
var todaystr = today.toFormat("YY-MMM-DD");
var restInterval = 7;
var twr = new Twitter({
consumer_key: 'XXX',
consumer_secret: 'XXX',
access_token_key: 'XXX',
access_token_secret: 'XXX'
});
var mkarr = ["#kaito1412"];
var mkrestarr = ["まじ快", "快斗"];
var dcarr = ["#conan"];
var dcrestarr = ["コナン"];
var tvarr = ["#ntv", "#ytv"];
var mkstart = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 17, 25, 0);
var mktvstart = new Date(today.year, today.getMonth(), today.getDate(), 17, 30, 0);
var mktvdcstart = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 17, 55, 0);
var mkdctvchange = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 18, 0, 0);
var dctvend = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 18, 30, 0);
var mkend = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 19, 0, 0);
var dcend = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 19, 30, 0);
//var today = new Date();
//var todaystr = today.toFormat("YY-MMM-DD");
var mkfname = "magickaito-tw-" + todaystr + ".csv";
var dcfname = "conan-tw-" + todaystr + ".csv";
// start debugging
/*
var mkarr = ["macintosh","ipod"];
var mkrestarr = ["ブラウザ", "タブレット"];
var dcarr = ["walkman"];
var dcrestarr = ["パソコン"];
var tvarr = ["Chrome", "Windows"];
var debugH = today.getHours();
var debugM = today.getMinutes();
var mkstart = new Date(today.getFullYear(), today.getMonth(), today.getDate(), debugH, debugM+1, 0);
var mktvstart = new Date(today.year, today.getMonth(), today.getDate(), debugH, debugM+1, 30);
var mktvdcstart = new Date(today.getFullYear(), today.getMonth(), today.getDate(), debugH, debugM+2, 0);
var mkdctvchange = new Date(today.getFullYear(), today.getMonth(), today.getDate(), debugH, debugM+2, 30);
var dctvend = new Date(today.getFullYear(), today.getMonth(), today.getDate(), debugH, debugM+3, 0);
var mkend = new Date(today.getFullYear(), today.getMonth(), today.getDate(), debugH,debugM+3, 30);
var dcend = new Date(today.getFullYear(), today.getMonth(), today.getDate(), debugH, debugM+4, 0);
//var today = new Date();
//var todaystr = today.toFormat("YY-MMM-DD");
var mkfname = "test1-tw-" + todaystr + ".csv";
var dcfname = "test2-tw-" + todaystr + ".csv";
*/
// end debugging
var mkfs = fs.openSync(mkfname, "a");
var dcfs = fs.openSync(dcfname, "a");
var searchstr = mkarr.join(',') + "," + dcarr.join(',') + "," + tvarr.join(',');
//restsearchstr = searchstr = mkrestarr.join(' OR ')+" OR "+dcrestarr.join(' OR ');
function isIncluded(targetstr, wordarr) {
var isincluded = false;
targetstr = targetstr.toUpperCase();
wordarr.forEach(function (word) {
if (targetstr.indexOf(word.toUpperCase()) !== -1) {
isincluded = true;
}
});
return isincluded;
}
function toCSV(dataarr) {
var csvstr = "";
dataarr.forEach(function (datatext) {
datatext = datatext.replace("\"", "\"\"");
if (datatext.indexOf("\n") !== -1 || datatext.indexOf(",") !== -1 || datatext.indexOf("\"") !== -1) {
datatext = "\"" + datatext + "\"";
}
csvstr += (csvstr !== "") ? ("," + datatext) : datatext;
});
return csvstr;
}
function endScript() {
fs.closeSync(mkfs);
fs.closeSync(dcfs);
console.log("file closed");
}
function streamSearch() {
twr.stream('statuses/filter', {'track': searchstr}, function (stream) {
stream.on('data', function (data) {
var tweettime = Date.parse(data.created_at),
checkarr = [],
tweetstr = data.text;
if (tweettime < mkstart) {
checkarr = [];
} else if (tweettime < mktvstart) {
checkarr = mkarr;
} else if (tweettime < mktvdcstart) {
checkarr = mkarr.concat(tvarr);
} else if (tweettime < mkdctvchange) {
checkarr = mkarr.concat(dcarr).concat(tvarr);
} else if (tweettime < dctvend) {
checkarr = mkarr.concat(dcarr).concat(tvarr);
} else if (tweettime < mkend) {
checkarr = mkarr.concat(dcarr);
} else if (tweettime < dcend) {
checkarr = dcarr;
}
if (isIncluded(tweetstr, checkarr)) {
var result = toCSV([data.id.toString(), data.user.screen_name, data.created_at, data.text]),
ismk = isIncluded(tweetstr, mkarr),
isdc = isIncluded(tweetstr, dcarr);
console.log("stream:" + result);
result = result + "\n";
if (ismk && isdc) {
if (tweettime < mkdctvchange) {
fs.writeSync(mkfs, result);
} else {
fs.writeSync(dcfs, result);
}
} else if (ismk) {
fs.writeSync(mkfs, result);
} else if (isdc) {
fs.writeSync(dcfs, result);
} else {
if (tweettime < mkdctvchange) {
fs.writeSync(mkfs, result);
} else {
fs.writeSync(dcfs, result);
}
}
} else {
console.log("ignored tweet:" + tweetstr);
}
if (dcend < tweettime) {
stream.destroy();
setTimeout(endScript, 3000);
}
});
stream.on('end', function (response) {
//切断された場合
console.log("stream ended:" + util.inspect(response));
setTimeout(stream.start, 3000);
});
stream.on('destroy', function (response) {
//接続が破棄された場合
console.log("stream destroyed:" + util.inspect(response));
});
});
}
var sinceID = 0;
function restSearch() {
//
var searcharr = [],
tweettime = new Date();
if (tweettime < mkstart) {
searcharr = [];
} else if (tweettime < mktvstart) {
searcharr = mkrestarr;
} else if (tweettime < mktvdcstart) {
searcharr = mkrestarr;
} else if (tweettime < mkdctvchange) {
searcharr = mkrestarr.concat(dcrestarr);
} else if (tweettime < dctvend) {
searcharr = mkrestarr.concat(dcrestarr);
} else if (tweettime < mkend) {
searcharr = mkrestarr.concat(dcrestarr);
} else if (tweettime < dcend) {
searcharr = dcrestarr;
}
if ((new Date()) < dcend) {
twr.search(searcharr.join(' OR '), {count: 100, since_id: sinceID, result_type: "recent"}, function (data) {
//
var tweets = (data.statuses || []).reverse();
tweets.forEach(function (tweet) {
var result = toCSV([tweet.id.toString(), tweet.user.screen_name, tweet.created_at, tweet.text]),
ismk = isIncluded(tweet.text, mkrestarr),
isdc = isIncluded(tweet.text, dcrestarr);
console.log("rest:" + result);
sinceID = tweet.id;
result = result + "\n";
if (ismk && isdc) {
if (tweettime < mkdctvchange) {
fs.writeSync(mkfs, result);
} else {
fs.writeSync(dcfs, result);
}
} else if (ismk) {
fs.writeSync(mkfs, result);
} else if (isdc) {
fs.writeSync(dcfs, result);
} else {
if (tweettime < mkdctvchange) {
fs.writeSync(mkfs, result);
} else {
fs.writeSync(dcfs, result);
}
}
});
//
//call next search
setTimeout(restSearch, restInterval * 1000);
//
});
//
}
//
}
// waiting
var waitTime = mkstart - today;
if (waitTime < 0) {
waitTime = 0;
}
console.log("waiting for " + waitTime);
setTimeout(streamSearch, waitTime);
setTimeout(restSearch, waitTime);
//console.log();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment