Skip to content

Instantly share code, notes, and snippets.

@gtk2k
Last active August 29, 2015 14:07
Show Gist options
  • Save gtk2k/58f74ebcf1ec08e8bc2f to your computer and use it in GitHub Desktop.
Save gtk2k/58f74ebcf1ec08e8bc2f to your computer and use it in GitHub Desktop.
var http = require('http');
var https = require('https');
var parseString = require('xml2js').parseString;
var net = require('net');
var nicoLogin = function() {
var buf = new Buffer('mail=hoge&password=pass');
var options = {
host: 'secure.nicovideo.jp',
path: '/secure/login?site=niconico',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': buf.length
},
method: 'POST'
}
var req = https.request(options, function (res) {
var cookies = res.headers['set-cookie'];
var cookie = '';
for (var i = cookies.length; i--;) {
if (cookies[i].indexOf('user_session_') + 1) {
return getPlayerStatus(cookies[i]);
}
}
});
req.write(buf);
req.end();
}();
function getPlayerStatus(cookie) {
var options = {
host: 'watch.live.nicovideo.jp',
path: '/api/getplayerstatus?v=lv197261141', // lv~のところは適宜変更
headers: { cookie: cookie }
};
var req = http.request(options, function (res) {
var strRes = '';
res.on('data', function (data) {
strRes += data.toString();
});
res.on('end', function () {
parseString(strRes, function (err, result) {
var ms = result.getplayerstatus.ms[0];
getComment(ms.port[0], ms.addr[0], ms.thread[0]);
});
});
}).end();
}
function getComment(port, addr, threadId) {
var socket = new net.Socket();
socket.connect(port, addr, function () {
socket.write('<thread thread="' + threadId + '" version="20061206" res_from="-1"/>\0');
});
socket.on('data', function (data) {
parseString(data.toString(), function (err, result) {
if (result.chat) {
console.log(result.chat._); //コメ出力
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment