From ae3d3d139f3cf55a2cf010a10e6f421e646ad2d2 Mon Sep 17 00:00:00 2001 From: shanmite <1971513084@qq.com> Date: Mon, 3 May 2021 17:59:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9C=AC=E5=9C=B0=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- env.example.js | 76 ++++++++++++++++++++++++-------------- lib/Base.js | 10 +++-- lib/GithubAPI.js | 2 +- lib/MyStorage.js | 4 +- lib/eventBus.js | 25 ++++++++----- lib/lottery-in-nodejs.js | 37 +++++++++---------- lib/setVariable.js | 5 ++- main.js | 80 ++++++++++++++++++++++++---------------- 9 files changed, 143 insertions(+), 98 deletions(-) diff --git a/.gitignore b/.gitignore index 40b24ce..05a1743 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ node_modules/ lib/GlobalVar.json -lib/dyid.txt +lib/dyid*.txt dyid.zip env.js my_config.json \ No newline at end of file diff --git a/env.example.js b/env.example.js index 32df543..628a86e 100644 --- a/env.example.js +++ b/env.example.js @@ -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 = { diff --git a/lib/Base.js b/lib/Base.js index 2cc5932..3b4a53e 100644 --- a/lib/Base.js +++ b/lib/Base.js @@ -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' }) } }; diff --git a/lib/GithubAPI.js b/lib/GithubAPI.js index 9aa5bd8..9c34c64 100644 --- a/lib/GithubAPI.js +++ b/lib/GithubAPI.js @@ -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) diff --git a/lib/MyStorage.js b/lib/MyStorage.js index ef2b8b9..12fbfde 100644 --- a/lib/MyStorage.js +++ b/lib/MyStorage.js @@ -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() diff --git a/lib/eventBus.js b/lib/eventBus.js index a0d8f30..1fb38ac 100644 --- a/lib/eventBus.js +++ b/lib/eventBus.js @@ -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; diff --git a/lib/lottery-in-nodejs.js b/lib/lottery-in-nodejs.js index 114bce4..62a6383 100644 --- a/lib/lottery-in-nodejs.js +++ b/lib/lottery-in-nodejs.js @@ -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) { diff --git a/lib/setVariable.js b/lib/setVariable.js index 6b1f732..caa17db 100644 --- a/lib/setVariable.js +++ b/lib/setVariable.js @@ -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 } diff --git a/main.js b/main.js index ad67d4e..6f0eedb 100644 --- a/main.js +++ b/main.js @@ -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) -}))(); +})() \ No newline at end of file