diff --git a/.github/behind.png b/.github/behind.png new file mode 100644 index 0000000..5d49dd0 Binary files /dev/null and b/.github/behind.png differ diff --git a/.github/workflows/clear.yml b/.github/workflows/clear.yml new file mode 100644 index 0000000..405139d --- /dev/null +++ b/.github/workflows/clear.yml @@ -0,0 +1,24 @@ +name: 'Automatic clear dynamic&follow' +on: + workflow_dispatch: + branches: + - main + schedule: + - cron: '0 0 */15 * *' +jobs: + clear: + 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: + COOKIE_1: ${{ secrets.COOKIE }} + SCKEY: ${{ secrets.SCKEY }} + run: + npm run clear \ No newline at end of file diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index c36ef7b..1ccfe97 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -24,7 +24,7 @@ jobs: COOKIE_1: ${{ secrets.COOKIE }} SCKEY: ${{ secrets.SCKEY }} run: - node main.js + npm start lottery_2: runs-on: ubuntu-latest steps: @@ -40,7 +40,7 @@ jobs: COOKIE_2: ${{ secrets.COOKIE2 }} SCKEY: ${{ secrets.SCKEY }} run: - node main.js + npm start lottery_3: runs-on: ubuntu-latest steps: @@ -56,7 +56,7 @@ jobs: COOKIE_3: ${{ secrets.COOKIE3 }} SCKEY: ${{ secrets.SCKEY }} run: - node main.js + npm start lottery_4: runs-on: ubuntu-latest steps: @@ -72,7 +72,7 @@ jobs: COOKIE_4: ${{ secrets.COOKIE4 }} SCKEY: ${{ secrets.SCKEY }} run: - node main.js + npm start lottery_5: runs-on: ubuntu-latest steps: @@ -88,4 +88,4 @@ jobs: COOKIE_5: ${{ secrets.COOKIE5 }} SCKEY: ${{ secrets.SCKEY }} run: - node main.js + npm start diff --git a/README.md b/README.md index 4d879aa..0c217c6 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,10 @@ ## 动态抽奖 通过Github Actions挂载Nodejs脚本 -> [Actions官方文档](https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions) + + > [Actions官方文档](https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions) + +此脚本将在B站专栏草稿中储存信息 --- @@ -90,8 +93,11 @@ Chrome浏览器: ## 其他细节 - 更新 - > [如何同步更新Github上Fork的项目](https://www.cnblogs.com/idyllcheung/p/13555934.html) -- 支持最多5个账号 + 如果出现 + ![滞后](.github/behind.png) + 说明此脚本有更新 + 自行搜索`如何同步更新Github上Fork的项目` +- 默认支持5个账号 | cookies | value | | --------- | ----- | | `COOKIE` | 值 | @@ -99,15 +105,36 @@ Chrome浏览器: | `COOKIE3` | 值 | | `COOKIE4` | 值 | | `COOKIE5` | 值 | - + 也可在`.github/workflows/node.js.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: + COOKIE_*: ${{ secrets.COOKIE* }} + SCKEY: ${{ secrets.SCKEY }} + run: + npm start + ``` + 将以上星号处改为数字并依次复制粘贴(简单的找规律问题) - 部分设置说明 - 定时运行(`UTC`时间) + `.github/workflows/node.js.yml` ```yaml schedule: - cron: '0 */2 * * *' ``` [填写格式](https://crontab.guru/) - 模式选择 + `lib/config.js` ```javascript /** * 默认设置 diff --git a/lib/BiliAPI.js b/lib/BiliAPI.js index 2a07547..8d6fec9 100644 --- a/lib/BiliAPI.js +++ b/lib/BiliAPI.js @@ -555,6 +555,40 @@ const BiliAPI = { }); }); }, + /** + * 获取一个分区中50个的id + * @param {number} tagid + * @param {number} n 1-> + * @returns {Promise} + */ + getPartitionUID: (tagid, n) => { + return new Promise((resolve) => { + Ajax.get({ + url: 'https://api.bilibili.com/x/relation/tag', + queryStringsObj: { + mid: GlobalVar.myUID, + tagid: tagid, + pn: n, + ps: 50 + }, + hasCookies: true, + success: responseText => { + const res = Base.strToJson(responseText); + let uids = []; + if (res.code === 0) { + res.data.forEach(d => { + uids.push(d.mid); + }) + console.log(`[获取分组${tagid}]成功获取取关分区列表${n}`); + resolve(uids) + } else { + console.warn(`[获取分组]获取取关分区列表失败\n${responseText}`); + resolve(uids) + } + } + }) + }); + }, /** * 获取草稿列表 * @typedef Page diff --git a/lib/MyStorage.js b/lib/MyStorage.js index 849f8a6..b1531e3 100644 --- a/lib/MyStorage.js +++ b/lib/MyStorage.js @@ -49,13 +49,25 @@ async function updateDyid(dyid) { if (typeof aid === 'number' && aid !== -1) { if (_dyid === '' && dyid === '') return; console.log(`[更新专栏草稿]id:${aid}`); - await BiliAPI.addDraft(_dyid + dyid,aid); + await BiliAPI.addDraft(_dyid + dyid, aid); } } +/** + * 清空dyid + * @param {string} dyid + * @returns + */ +async function clearDyid() { + const draftId = await getDraftid(); + console.log(`[清空专栏草稿]id:${draftId}`); + await BiliAPI.addDraft('', draftId); +} + const MyStorage = { getDyid, - updateDyid + updateDyid, + clearDyid }; module.exports = MyStorage; \ No newline at end of file diff --git a/lib/clear.js b/lib/clear.js new file mode 100644 index 0000000..b9401b2 --- /dev/null +++ b/lib/clear.js @@ -0,0 +1,56 @@ +const Base = require("./Base"); +const { checkMyPartition } = require("./BiliAPI"); +const BiliAPI = require("./BiliAPI"); +const Public = require("./Public"); +const GlobalVar = require('./GlobalVar.json'); +const { clearDyid } = require("./MyStorage"); + +let offset = '0'; +async function delDynamic() { + for (let index = 0; index < 1000; index++) { + const { allModifyDynamicResArray, offset: _offset } = await Public.prototype.checkAllDynamic(GlobalVar.myUID, 1, 5 * 1000, offset); + offset = _offset; + if (index < 2) { + console.log(`跳过第${index + 1}页(12条)`); + } else { + console.log(`开始读取第${index + 1}页(12条)`); + for (let index = 0; index < allModifyDynamicResArray.length; index++) { + const res = allModifyDynamicResArray[index]; + const { type, dynamic_id } = res; + if (type === 1) BiliAPI.rmDynamic(dynamic_id); + await Base.delay(8 * 1000); + } + console.log(`第${index}页中的转发动态全部删除成功`) + } + if (offset === '0') break; + } +} +async function unFollow() { + const tagid = await checkMyPartition(); + if (tagid === 0) { console.log('未能成功获取关注分区id'); return } + let rmup = []; + for (let index = 1; index < 42; index++) { + const uids = await BiliAPI.getPartitionUID(tagid, index); + await Base.delay(5 * 1000); + rmup.push(...uids); + if (uids.length === 0) break; + } + for (let index = 0; index < rmup.length; index++) { + const uid = rmup[index]; + BiliAPI.cancelAttention(uid); + await Base.delay(8 * 1000); + } +} + + +/** + * 清理动态和关注 + */ +async function clear() { + await delDynamic(); + await unFollow(); + await clearDyid(); + return; +} + +module.exports = { clear } \ No newline at end of file diff --git a/main.js b/main.js index d206faf..59fd2ae 100644 --- a/main.js +++ b/main.js @@ -16,9 +16,18 @@ COOKIE_ARR.forEach(async (cookie, num) => { if (typeof cookie === 'string' && cookie.length > 10) { await setGlobalVar(cookie, SCKEY); const { start, isMe, checkCookie } = require("./lib/lottery-in-nodejs"); + const { clear } = require("./lib/clear"); const isRight = await checkCookie(num + 1); if (!isRight) return; await isMe(); - await start(); + if (process.argv.slice(2)[0] === 'clear') { + console.log('开始清理动态'); + await clear(); + console.log('清理动态完毕'); + } else { + console.log('开始参与抽奖'); + await start(); + console.log('参与抽奖结束'); + } } }); diff --git a/package.json b/package.json index 32fd832..a6d8fe5 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "main": "main.js", "scripts": { "start": "node main.js", - "test": "env.bat && node main.js" + "clear": "node main.js clear", + "test_start": "env.bat && node main.js", + "test_clear": "env.bat && node main.js clear" }, "repository": { "type": "git",