Skip to content

Commit

Permalink
perf: 重构代码
Browse files Browse the repository at this point in the history
- 分离动态筛选部分的网络请求和数据处理
- 优化部分ifelse判断
feat:
- 新增NOT_GO_LOTTERY环境变量
- 边转边存dyid
- 滤除的dyid也进行存储
fix: 修复部分bug
  • Loading branch information
shanmite committed Jul 17, 2021
1 parent 266a657 commit 18760eb
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 168 deletions.
8 changes: 5 additions & 3 deletions env.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
*/

/**
* ## 账号相关参数
* ## 账号相关
* - `COOKIE` 是必填项
* - `NUMBER` 表示是第几个账号
* - `CLEAR` 是否启用清理功能
* - `ENABLE_MULTIPLE_ACCOUNT` 是否启用多账号
* - `MULTIPLE_ACCOUNT_PARM` 多账号参数(JSON格式)
* ## 调试相关
* - `LOTTERY_LOG_LEVEL` 输出日志等级 Error<Warn<Info<Debug 1<2<3<4
*
* - `NOT_GO_LOTTERY` 关闭抽奖行为 为空字符即关
* ## 多账号
* 1. 将 ENABLE_MULTIPLE_ACCOUNT 的值改为true
* 2. 将账号信息依次填写于 multiple_account_parm 中, 参考例子类推
Expand All @@ -26,7 +27,8 @@ const account_parm = {
CLEAR: true,
ENABLE_MULTIPLE_ACCOUNT: false,
MULTIPLE_ACCOUNT_PARM: "",
LOTTERY_LOG_LEVEL: 3
LOTTERY_LOG_LEVEL: 3,
NOT_GO_LOTTERY: ''
}

/**
Expand Down
16 changes: 15 additions & 1 deletion lib/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ const Base = {
})(params);
return isJSON ? isJSON : {}
},
/**
* @template T
* @param {Array<T>} iter
* @param {(value: T) => Promise<Boolean>} fn 返回true整体退出
*/
async try_for_each(iter, fn) {
for (const item of iter) {
if (await fn(item)) break
}
},
/**
* 函数柯里化
* @param {function} func
Expand All @@ -58,6 +68,7 @@ const Base = {
* @returns {Promise<void>}
*/
delay(time) {
Base.log.info('时延', `${time}ms`);
return new Promise(resolve => {
setTimeout(() => {
resolve();
Expand Down Expand Up @@ -93,6 +104,9 @@ const Base = {
}
return RandomOne
},
hasEnv(env_name) {
return process.env[env_name] ? true : false;
},
/**日志 */
log: {
level: 0,
Expand All @@ -113,7 +127,7 @@ const Base = {
debug(context, msg) {
if (this.level > 3) {
if (msg instanceof Object) msg = JSON.stringify(msg, null, 4);
this.proPrint([`[${Date()}]`, chalk.grey("[Debug]"), chalk.hex('#FFA500')(`[${context}]`), '\n', chalk.hex('#0070BB')(`[${msg}]`)])
this.proPrint([`[${Date()}]`, chalk.grey("[Debug]"), chalk.hex('#FFA500')(`[${context}]`), chalk.hex('#0070BB')(`[\n${msg}\n]`)])
}
},
info(context, msg) {
Expand Down
4 changes: 2 additions & 2 deletions lib/BiliAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ const BiliAPI = {
success: responseText => {
const res = strToJson(responseText);
if (res.code === 0) {
resolve(res.data.follower);
log.info('获取粉丝数', 'ok');
resolve(res.data.follower);
} else {
log.error('获取粉丝数', `出错 可能是访问过频繁\n${responseText}`);
resolve(-1);
Expand Down Expand Up @@ -389,7 +389,7 @@ const BiliAPI = {
isMe: isMe
});
} else {
log.info('获取开奖信息', `失败\n${responseText}`);
log.error('获取开奖信息', `失败\n${responseText}`);
resolve({
ts: -1,
text: '获取开奖信息失败',
Expand Down
Loading

0 comments on commit 18760eb

Please sign in to comment.