diff --git a/.gitignore b/.gitignore index 070512b..a6161a8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ node_modules/ tests/ dyids/ lottery_info/ +lottery_dyids/ dist/ .env *.log diff --git a/lib/core/monitor.js b/lib/core/monitor.js index d2435f3..5fea69c 100644 --- a/lib/core/monitor.js +++ b/lib/core/monitor.js @@ -26,7 +26,8 @@ class Monitor extends Searcher { ['UIDs', this.getLotteryInfoByUID.bind(this)], ['TAGs', this.getLotteryInfoByTag.bind(this)], ['Articles', this.getLotteryInfoByArticle.bind(this)], - ['APIs', this.getLotteryInfoByAPI.bind(this)] + ['APIs', this.getLotteryInfoByAPI.bind(this)], + ['TxT', this.getLotteryInfoByTxT.bind(this)] ]); } /** diff --git a/lib/core/searcher.js b/lib/core/searcher.js index 4e951c2..07833b4 100644 --- a/lib/core/searcher.js +++ b/lib/core/searcher.js @@ -605,6 +605,71 @@ class Searcher { } }); } + + /** + * 从本地文件中获取抽奖信息 + * @param {string} txt + * @returns {Promise} + */ + async getLotteryInfoByTxT(txt) { + log.info('获取动态', `开始获取${utils.lottery_dyids}`); + const dyids = await utils.getLocalLotteryTxt(txt); + let + length = dyids.length, + dyinfos = []; + + for (const dyid of dyids) { + if (typeof dyid === "string" + && dyid.length === utils.dyid_length) { + + log.info('获取动态', `查看Txt中所提及动态(${dyid}) (${length--})`) + const card = await bili.getOneDynamicByDyid(dyid) + + if (card) { + await utils.delay(get_dynamic_detail_wait) + + const parsed_card = parseDynamicCard(card) + , { is_liked } = parsed_card; + + if ( + ((!check_if_duplicated || check_if_duplicated >= 2) + && is_liked) + || ((check_if_duplicated >= 1) + && await d_storage.searchDyid(dyid)) + ) { + log.info('获取动态', `动态(${dyid})已转发过`) + continue + } + + dyinfos.push(parsed_card); + } + } else { + log.warn('获取动态', `动态(${dyid})无效 (${length--})`) + } + } + const fomatdata = dyinfos.map(o => { + return { + lottery_info_type: 'txt', + create_time: o.create_time, + is_liked: o.is_liked, + uids: [o.uid, o.origin_uid], + uname: o.uname, + ctrl: o.ctrl, + dyid: o.dynamic_id, + reserve_id: o.reserve_id, + reserve_lottery_text: o.reserve_lottery_text, + is_charge_lottery: o.is_charge_lottery, + rid: o.rid_str, + chat_type: o.chat_type, + des: o.description, + type: o.type, + hasOfficialLottery: o.hasOfficialLottery + }; + }) + log.info('获取动态', `成功获取txt信息`); + + return fomatdata + } } module.exports = { Searcher, parseDynamicCard }; diff --git a/lib/data/config.js b/lib/data/config.js index 3a96967..abd7623 100644 --- a/lib/data/config.js +++ b/lib/data/config.js @@ -47,12 +47,19 @@ const config = { */ APIs: [], + /** + * lottery_dyids目录下抽奖动态文件名(如dyids.txt) + * 一行一个dyids + */ + TxT: [], + /** * 抽奖参与顺序组合 * * 0 - UIDs * * 1 - TAGs * * 2 - Articles * * 3 - APIs + * * 4 - TxT * @example * [3,2,1,0] * [1,2,1,2,1] diff --git a/lib/data/global_var.js b/lib/data/global_var.js index 0758209..a0893b2 100644 --- a/lib/data/global_var.js +++ b/lib/data/global_var.js @@ -25,7 +25,9 @@ let global_var = { [0, "UIDs"], [1, "TAGs"], [2, "Articles"], - [3, "APIs"]]); + [3, "APIs"], + [4, "TxT"], + ]); config.updata(num); diff --git a/lib/utils.js b/lib/utils.js index 88ed39e..c76f6ff 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -17,6 +17,8 @@ const utils = { dyids_dir: path.join(process.cwd(), "dyids"), /**lottery_info存放目录 */ lottery_info_dir: path.join(process.cwd(), "lottery_info"), + /**本地抽奖信息存放目录 */ + lottery_dyids: path.join(process.cwd(), "lottery_dyids"), /**dyid长度 */ dyid_length: 18, /** @@ -482,6 +484,22 @@ const utils = { async clearLotteryInfo() { await utils.createDir(utils.lottery_info_dir); await utils.createFile(utils.lottery_info_dir, `lottery_info_${Number(process.env.NUMBER)}.json`, "{}", "w") + }, + /** + * 获取含抽奖dyids + * @param {string} filename + * @returns {Promise>} + */ + getLocalLotteryTxt(filename) { + return new Promise((resolve) => { + fs.readFile(path.join(utils.lottery_dyids, filename), (err, data) => { + if (err) { + resolve([]) + } else { + resolve(data.toString("utf8").split(/[^0123456789]+/)) + } + }) + }); } }; diff --git a/my_config.example.js b/my_config.example.js index 690dd8b..cd3f3f8 100644 --- a/my_config.example.js +++ b/my_config.example.js @@ -51,12 +51,19 @@ module.exports = Object.freeze({ */ APIs: ["file://lottery_info_1.json"], + /** + * lottery_dyids目录下抽奖动态文件名(如dyids.txt) + * 一行一个dyids(非数字字符分割即可) + */ + TxT: ["dyids.txt"], + /** * 抽奖参与顺序组合 * * 0 - UIDs * * 1 - TAGs * * 2 - Articles * * 3 - APIs + * * 4 - TxT * @example * [3,2,1,0] * [1,2,1,2,1]