针对DISCUZ!NT论坛的机器人一些方法

基于nodejs和一个nodejs的包request


代码如下:rhinoceros.js(犀牛)

其中postList是为了串行的发送发帖的请求(一下子都抛出去,会发生丢失)

var request = require("request").defaults({
	jar: true
});
var querystring = require("querystring");



function register(url, username, password, email) {
	var obj = {
		createuser: 1,
		regsubmit: true,
		username: username,
		password: password,
		password2: password,
		email: email
	};

	url = url + "?" + querystring.stringify(obj);
	// console.log(url);
	request.post(url, function(error, res, body) {
		if (error) {
			console.log("register fail ...");
		} else {
			console.log("register success ...");
		}
	});

}

function login(url, username, password, next) {
	var obj = {
		answer: "",
		expires: 43200,
		login: "",
		password: password,
		question: 0,
		templateid: 0,
		username: username
	};
	url = url + "?" + querystring.stringify(obj);
	console.log(url);

	request({
		url: url,
		method: "POST"
	}, function(error, res, body) {
		if (error) {
			console.log("login fail ...");
		} else {
			console.log("login success ...");
			// console.log(body);

			// var keyStr = "userkey=";
			// var index = body.indexOf(keyStr);
			// if (index >= 0) {
			// 	var userkey = body.substr(index, 18);
			// 	console.log(userkey);
			// }
			next && next();
		}
	});
}

// needuserkey
function logout(url, userkey) {
	request.post(url + "?userkey=" + userkey);
}

function post(url, data, headers) {
	// title
	(function(data) {
		data.title1 = data.title;
		delete data.title;
	})(data);

	// icon
	(function(data) {
		var iconDict = {
			"new": 0,
			"love": 2,
			"leaf": 3,
			"video": 9,
			"hot": 10,
			"info": 12,
			"paper": 14,
			"flower": 15

		};
		data.iconid = iconDict[data.icon];
		delete data.icon;
	})(data);
	// text video image
	(function(data) {
		var message = [];
		data.content && message.push(data.content);
		if (data.videoList) {
			data.videoList.forEach(function(vi, i) {
				message.push(["[flash]", vi, "[/flash]"].join(""));

			});
		}
		if (data.imageList) {
			data.imageList.forEach(function(ii, i) {
				message.push(["[image]", ii, "[/image]"].join(""));

			});
		}
		data.message = message.join("");
		delete data.content;
		delete data.imageList;
		delete data.videoList;
	})(data);
	var getTime = function() {
		var now = new Date();
		return [[now.getFullYear(), +now.getMonth() + 1, now.getDate()].join("/"), [now.getHours(), now.getMinutes(), now.getSeconds()].join(":")].join(" ");
	}
	var _defOpts = {
		posttime: getTime(),
		iconid: 3, // left icon
		wysiwyg: 1,
		tags: "",
		topicsubmit: true,
		parseurloff: 1,
		usesig: 1,
		"imglocalid[]": "",
		Filedata: ""
	};
	for (var i in data) {
		_defOpts[i] = data[i];
	}
	var obj = _defOpts;
	url = url + "&" + querystring.stringify(obj);
	// console.log(JSON.stringify(obj, undefined, 4));
	// console.log(url);
	request({
		url: url,
		headers: headers,
		method: "POST"
	}, function(error, res, body) {
		if (error) {
			console.log("pots fail ...");
		} else {
			console.log("post success ...");
			// console.log(body);
		}
	});
}


var rhinoceros = function(hostname, postDict) {
	this.hostname = "http://www." + hostname;

	this.registerUrl = this.hostname + "/register.aspx";
	this.loginUrl = this.hostname + "/login.aspx";
	this.logoutUrl = this.hostname + "/logout.aspx";
	//
	this.postDict = postDict;
	//
	this.headers = {
		'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)',
		'Cookie': 'dinoId=' + new Date().getTime(),
		"Host": hostname,
		// "Referer": this.hostname+"/index.aspx"
		"Referer": "http://" + hostname + "/"
	};
};
rhinoceros.prototype.register = function(username, password, email) {
	register(this.registerUrl, username, password, email);
};
rhinoceros.prototype.login = function(username, password, next) {
	login(this.loginUrl, username, password, next);
};
rhinoceros.prototype.logout = function() {
	logout(this.logoutUrl);
};

rhinoceros.prototype.post = function(nameOrUrl, data) {
	var postUrl = nameOrUrl.indexOf("http://") >= 0 ? nameOrUrl : this.hostname + this.postDict[nameOrUrl];
	post(postUrl, data, this.headers);

}

rhinoceros.prototype.postList = function(nameOrUrl, arr, delay) {
	var myRhino=this;
	var fnQueue = {
		_list: [],
		push: function(item) {
			this._list.push(item);
		},
		run: function(delay) {
			var me = this;
			var index = 0;
			var fn = function() {
				if (index == me._list.length) {
					return;
				}
				setTimeout(function() {
					me._list[index]();
					index++;
					fn();
				}, delay);

			};
			fn();
		}
	};
	arr.forEach(function(n, i) {
		fnQueue.push(function() {
			myRhino.post(nameOrUrl, n, delay);
		});
	});

	fnQueue.run(delay);
};

module.exports = rhinoceros;

这个代码模块有待继续补充...



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值