Commit c04f3c61 by mawei

spider code refactor

parent 77212e42
...@@ -7,7 +7,7 @@ const path = require('path'); ...@@ -7,7 +7,7 @@ const path = require('path');
const program = require('commander'); const program = require('commander');
const config = require('config'); const config = require('config');
const captcha = require('./lib/captcha'); const captcha = require('./lib/captcha');
const DealerSpider = require('./lib/dealerspider/index'); const Spider = require('./lib/spider');
program program
.version('0.1.0') .version('0.1.0')
...@@ -16,10 +16,10 @@ program ...@@ -16,10 +16,10 @@ program
.parse(process.argv); .parse(process.argv);
// console.log(process.pid); // console.log(process.pid);
const dealerWebInfo = config.get('Dealer')[program.webname]; const dealerWebInfo = config.get(program.webname);
const spider = new DealerSpider({ const spider = new Spider({
username : dealerWebInfo.username, username : dealerWebInfo.USERNAME,
password : dealerWebInfo.password, password : dealerWebInfo.PASSWORD,
webname : program.webname, webname : program.webname,
task : program.task task : program.task
}); });
......
[dmt] [DMT]
id = 48793 ID = 48793
key = "c6c1bb5ea2c8f3706ed531c6cb5169cb" KEY = "c6c1bb5ea2c8f3706ed531c6cb5169cb"
host = "http://api.dama2.com:7766/app/" HOST = "http://api.dama2.com:7766/app/"
user = "test" USER = "test"
pwd = "test" PWD = "test"
[Dealer.autoifeng] [DEALER.AUTOIFENG]
username = "上海车城汽车" USERNAME = "上海车城汽车"
password = "Checheng235#" PASSWORD = "Checheng235#"
captchaType = "FOUR_CHARACTERS" CAPTCHA_TYPE = "FOUR_CHARACTERS"
[Dealer.easypass] [DEALER.EASYPASS]
username = "100074110" USERNAME = "100074110"
password = "hnmz147852." PASSWORD = "hnmz147852."
captchaType = "ARITHMETIC" CAPTCHA_TYPE = "ARITHMETIC"
[Dealer.autohome] [DEALER.AUTOHOME]
username = "河南明志汽车大迈8054" USERNAME = "河南明志汽车大迈8054"
password = "hnmz147852." PASSWORD = "hnmz147852."
captchaType = "FOUR_LETTERS" CAPTCHA_TYPE = "FOUR_LETTERS"
[Captcha.ruokuai] [CAPTCHA.RUOKUAI]
username = "chedev" USERNAME = "chedev"
password = "Checheng235#" PASSWORD = "Checheng235#"
softid = 86134 SOFTID = 86134
softkey = "b167ed636a224990a2f04ae2d28d1542" SOFTKEY = "b167ed636a224990a2f04ae2d28d1542"
[Captcha.ruokuai.typeids] [CAPTCHA.RUOKUAI.TYPEIDS]
FOUR_LETTERS = 2040 # 四个英文字母 FOUR_LETTERS = 2040 # 四个英文字母
FOUR_CHARACTERS = 3040 # 四个字母数字混合 FOUR_CHARACTERS = 3040 # 四个字母数字混合
ARITHMETIC = 7100 # 算术题 ARITHMETIC = 7100 # 算术题
...@@ -5,7 +5,7 @@ const captcha = exports; ...@@ -5,7 +5,7 @@ const captcha = exports;
captcha.decode = function (captchaType, imageFile, callback) { captcha.decode = function (captchaType, imageFile, callback) {
console.info('captchaType:', captchaType); console.info('captchaType:', captchaType);
console.info('imageFile:', imageFile); console.info('imageFile:', imageFile);
const typeids = config.get('Captcha.ruokuai.typeids'); const typeids = config.get('CAPTCHA.RUOKUAI.TYPEIDS');
typeid = typeids[captchaType]; typeid = typeids[captchaType];
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
ruokuai.decode(typeid, imageFile).then(function (captchaText) { ruokuai.decode(typeid, imageFile).then(function (captchaText) {
......
...@@ -10,13 +10,13 @@ const errors = require('../errors'); ...@@ -10,13 +10,13 @@ const errors = require('../errors');
const ruokuai = exports; const ruokuai = exports;
ruokuai.decode = async function (typeid, imageFile) { ruokuai.decode = async function (typeid, imageFile) {
const ruokuaiInfo = config.get('Captcha.ruokuai'); const ruokuaiInfo = config.get('CAPTCHA.RUOKUAI');
const formData = new FormData(); const formData = new FormData();
formData.append('username', ruokuaiInfo.username); formData.append('username', ruokuaiInfo.USERNAME);
formData.append('password', ruokuaiInfo.password); formData.append('password', ruokuaiInfo.PASSWORD);
formData.append('softid', ruokuaiInfo.softid); formData.append('softid', ruokuaiInfo.SOFTID);
formData.append('softkey', ruokuaiInfo.softkey); formData.append('softkey', ruokuaiInfo.SOFTKEY);
formData.append('typeid', typeid); formData.append('typeid', typeid);
formData.append("image", fs.createReadStream(imageFile), imageFile); formData.append("image", fs.createReadStream(imageFile), imageFile);
......
...@@ -9,12 +9,14 @@ ...@@ -9,12 +9,14 @@
* The Base Error all Carspider Errors inherit from. * The Base Error all Carspider Errors inherit from.
*/ */
class AppError extends Error { class AppError extends Error {
constructor(message) { constructor(message, trace = true) {
super(message); super(message);
this.name = this.constructor.name; this.name = this.constructor.name;
this.message = message; this.message = message;
if (trace) {
Error.captureStackTrace(this, this.constructor); Error.captureStackTrace(this, this.constructor);
} }
}
} }
exports.AppError = AppError; exports.AppError = AppError;
...@@ -23,7 +25,7 @@ exports.AppError = AppError; ...@@ -23,7 +25,7 @@ exports.AppError = AppError;
*/ */
class TryLoginError extends AppError { class TryLoginError extends AppError {
constructor(message) { constructor(message) {
super(message); super(message, false);
this.name = this.constructor.name; this.name = this.constructor.name;
this.message = message; this.message = message;
Error.captureStackTrace(this, this.constructor); Error.captureStackTrace(this, this.constructor);
...@@ -33,10 +35,14 @@ exports.TryLoginError = TryLoginError; ...@@ -33,10 +35,14 @@ exports.TryLoginError = TryLoginError;
class LoginError extends AppError { class LoginError extends AppError {
constructor(options) { constructor(options) {
if (typeof options === 'string') {
options = {message: options}
console.log('LoginError options:', options);
}
options = options || {}; options = options || {};
options.code = options.code || {}; options.code = options.code || {};
options.message = options.message || '登录失败: ' + options.code; options.message = options.message || '登录失败: ' + options.code;
super(options.message); super(options.message, false);
this.name = this.constructor.name; this.name = this.constructor.name;
this.message = options.message; this.message = options.message;
this.code = options.code; this.code = options.code;
......
'use strict';
const phantom = require('phantom');
const cheerio = require('cheerio');
class AutoIfeng {
constructor(params) {
this.params = params;
console.log(params);
}
execute() {
return new Promise((resolve, reject) => {
switch (this.params.task) {
case 'login': {
console.log('do login');
this.login();
break;
}
case 'tryLogin': {
break;
}
}
});
}
async login() {
const passport = this.params.username;
const password = this.params.password;
const instance = await phantom.create(['--ignore-ssl-errors=yes']);
const page = await instance.createPage();
page.property('viewportSize', {width: 1920, height: 1080});
await page.on("onResourceRequested", function(requestData) {
console.info('Requesting', requestData.url)
});
const status = await page.open('http://dealer.auto.ifeng.com/dealerback/dealerIndexPage');
console.log(status);
console.log(passport);
page.evaluate(function(passport, password) {
document.querySelector('input[id=passport]').value = passport;
document.querySelector('input[id=password]').value = password;
}).then(function(html) {
console.log(html);
});
const content = await page.property('content');
const $ = cheerio.load(content)
console.log('倒霉');
await page.render('m2.png');
await instance.exit();
}
}
module.exports = AutoIfeng;
\ No newline at end of file
'use strict';
const AutoHome = require('./AutoHome');
const EasyPass = require('./EasyPass');
// TODO: someday will finish this module
// const AutoIfeng = require('./AutoIfeng');
exports.AutoHome = AutoHome;
exports.EasyPass = EasyPass;
// exports.AutoIfeng = AutoIfeng;
\ No newline at end of file
'use strict';
const dealer = require('./dealer');
class Spider {
constructor(params) {
let self = this;
let _params = {};
this.params = Object.assign(_params, params);
this.spider = this.getSpider();
}
getSpider() {
let spider;
switch (this.params.webname) {
case 'DEALER.AUTOIFENG': {
spider = new dealer.AutoIfeng(this.params);
break;
}
case 'DEALER.AUTOHOME': {
spider = new dealer.AutoHome(this.params);
break;
}
case 'DEALER.EASYPASS': {
spider = new dealer.EasyPass(this.params);
break;
}
}
if (spider) {
return spider;
} else {
throw new Error('Spider not found');
}
}
execute() {
this.spider.execute();
}
}
module.exports = Spider;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment