Commit 2a7202c0 by 马玮

Refactor cookies handle

parent ab6e1e62
......@@ -30,6 +30,8 @@ sudo yum install redis
- [ ] 功能:增加日志系统
- [ ] 功能:增加随机userAgent
- [ ] 功能:增加测试用例和工具
- [ ] 功能:通过环境变量设定根目录
- [ ] 优化:cookie使用redis做一级缓存,缓存不存在时再从db读取
- [ ] 优化:整理所有错误,建立错误继承关系,区分操作错误和程序错误
- [ ] 优化:易车抓取方式尝试破解request,phantomjs方案有点过于缓慢
- [ ] 优化:汽车之家发起request前先获取接口链接,通过url解析修改参数获取正确接口以保证稳定性
......
const errors = require('../errors');
const redis = require('redis');
const moment = require('moment');
const errors = require('../errors');
const Promise = require('bluebird');
const poolCluster = require('../database/poolCluster');
Promise.promisifyAll(redis);
class DealerHelper {
static async getCookies(params) {
try {
const client = await redis.createClient();
const cookies_key = params.webname + '_cookies';
let cookies = JSON.parse(await client.getAsync(cookies_key));
await cookies.forEach((cookie) => {
cookie.expires = (new Date()).getTime() + (1000 * 60 * 60);
});
await client.quit();
return cookies;
} catch (e) {
return null;
}
}
static async saveCookies(params) {
try {
const cookie_key = params.webname + '_cookies';
const client = await redis.createClient();
await client.set(cookie_key, JSON.stringify(params.cookies), redis.print);
await client.quit();
console.info('cookie保存成功');
} catch (e) {
return e;
}
}
// 保存线索
static async saveClues(params) {
const userid = params.userid || 0;
......
......@@ -24,21 +24,6 @@ class AutoHome {
this.params = params;
}
async getCookies() {
try {
const client = await redis.createClient();
const cookies_key = this.params.webname + '_cookies';
let cookies = JSON.parse(await client.getAsync(cookies_key));
await cookies.forEach((cookie) => {
cookie.expires = (new Date()).getTime() + (1000 * 60 * 60);
});
await client.quit();
return cookies;
} catch (e) {
return null;
}
}
async execute() {
switch (this.params.task) {
case 'login': {
......@@ -58,7 +43,7 @@ class AutoHome {
async tryLogin() {
try {
let cookies = await this.getCookies();
let cookies = await DealerHelper.getCookies(this.params);
let loginSuccess = false;
if (cookies) {
......@@ -80,6 +65,9 @@ class AutoHome {
}
if (loginSuccess) {
if (this.params.task === 'tryLogin') {
console.log('尝试登陆成功');
}
return true;
} else {
throw new errors.TryLoginError('尝试登录失败');
......@@ -162,15 +150,11 @@ class AutoHome {
if (loginSuccess) {
console.log('登录成功!');
const cookies = await horseman .cookies().close();
const cookies = await horseman .cookies();
const params = this.params;
params.cookies = cookies;
console.log(JSON.stringify(cookies));
const cookie_key = this.params.webname + '_cookies';
const client = await redis.createClient();
await client.set(cookie_key, JSON.stringify(cookies), redis.print);
await client.quit();
console.info('cookie保存成功');
await DealerHelper.saveCookies(params);
}
await horseman.close();
......@@ -196,7 +180,7 @@ class AutoHome {
return;
}
const cookies = await this.getCookies();
const cookies = await DealerHelper.getCookies(this.params);
// console.log(JSON.stringify(cookies));return;
const cookieArray = [];
......
......@@ -24,21 +24,6 @@ class EasyPass {
this.params = params;
}
async getCookies() {
try {
const client = await redis.createClient();
const cookies_key = this.params.webname + '_cookies';
let cookies = JSON.parse(await client.getAsync(cookies_key));
await cookies.forEach((cookie) => {
cookie.expires = (new Date()).getTime() + (1000 * 60 * 60);
});
await client.quit();
return cookies;
} catch (e) {
return null;
}
}
async execute() {
switch (this.params.task) {
case 'login': {
......@@ -58,7 +43,7 @@ class EasyPass {
async tryLogin() {
try {
let cookies = await this.getCookies();
let cookies = await DealerHelper.getCookies(this.params);
let loginSuccess = false;
if (cookies) {
......@@ -80,6 +65,9 @@ class EasyPass {
}
if (loginSuccess) {
if (this.params.task === 'tryLogin') {
console.log('尝试登陆成功');
}
return true;
} else {
throw new errors.TryLoginError('尝试登录失败');
......@@ -161,15 +149,11 @@ class EasyPass {
if (loginSuccess) {
console.log('登录成功!');
const cookies = await horseman .cookies().close();
const cookies = await horseman .cookies();
const params = this.params;
params.cookies = cookies;
console.log(JSON.stringify(cookies));
const cookie_key = this.params.webname + '_cookies';
const client = await redis.createClient();
await client.set(cookie_key, JSON.stringify(cookies), redis.print);
await client.quit();
console.info('cookie保存成功');
await DealerHelper.saveCookies(params);
}
await horseman.close();
if (loginSuccess) {
......@@ -194,7 +178,7 @@ class EasyPass {
return;
}
const cookies = await this.getCookies();
const cookies = await DealerHelper.getCookies(this.params);
if (cookies) {
const horseman = new Horseman({
......
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