Skip to content

Instantly share code, notes, and snippets.

@ashi009
Created October 23, 2012 14:00
Show Gist options
  • Save ashi009/3938907 to your computer and use it in GitHub Desktop.
Save ashi009/3938907 to your computer and use it in GitHub Desktop.
Shuafen.js
node shuafen.js
var http = require('http');
var gDate;
var gVoteData;
var gCount = 0;
var kNumberOfConnections = 20;
function checkIndex(callback) {
http.get('http://szjyzx.cnu.edu.cn/szjyzx/a/class2011/2012niandu/', function(res) {
var str = '';
res.on('data', function(chunk) {
str += chunk;
});
res.on('end', function() {
var match = /value='(\d+)'\s*\/>\s*教育学院09级心理班/im.exec(str);
if (!match) {
console.error('failed to retieve vote index.');
process.exit();
}
gVoteData = 'dopost=send&aid=10&ismore=1&voteitem%5B%5D=' + match[1] +
'&vbt1=%E6%8A%95%E7%A5%A8';
if (callback)
callback();
});
}).on('error', function(err) {
console.error(err);
process.nextTick(checkIndex);
});
}
function vote() {
var req = http.request({
method: 'POST',
host: 'szjyzx.cnu.edu.cn',
path: '/szjyzx/plus/vote.php',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}, function(res) {
res.on('data', function(chunk) {
req.abort();
});
res.on('end', function() {
gCount++;
process.nextTick(vote);
});
});
req.on('error', function(err) {
console.error(err);
process.nextTick(vote);
});
req.write(gVoteData);
req.end();
}
http.globalAgent.maxSockets = kNumberOfConnections;
checkIndex(function() {
gDate = new Date();
for (var i = 0; i < kNumberOfConnections; i++)
vote();
setInterval(function() {
console.log("%d in total, %d votes / s", gCount, gCount / (new Date() - gDate) * 1000);
}, 1000);
setInterval(checkIndex, 10000);
});
process.on('SIGINT', function() {
console.log('total %d votes', gCount);
process.exit();
});
#!/bin/sh
node shuafen.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment