From 342d5d628fd7db7be1683c2b721854b557765ecc Mon Sep 17 00:00:00 2001 From: shanmite <1971513084@qq.com> Date: Mon, 1 Feb 2021 13:10:13 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=B8=AD=E5=A5=96=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E5=8D=95=E7=8B=AC=E8=BF=90=E8=A1=8C;=20docs:=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=A3=80=E6=B5=8B=E4=B8=AD=E5=A5=96=E7=9A=84=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/check.yml | 93 +++++++++++++++++++++++++++++++++++++ README.md | 46 ++++++++++++++++-- lib/Base.js | 2 +- lib/BiliAPI.js | 55 +++++----------------- lib/Monitor.js | 6 +-- lib/lottery-in-nodejs.js | 19 ++++---- main.js | 28 +++++++---- package.json | 8 ++-- 8 files changed, 183 insertions(+), 74 deletions(-) create mode 100644 .github/workflows/check.yml diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..4fb7ac8 --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,93 @@ +name: 'Automatic check' +on: + workflow_dispatch: + branches: + - main + schedule: + - cron: '0 0 */2 * *' +jobs: + lottery_1: + runs-on: ubuntu-latest + steps: + - name: 'Checkout codes' + uses: actions/checkout@v2 + - name: 'Use Node.js' + uses: actions/setup-node@v1 + with: + node-version: '12.18.3' + - name: 'Run in Nodejs' + shell: bash + env: + NUMBER: 1 + COOKIE: ${{ secrets.COOKIE }} + SCKEY: ${{ secrets.SCKEY }} + run: + npm run check + lottery_2: + runs-on: ubuntu-latest + steps: + - name: 'Checkout codes' + uses: actions/checkout@v2 + - name: 'Use Node.js' + uses: actions/setup-node@v1 + with: + node-version: '12.18.3' + - name: 'Run in Nodejs' + shell: bash + env: + NUMBER: 2 + COOKIE: ${{ secrets.COOKIE2 }} + SCKEY: ${{ secrets.SCKEY }} + run: + npm run check + lottery_3: + runs-on: ubuntu-latest + steps: + - name: 'Checkout codes' + uses: actions/checkout@v2 + - name: 'Use Node.js' + uses: actions/setup-node@v1 + with: + node-version: '12.18.3' + - name: 'Run in Nodejs' + shell: bash + env: + NUMBER: 3 + COOKIE: ${{ secrets.COOKIE3 }} + SCKEY: ${{ secrets.SCKEY }} + run: + npm run check + lottery_4: + runs-on: ubuntu-latest + steps: + - name: 'Checkout codes' + uses: actions/checkout@v2 + - name: 'Use Node.js' + uses: actions/setup-node@v1 + with: + node-version: '12.18.3' + - name: 'Run in Nodejs' + shell: bash + env: + NUMBER: 4 + COOKIE: ${{ secrets.COOKIE4 }} + SCKEY: ${{ secrets.SCKEY }} + run: + npm run check + lottery_5: + runs-on: ubuntu-latest + steps: + - name: 'Checkout codes' + uses: actions/checkout@v2 + - name: 'Use Node.js' + uses: actions/setup-node@v1 + with: + node-version: '12.18.3' + - name: 'Run in Nodejs' + shell: bash + env: + NUMBER: 5 + COOKIE: ${{ secrets.COOKIE5 }} + SCKEY: ${{ secrets.SCKEY }} + run: + npm run check \ No newline at end of file diff --git a/README.md b/README.md index 6e32752..f777acb 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ +![Automatic check](https://github.com/shanmite/LotteryAutoScript/workflows/Automatic%20check/badge.svg) + + + ![Automatic clear dynamic&follow](https://github.com/shanmite/LotteryAutoScript/workflows/Automatic%20clear%20dynamic&follow/badge.svg) @@ -19,7 +23,9 @@ - [操作步骤](#操作步骤) - [Fork本仓库](#fork本仓库) - [填入COOKIE](#填入cookie) - - [微信推送中奖信息(可选)](#微信推送中奖信息可选) + - [检测中奖](#检测中奖) + - [手动检查](#手动检查) + - [微信推送(可选)](#微信推送可选) - [运行](#运行) - [完成!](#完成) - [清理动态](#清理动态) @@ -99,15 +105,22 @@ Chrome浏览器: ↓↓ -### 微信推送中奖信息(可选) +### 检测中奖 +每两个小时检测一次 +通过`@`信息判断 +#### 手动检查 +手动触发`Automatic check`工作流后可在日志中查看 +#### 微信推送(可选) 如果想使用Server酱提供的**微信推送**服务 > [Server酱是什么?](http://sc.ftqq.com/3.version) 可在 `Repository secrets` 中新建一个 `SCKEY` 并填入相应的值 -![new secret SCKEY](.github/secret2.png) +![new secret SCKEY](.github/secret2.png) + +**注意**Server酱有时会出错 ↓↓ @@ -182,6 +195,7 @@ Chrome浏览器: *添加更多的账号* 可在文件`.github/workflows/node.js.yml`中 +将以下代码中星号处改为数字并依次复制粘贴 ```yaml lottery_*: runs-on: ubuntu-latest @@ -202,9 +216,31 @@ steps: run: npm start ``` -将以上星号处改为数字并依次复制粘贴 此时`Secrets`里就可以添加更多的`COOKIE*`(简单的找规律问题) -若使用自动清理功能,还需再`.github/workflows/clear.yml`中 + +同理须在文件`.github/workflows/check.yml`中 +将以下代码中星号处改为数字并依次复制粘贴(启用对应的中奖检测) +```yaml +lottery_*: + runs-on: ubuntu-latest + steps: + - name: 'Checkout codes' + uses: actions/checkout@v2 + - name: 'Use Node.js' + uses: actions/setup-node@v1 + with: + node-version: '12.18.3' + - name: 'Run in Nodejs' + shell: bash + env: + NUMBER: * + COOKIE: ${{ secrets.COOKIE* }} + SCKEY: ${{ secrets.SCKEY }} + run: + npm run check +``` + +若使用**自动清理**功能,还需再`.github/workflows/clear.yml`中 ```yaml lottery_*: runs-on: ubuntu-latest diff --git a/lib/Base.js b/lib/Base.js index 68dec83..2ae7152 100644 --- a/lib/Base.js +++ b/lib/Base.js @@ -152,5 +152,5 @@ module.exports = { * @param {string} ts 13位时间戳 * @param {number} zoneOffset 东时区记做正数 */ - transformTimeZone: (ts, zoneOffset) => new Date(new Date(ts).getTime() + zoneOffset * 60 * 60 * 1000) + transformTimeZone: (ts, zoneOffset) => new Date(new Date(ts).getTime() + zoneOffset * 60 * 60 * 1000).toLocaleString() }; diff --git a/lib/BiliAPI.js b/lib/BiliAPI.js index 9c64d02..43fa7b9 100644 --- a/lib/BiliAPI.js +++ b/lib/BiliAPI.js @@ -28,16 +28,13 @@ const BiliAPI = { }, /** * 获取被at的信息 - * @returns { - Promise<{ - at_time: number; - id: number; - nickname: string; - business: string; - source_content: string; - url: string - }[] | []> - } + * @typedef AtInfo + * @property {number} at_time + * @property {string} up_uname + * @property {string} business + * @property {string} source_content + * @property {string} url + * @returns {Promise} */ getMyAtInfo: async () => { return new Promise((resolve) => { @@ -51,13 +48,12 @@ const BiliAPI = { const items = res.data.items; if (items.length !== 0) { items.forEach(i => { - const { at_time, id, item, user } = i, - { nickname } = user, - { business, uri: url, source_content } = item; + const { at_time, item, user } = i + , { nickname: up_uname } = user + , { business, uri: url, source_content } = item; atInfo.push({ at_time, - id, - nickname, + up_uname, business, source_content, url @@ -72,35 +68,6 @@ const BiliAPI = { }); }); }, - /** - * 删除at信息 - * @param {number} id - * @returns {Promise} - */ - delAtinfo: async (id) => { - return new Promise((resolve) => { - Ajax.post({ - url: 'https://api.bilibili.com/x/msgfeed/del', - hasCookies: true, - dataType: 'application/x-www-form-urlencoded', - data: { - tp: 2, - id, - csrf: GlobalVar.csrf - }, - success: responseText => { - let res = Base.strToJson(responseText); - if (res.code === 0) { - console.log('删除at信息成功'); - resolve(); - } else { - console.log(`删除at信息失败`); - resolve(); - } - } - }) - }); - }, /** * 获取关注列表 * @param {number} uid diff --git a/lib/Monitor.js b/lib/Monitor.js index a049bce..4253c4e 100644 --- a/lib/Monitor.js +++ b/lib/Monitor.js @@ -182,6 +182,9 @@ class Monitor extends Public { async go(option) { const { uid, dyid, type, rid, relay_chat, ctrl } = option; if (typeof dyid === 'string') { + if (typeof rid === 'string' && type !== 0) { + if (await BiliAPI.sendChat(rid, Base.getRandomStr(config.chat), type) === 0) return 0; + } if (!this.isAttentionErr) { uid.forEach(async (one_uid) => { if (typeof one_uid === 'number') { @@ -198,9 +201,6 @@ class Monitor extends Public { }) } if (this.isAttentionErr && uid.length !== 0) return 0; - if (typeof rid === 'string' && type !== 0) { - if (await BiliAPI.sendChat(rid, Base.getRandomStr(config.chat), type) === 0) return 0; - } if (await BiliAPI.autoRelay(GlobalVar.myUID, dyid, relay_chat, ctrl) === 0) return 0; if (await MyStorage.updateDyid((Array.from(new Set([dyid, ...this.AllMyLotteryInfo.split(',')]))).toString()) === -1) return 0; if (await BiliAPI.autolike(dyid) === 0) return 0; diff --git a/lib/lottery-in-nodejs.js b/lib/lottery-in-nodejs.js index 76cf20d..ffa6ec5 100644 --- a/lib/lottery-in-nodejs.js +++ b/lib/lottery-in-nodejs.js @@ -25,6 +25,7 @@ const ServerChan = (text, desp) => { }, success: responseText => { console.log(responseText); + if (JSON.parse(responseText).errno !== 0) throw new Error("ServerChan通知失败") } }) } @@ -56,26 +57,28 @@ function start() { * 是否中奖 */ async function isMe() { - const { SCKEY } = GlobalVar; - if (SCKEY === '') return; const arr = await BiliAPI.getMyAtInfo(); let desp = ''; if (arr.length !== 0) { arr.forEach(async e => { - const { at_time, id, nickname, business, source_content, url } = e + const { at_time, up_uname, business, source_content, url } = e if ((Date.now() / 1000 - at_time) / 3600 >= 2) { - await BiliAPI.delAtinfo(id); return; } desp += `发生时间: ${transformTimeZone(at_time * 1000, 8)} \n\n` - desp += `用户: ${nickname} \n\n` - desp += `在${business}中@了你 \n\n` + desp += `用户: ${up_uname} \n\n` + desp += `在${business}中@了你(${GlobalVar.myUID}) \n\n` desp += `原内容为: ${source_content} \n\n` desp += `[直达链接](${url}) \n\n` desp += `---\n\n` }); } - if (desp !== '') ServerChan('可能中奖了!', desp); + if (desp !== '') { + console.log(desp); + ServerChan('可能中奖了!', desp) + } else { + console.log("未中奖"); + } return; } @@ -89,7 +92,7 @@ async function checkCookie(num) { return true; } else { console.log('登录失败'); - ServerChan('动态抽奖出错', `COOKIE${num} 已失效`); + ServerChan('动态抽奖出错', `COOKIE${num} 已失效 UID:${GlobalVar.myUID}`); return false; } } diff --git a/main.js b/main.js index 2b0d501..051e891 100644 --- a/main.js +++ b/main.js @@ -9,16 +9,24 @@ const { NUMBER, CLEAR, COOKIE, SCKEY } = process.env; const { clear } = require("./lib/clear"); const isRight = await checkCookie(NUMBER); if (!isRight) return; - await isMe(); - if (process.argv.slice(2)[0] === 'clear') { - if (typeof CLEAR === 'string' && CLEAR === 'true') { - console.log('开始清理动态'); - await clear(); - console.log('清理动态完毕'); - } - } else { - console.log('开始参与抽奖'); - await start(); + switch (process.argv.slice(2)[0]) { + case 'start': + console.log('开始参与抽奖'); + await start(); + break; + case 'check': + console.log('检查是否中奖'); + await isMe(); + break; + case 'clear': + if (typeof CLEAR === 'string' && CLEAR === 'true') { + console.log('开始清理动态'); + await clear(); + console.log('清理动态完毕'); + } + break; + default: + break; } } }))(); diff --git a/package.json b/package.json index 4fcc69c..736a7df 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,12 @@ "description": "", "main": "main.js", "scripts": { - "start": "node main.js", + "start": "node main.js start", "clear": "node main.js clear", - "test_start": "env.bat && node main.js", - "test_clear": "env.bat && node main.js clear" + "check": "node main.js check", + "test_start": "env.bat && node main.js start", + "test_clear": "env.bat && node main.js clear", + "test_check": "env.bat && node main.js check" }, "repository": { "type": "git",