From 6bbbfbb5217a68c056369ea315108c0840937f73 Mon Sep 17 00:00:00 2001 From: shanmite Date: Thu, 29 Jul 2021 12:57:14 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E4=BC=98=E5=8C=96=E6=B8=85=E7=90=86?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E7=9A=84=E4=BD=BF=E7=94=A8=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/BiliAPI.js | 8 ++--- lib/Public.js | 7 ++--- lib/Util.js | 9 ++++++ lib/clear.js | 66 +++++++++++++++++++++++++--------------- lib/lottery-in-nodejs.js | 17 ++++++----- main.js | 2 +- script/pkg/pkg.ps1 | 7 ++++- 7 files changed, 73 insertions(+), 43 deletions(-) diff --git a/lib/BiliAPI.js b/lib/BiliAPI.js index 759d31e..2748b9b 100644 --- a/lib/BiliAPI.js +++ b/lib/BiliAPI.js @@ -582,10 +582,10 @@ const BiliAPI = { success: responseText => { const res = strToJson(responseText); if (res.code === 0) { - log.info('自动取关', '取关成功'); + log.info('自动取关', `取关成功(${uid})`); resolve(true) } else { - log.error('自动取关', `取关失败\n${responseText}`); + log.error('自动取关', `取关失败(${uid})\n${responseText}`); resolve(false) } } @@ -722,10 +722,10 @@ const BiliAPI = { retry: false, success: responseText => { if (/^{"code":0/.test(responseText)) { - log.info('删除动态', '成功删除一条动态'); + log.info('删除动态', `成功删除一条动态(${dyid})`); resolve(true); } else { - log.error('删除动态', `删除动态失败\n${responseText}`); + log.error('删除动态', `删除动态失败(${dyid})\n${responseText}`); resolve(false); } } diff --git a/lib/Public.js b/lib/Public.js index e3f15c4..edbe74a 100644 --- a/lib/Public.js +++ b/lib/Public.js @@ -143,7 +143,7 @@ class Public { */ /** * 检查指定用户的所有的动态信息 - * @param {string} UID 指定的用户UID + * @param {number} hostuid 指定的用户UID * @param {number} pages 读取页数 * @param {number} time 时延 * @param {string} [offset] 默认'0' @@ -188,19 +188,18 @@ class Public { if (nextinfo.has_more === 0) { offset = nextinfo.next_offset; - log.info('检查所有动态', `成功读取${i + 1}页信息(已经是最后一页了故无法读取更多)`); + log.info('检查所有动态', `已经是最后一页了故无法读取更多`); break; } else { /**合并 */ allModifyDynamicResArray.push.apply(allModifyDynamicResArray, mDRArry); - log.info('检查所有动态', `开始读取第${i + 2}页动态信息`) offset = nextinfo.next_offset; } await Util.delay(time); } - log.info('检查所有动态', `第${pages}页信息读取完成`) + log.info('检查所有动态', `${pages}页信息读取完成`) return ({ allModifyDynamicResArray, offset }); } diff --git a/lib/Util.js b/lib/Util.js index da6d914..419814c 100644 --- a/lib/Util.js +++ b/lib/Util.js @@ -93,6 +93,15 @@ const Util = { } return c }, + /** + * 无限序列 + * `[0..]` + */ + *infiniteNumber() { + for (let index = 0; ; index++) { + yield index + } + }, /** * 随机获取数组中的一个元素 * @template T diff --git a/lib/clear.js b/lib/clear.js index fc9aaae..e791c97 100644 --- a/lib/clear.js +++ b/lib/clear.js @@ -1,12 +1,9 @@ -const { log, delay } = require("./Util"); +const { log, delay, infiniteNumber } = require("./Util"); const BiliAPI = require("./BiliAPI"); const Public = require("./Public"); const GlobalVar = require('./GlobalVar'); const config = require("./config"); -/**动态偏移量 */ -let offset = '0'; - /** * 获取关注分区里的uid * @returns { Promise } @@ -36,31 +33,47 @@ async function clear() { const { clear_white_list, clear_max_day, clear_remove_dynamic, clear_remove_attention, clear_remove_delay, clear_dynamic_type } = config; let success = true; const uid_list = await getFollowList(); + if (!uid_list.length) { + log.info('清理关注', `关注为空`) + } else { + log.info('清理关注', `共有${uid_list.length}个关注`) + } if (!clear_remove_dynamic && clear_remove_attention) { + log.info('清理关注', '进入只清理关注模式') /* 专清关注 */ - for (let index = 0; index < uid_list.length; index++) { - const uid = uid_list[index]; + for (const [index, uid] of uid_list.entries()) { + log.info('清理关注', `(${index}) (${uid})`) /* 取消关注 */ - success = await BiliAPI.cancelAttention(uid); + if (await BiliAPI.cancelAttention(uid)) { + log.info('清理关注', '成功') + } else { + log.error('清理关注', '失败') + break + } /* 延时 */ await delay(clear_remove_delay); - - if (!success) break; } } else { - const now = Date.now() / 1000; - let before_separate = []; - const MY_UID = Number(GlobalVar.get('myUID')); - for (let index = 0; ; index++) { - log.info('清理动态', `开始读取第${index + 1}页(12条)`); - const { allModifyDynamicResArray, offset: _offset } = await Public.prototype.checkAllDynamic(GlobalVar.get("myUID"), 1, 5 * 1000, offset); - offset = _offset; - for (let index = 0; index < allModifyDynamicResArray.length; index++) { - const res = allModifyDynamicResArray[index]; - const { type, dynamic_id, createtime } = res; + const + Now = Date.now() / 1000, + MY_UID = Number(GlobalVar.get('myUID')); + + let next_offset = '0', + before_separate = []; + + for (const page of infiniteNumber()) { + log.info('清理动态', `开始读取第${page + 1}页`); + const { allModifyDynamicResArray, offset } = await Public.prototype.checkAllDynamic(MY_UID, 1, 5 * 1000, next_offset); + next_offset = offset; + for (const [index, dyinfo] of allModifyDynamicResArray.entries()) { + log.info('清理动态', `第${page + 1}页中的第${index + 1}个动态`) + const { type, dynamic_id, createtime } = dyinfo || {}; if (type === clear_dynamic_type) { - const { origin_uid } = res; - if ((now - createtime) > 86400 * clear_max_day) { + const + { origin_uid } = dyinfo, + days_ago = (Now - createtime) / 86400; + + if (days_ago > clear_max_day) { /* 移除动态 */ if (dynamic_id && clear_remove_dynamic @@ -77,17 +90,20 @@ async function clear() { success = await BiliAPI.cancelAttention(origin_uid); } + if (!success) break; /* 延时 */ await delay(clear_remove_delay); - - if (!success) break; } else { + log.info('清理动态', `已设置跳过${clear_max_day}天 当前动态发布时间: ${~~days_ago}天前`) + log.info('清理动态', `储存用户(${origin_uid})防止误删`) before_separate.push(origin_uid) } + } else { + log.info('清理动态', `此动态类型为${type} != 要清理的动态类型${clear_dynamic_type}`) } } - log.info('清理动态', `第${index + 1}页中的转发动态与关注全部处理成功`) - if (offset === '0' || !success) break; + log.info('清理动态', `第${page + 1}页(${allModifyDynamicResArray.length})中的转发动态与关注全部处理成功`) + if (next_offset === '0' || !success) break; } } return diff --git a/lib/lottery-in-nodejs.js b/lib/lottery-in-nodejs.js index 2e30a6e..40eb92b 100644 --- a/lib/lottery-in-nodejs.js +++ b/lib/lottery-in-nodejs.js @@ -85,9 +85,8 @@ async function isMe() { if (follow_unread + unfollow_unread > 0) { const check = async (type) => { let session_t = ''; - let max = 0; let MySession = await BiliAPI.getSessionInfo(type) - do { + for (const index of Util.infiniteNumber()) { for (const Session of MySession.data) { const { content, sender_uid, session_ts, timestamp, talker_id } = Session; session_t = session_ts; @@ -103,12 +102,13 @@ async function isMe() { } } } - if (MySession.has_more) { + if (MySession.has_more && index < 16) { await Util.delay(3e3); MySession = await BiliAPI.getSessionInfo(type, session_t) + } else { + break } - max++ - } while (MySession.has_more && max < 10); + } } if (follow_unread) { log.info('中奖检测', '<-- 正在检查已关注者的私信') @@ -134,12 +134,13 @@ async function isMe() { * @param {string} num */ async function checkCookie(num) { + const My_UID = GlobalVar.get("myUID"); if (await BiliAPI.getMyinfo()) { - log.info('Cookie有效性检测', '成功登录'); + log.info('Cookie有效性检测', `成功登录 UID:${My_UID}`); return true; } else { - log.error('Cookie有效性检测', `登录失败 COOKIE${num} 已失效 UID:${GlobalVar.get("myUID")}`); - await sendNotify('动态抽奖出错-登录失败', `COOKIE${num} 已失效 UID:${GlobalVar.get("myUID")}`) + log.error('Cookie有效性检测', `登录失败 COOKIE${num} 已失效 UID:${My_UID}`); + await sendNotify('动态抽奖出错-登录失败', `COOKIE${num} 已失效 UID:${My_UID}`) return false; } } diff --git a/main.js b/main.js index 2b0accd..e562f2e 100644 --- a/main.js +++ b/main.js @@ -10,7 +10,7 @@ const metainfo = [ ` __/ | | | `, ` |___/ |_| `, ` `, - ` v1.9.8 by shanmite`, + ` v1.9.9 by shanmite`, ] /**多账号存储 */ let multiple_account = []; diff --git a/script/pkg/pkg.ps1 b/script/pkg/pkg.ps1 index 64ef37f..8469cb6 100644 --- a/script/pkg/pkg.ps1 +++ b/script/pkg/pkg.ps1 @@ -1,3 +1,4 @@ +$README = "README.md" $TEMPLATE_CONFIG_FILE = "my_config.example.js" $TEMPLATE_ENV_FILE = "env.example.js" @@ -14,6 +15,7 @@ $TRIARR = @( Copy-Item -Path $TEMPLATE_ENV_FILE -Destination $TARGET_DIR -Force Copy-Item -Path $TEMPLATE_CONFIG_FILE -Destination $TARGET_DIR -Force +Copy-Item -Path $README -Destination $TARGET_DIR -Force Set-Location -Path $TARGET_DIR @@ -30,6 +32,8 @@ foreach ($TRI in $TRIARR) { Move-Item -Path $BIN -Destination $DIR -Force Copy-Item -Path $ENV_FILE -Destination $DIR -Force Copy-Item -Path $CONFIG_FILE -Destination $DIR -Force + Copy-Item -Path $README -Destination $DIR -Force + if ($DIR -eq "nlts-win-x64") { New-Item -Path $DIR -Name "start.bat" -ItemType File -Value "@echo off && lottery start && pause" -Force New-Item -Path $DIR -Name "check.bat" -ItemType File -Value "@echo off && lottery check && pause" -Force @@ -40,4 +44,5 @@ foreach ($TRI in $TRIARR) { } Remove-Item -Path $ENV_FILE -Remove-Item -Path $CONFIG_FILE \ No newline at end of file +Remove-Item -Path $CONFIG_FILE +Remove-Item -Path $README \ No newline at end of file