Skip to content

Commit

Permalink
feat: 新增本地dyids抽奖新信息获取方式 (#311)(#287)
Browse files Browse the repository at this point in the history
Fixed #311
  • Loading branch information
shanmiteko committed Aug 25, 2023
1 parent a83c5ab commit 9ab605a
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules/
tests/
dyids/
lottery_info/
lottery_dyids/
dist/
.env
*.log
Expand Down
3 changes: 2 additions & 1 deletion lib/core/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
]);
}
/**
Expand Down
65 changes: 65 additions & 0 deletions lib/core/searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,71 @@ class Searcher {
}
});
}

/**
* 从本地文件中获取抽奖信息
* @param {string} txt
* @returns {Promise<LotteryInfo[] | null>}
*/
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 };
7 changes: 7 additions & 0 deletions lib/data/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion lib/data/global_var.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ let global_var = {
[0, "UIDs"],
[1, "TAGs"],
[2, "Articles"],
[3, "APIs"]]);
[3, "APIs"],
[4, "TxT"],
]);

config.updata(num);

Expand Down
18 changes: 18 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
/**
Expand Down Expand Up @@ -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<Array<String>>}
*/
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]+/))
}
})
});
}
};

Expand Down
7 changes: 7 additions & 0 deletions my_config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 9ab605a

Please sign in to comment.