feat: 本地运行支持多账号

This commit is contained in:
shanmite 2021-05-03 17:59:07 +08:00
parent 6bb88c48d0
commit ae3d3d139f
9 changed files with 143 additions and 98 deletions

2
.gitignore vendored
View File

@ -1,6 +1,6 @@
node_modules/
lib/GlobalVar.json
lib/dyid.txt
lib/dyid*.txt
dyid.zip
env.js
my_config.json

View File

@ -7,41 +7,63 @@
* `COOKIE` 是必填项
* `NUMBER` 表示是第几个账号
* `PAT` `GITHUB_REPOSITORY` 两项是为了从构件中下载已转发信息 可不填
*
* 多账号
* ENABLE_MULTIPLE_ACCOUNT 的值改为true
* 将账号信息依次填写于 MULTIPLE_ACCOUNT , 参考例子类推
* `WAIT` 表示下一个账号运行等待时间(毫秒)
*/
const account_parm = {
"COOKIE": "",
"NUMBER": 1,
"CLEAR": true,
"LOCALLAUNCH": true,
"PAT": "",
"GITHUB_REPOSITORY": "用户名/仓库名"
COOKIE: "",
NUMBER: 1,
CLEAR: true,
LOCALLAUNCH: true,
PAT: "",
GITHUB_REPOSITORY: "用户名/仓库名",
ENABLE_MULTIPLE_ACCOUNT: false,
MULTIPLE_ACCOUNT: JSON.stringify([
{
COOKIE: "",
NUMBER: 1,
CLEAR: true,
LOCALLAUNCH: true,
WAIT: 60 * 1000,
},
// {
// COOKIE: "",
// NUMBER: 2,
// CLEAR: true,
// LOCALLAUNCH: true,
// WAIT: 60 * 1000,
// }
])
}
/**
* 推送相关参数
*/
const push_parm = {
"SCKEY=": "",
"SENDKEY": "",
"QQ_SKEY": "",
"QQ_MODE": "",
"BARK_PUSH": "",
"BARK_SOUND": "",
"TG_BOT_TOKEN": "",
"TG_USER_ID": "",
"TG_PROXY_HOST": "",
"TG_PROXY_PORT": "",
"DD_BOT_TOKEN": "",
"DD_BOT_SECRET": "",
"QYWX_KEY": "",
"IGOT_PUSH_KEY": "",
"PUSH_PLUS_TOKEN": "",
"PUSH_PLUS_USER": "",
"SMTP_HOST": "",
"SMTP_PORT": "",
"SMTP_USER": "",
"SMTP_PASS": "",
"SMTP_TO_USER": ""
SCKEY: "",
SENDKEY: "",
QQ_SKEY: "",
QQ_MODE: "",
BARK_PUSH: "",
BARK_SOUND: "",
TG_BOT_TOKEN: "",
TG_USER_ID: "",
TG_PROXY_HOST: "",
TG_PROXY_PORT: "",
DD_BOT_TOKEN: "",
DD_BOT_SECRET: "",
QYWX_KEY: "",
IGOT_PUSH_KEY: "",
PUSH_PLUS_TOKEN: "",
PUSH_PLUS_USER: "",
SMTP_HOST: "",
SMTP_PORT: "",
SMTP_USER: "",
SMTP_PASS: "",
SMTP_TO_USER: ""
}
process.env = {

View File

@ -200,18 +200,20 @@ const Base = {
},
/**
* 读取dyid文件
* @param {number} num
* @returns {fs.ReadStream}
*/
readDyidFile: () => {
const fpath = path.join('./lib', 'dyid.txt');
readDyidFile: (num) => {
const fpath = num < 2 ? path.join('./lib', 'dyid.txt') : path.join('./lib', `dyid${num}.txt`);
return fs.createReadStream(fpath, { encoding: 'utf8', highWaterMark: 19 * 1000 })
},
/**
* 追加dyid
* @param {number} num
* @returns {fs.WriteStream}
*/
writeDyidFile: () => {
const fpath = path.join('./lib', 'dyid.txt');
writeDyidFile: (num) => {
const fpath = num < 2 ? path.join('./lib', 'dyid.txt') : path.join('./lib', `dyid${num}.txt`);
return fs.createWriteStream(fpath, { flags: 'a' })
}
};

View File

@ -71,7 +71,7 @@ const GihubAPI = {
wtbs.on('finish', () => {
tooltip.log('下载完成开始解压')
createReadStream('dyid.zip').pipe(unzip.Extract({
path: 'lib'
path: 'lib',
}).on('close', () => {
tooltip.log('解压完成')
resolve(true)

View File

@ -33,7 +33,7 @@ const MyStorage = {
searchDyid: (dyid) => {
return new Promise((resolve) => {
const Rdyid = new RegExp(dyid);
const rs = Base.readDyidFile();
const rs = Base.readDyidFile(Number(process.env.NUMBER));
let status = false;
rs.on('data', chunk => {
if (Rdyid.test(chunk)) {
@ -56,7 +56,7 @@ const MyStorage = {
updateDyid: (dyid) => {
Base.tooltip.log('写入已转发过的动态信息');
return new Promise((resolve) => {
const ws = Base.writeDyidFile();
const ws = Base.writeDyidFile(Number(process.env.NUMBER));
ws.write(dyid + ',', () => {
ws.destroy();
resolve()

View File

@ -1,17 +1,24 @@
const { EventEmitter } = require('events');
const eTarget = new EventEmitter();
/**
* 事件总线
*/
module.exports = {
on: (type, fn) => {
eTarget.addListener(type, fn);
const eventBus = {
ee: new EventEmitter(),
event_list: [],
on(event, fn) {
this.ee.addListener(event, fn);
this.event_list.push(event);
},
emit: (type) => {
eTarget.emit(type);
emit(event) {
this.ee.emit(event);
},
off: () => {
eTarget.off();
flush() {
this.event_list.forEach(event => {
this.ee.removeAllListeners(event)
})
}
};
}
module.exports = eventBus;

View File

@ -11,6 +11,21 @@ const config = require('./config');
/** Github运行限制时间*/
const MAX_TIME = 6 * 60 * 60 * 1000 - 6 * 60 * 1000;
async function createRandomDynamic(num) {
Base.tooltip.log(`准备创建${num}条随机动态`);
if (config.create_dy === '1') {
const Dynamic = await Public.prototype.checkAllDynamic(GlobalVar.myUID, 1);
if ((Dynamic.allModifyDynamicResArray[0] || { type: 1 }).type === 1) {
for (let index = 0; index < num; index++) {
await BiliAPI.createDynamic(Base.getRandomStr(config.dy_contents));
await Base.delay(2000);
}
} else {
Base.tooltip.log('已有非抽奖动态故无需创建');
}
}
}
/**
* 主函数
* @param {string} cookie
@ -18,25 +33,6 @@ const MAX_TIME = 6 * 60 * 60 * 1000 - 6 * 60 * 1000;
*/
function start() {
return new Promise(resolve => {
function createRandomDynamic(num) {
Base.tooltip.log(`准备创建${num}条随机动态`);
return new Promise((_resolve) => {
if (config.create_dy === '1') {
Public.prototype.checkAllDynamic(GlobalVar.myUID, 1).then(async Dynamic => {
if ((Dynamic.allModifyDynamicResArray[0] || { type: 1 }).type === 1) {
for (let index = 0; index < num; index++) {
await BiliAPI.createDynamic(Base.getRandomStr(config.dy_contents));
await Base.delay(2000);
}
_resolve()
} else {
Base.tooltip.log('已有非抽奖动态故无需创建');
_resolve()
}
})
}
});
}
let times = Base.counter();
/* 注册事件 */
eventBus.on('Turn_on_the_Monitor', async () => {
@ -55,11 +51,12 @@ function start() {
}
const lottery = GlobalVar.Lottery[times.next()];
const nlottery = Number(lottery);
(new Monitor(isNaN(nlottery) ? lottery : nlottery)).init();
await (new Monitor(isNaN(nlottery) ? lottery : nlottery)).init();
});
eventBus.on('Turn_off_the_Monitor', async () => {
await createRandomDynamic(config.create_dy_num);
Base.tooltip.log('[运行结束]程序自动关闭');
eventBus.flush();
resolve();
})
if (process.env.LOCALLAUNCH) {

View File

@ -18,8 +18,9 @@ let GlobalVar = {
/**
* 生成全局变量文件
* @param {string} cookie
* @param {number} n
*/
async function setVariable(cookie) {
async function setVariable(cookie, n) {
if (cookie) {
const key = ['DedeUserID','bili_jct']
GlobalVar.cookie = cookie;
@ -35,7 +36,7 @@ async function setVariable(cookie) {
if (process.env.PAT) {
await MyStorage.init()
} else {
await Base.createFile('dyid.txt', '', 'a')
await Base.createFile(n < 2 ? 'dyid.txt' : `dyid${n}.txt`, '', 'a')
}
return
}

80
main.js
View File

@ -1,4 +1,4 @@
const { tooltip } = require("./lib/Base");
const { tooltip, delay } = require("./lib/Base");
try {
require("./env");
@ -6,39 +6,55 @@ try {
tooltip.log("无env.js文件");
}
const { setVariable } = require("./lib/setVariable");
const { start, isMe, checkCookie } = require("./lib/lottery-in-nodejs");
const { clear } = require("./lib/clear");
((async () => {
const { NUMBER, CLEAR, COOKIE, PAT, LOCALLAUNCH } = process.env;
if (COOKIE) {
if (!LOCALLAUNCH && !PAT) {
tooltip.log('请查看README文件, 填入相应的PAT');
return;
}
await setVariable(COOKIE);
if (await checkCookie(NUMBER)) {
switch (process.argv.slice(2)[0]) {
case 'start':
tooltip.log('开始参与抽奖');
await start();
break;
case 'check':
tooltip.log('检查是否中奖');
await isMe();
break;
case 'clear':
if (CLEAR) {
tooltip.log('开始清理动态');
await clear();
tooltip.log('清理动态完毕');
async function main() {
const { COOKIE, NUMBER, CLEAR, PAT, LOCALLAUNCH, ENABLE_MULTIPLE_ACCOUNT, MULTIPLE_ACCOUNT } = process.env;
if (LOCALLAUNCH || PAT) {
if (ENABLE_MULTIPLE_ACCOUNT) {
let muti_acco = JSON.parse(MULTIPLE_ACCOUNT);
process.env.ENABLE_MULTIPLE_ACCOUNT = '';
for (const acco of muti_acco) {
process.env.COOKIE = acco.COOKIE;
process.env.NUMBER = acco.NUMBER;
process.env.CLEAR = acco.CLEAR;
await main();
await delay(acco.WAIT);
}
} else {
if (COOKIE) {
const { setVariable } = require("./lib/setVariable");
await setVariable(COOKIE, Number(NUMBER));
const { start, isMe, checkCookie } = require("./lib/lottery-in-nodejs");
const { clear } = require("./lib/clear");
tooltip.log('[LotteryAutoScript] 账号' + NUMBER);
if (await checkCookie(NUMBER)) {
switch (process.argv.slice(2)[0]) {
case 'start':
tooltip.log('开始参与抽奖');
await start();
break;
case 'check':
tooltip.log('检查是否中奖');
await isMe();
break;
case 'clear':
if (CLEAR) {
tooltip.log('开始清理动态');
await clear();
tooltip.log('清理动态完毕');
}
break;
default:
break;
}
break;
default:
break;
}
}
}
} else {
tooltip.log('请查看README文件, 填入相应的PAT');
}
}
(async function () {
await main();
process.exit(0)
}))();
})()