diff --git a/lib/core/monitor.js b/lib/core/monitor.js index 855607f..418121c 100644 --- a/lib/core/monitor.js +++ b/lib/core/monitor.js @@ -289,12 +289,12 @@ class Monitor extends Searcher { if (lottery_param[0] !== "APIs" && save_lottery_info_to_file && protoLotteryInfo.length) { log.info("保存抽奖信息", "保存开始") - appendLotteryInfoFile(lottery_param[1].toString(), protoLotteryInfo) + await appendLotteryInfoFile(lottery_param[1].toString(), protoLotteryInfo) } if (lottery_param[0] !== "APIs" && set_lottery_info_url && protoLotteryInfo.length) { log.info("上传抽奖信息", "上传开始") - new Promise((resolve) => { + await new Promise((resolve) => { send({ url: set_lottery_info_url, method: "POST", diff --git a/lib/utils.js b/lib/utils.js index 7b5757a..41276ce 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -370,25 +370,6 @@ const utils = { }) }); }, - /** - * deleteFolderRecursive - * @param {*} url - */ - deleteFolderRecursive(url) { - var files = []; - if (fs.existsSync(url)) { - files = fs.readdirSync(url); - files.forEach(function (file) { - var curPath = path.join(url, file); - if (fs.statSync(curPath).isDirectory()) { - utils.deleteFolderRecursive(curPath); - } else { - fs.unlinkSync(curPath); - } - }); - fs.rmdirSync(url); - } - }, /** * 读取dyid文件 * @param {number} num @@ -413,23 +394,20 @@ const utils = { * @param {import("./core/searcher").LotteryInfo[]} lottery_info * @return {Promise} */ - appendLotteryInfoFile(from, lottery_info) { + async appendLotteryInfoFile(from, lottery_info) { let all_lottery_info = {}; try { all_lottery_info = utils.strToJson(fs.readFileSync(path.join(utils.lottery_info_dir, `lottery_info_${Number(process.env.NUMBER)}.json`)).toString()) } catch (_) { all_lottery_info = {} } - utils - .createDir(utils.lottery_info_dir) - .then(() => { - if (all_lottery_info[from] instanceof Array) { - all_lottery_info[from].push(...lottery_info) - } else { - all_lottery_info[from] = lottery_info - } - utils.createFile(utils.lottery_info_dir, `lottery_info_${Number(process.env.NUMBER)}.json`, JSON.stringify(all_lottery_info), "w") - }) + await utils.createDir(utils.lottery_info_dir); + if (all_lottery_info[from] instanceof Array) { + all_lottery_info[from].push(...lottery_info) + } else { + all_lottery_info[from] = lottery_info + } + await utils.createFile(utils.lottery_info_dir, `lottery_info_${Number(process.env.NUMBER)}.json`, JSON.stringify(all_lottery_info), "w") }, /** * 读取lottery_info @@ -449,10 +427,11 @@ const utils = { }); }, /** - * 清空lottery_info + * 清空lottery_info file */ - clearLotteryInfo() { - utils.deleteFolderRecursive(utils.lottery_info_dir) + async clearLotteryInfo() { + await utils.createDir(utils.lottery_info_dir); + await utils.createFile(utils.lottery_info_dir, `lottery_info_${Number(process.env.NUMBER)}.json`, "{}", "w") } }; diff --git a/main.js b/main.js index 9079d1c..72212cd 100644 --- a/main.js +++ b/main.js @@ -56,11 +56,14 @@ async function main() { if (await checkCookie(NUMBER)) { const mode = process.env.lottery_mode; const help_msg = "用法: lottery [OPTIONS]\n\nOPTIONS:\n\tstart 启动抽奖\n\tcheck 中奖检查\n\tclear 清理动态和关注\n\tupdate 检查更新\n\thelp 帮助信息"; - const { lottery_loop_wait, check_loop_wait, clear_loop_wait } = require("./lib/data/config"); + const { lottery_loop_wait, check_loop_wait, clear_loop_wait, save_lottery_info_to_file } = require("./lib/data/config"); switch (mode) { case 'start': log.info('抽奖', '开始运行'); loop_wait = lottery_loop_wait; + if (save_lottery_info_to_file) { + await clearLotteryInfo() + } await start(NUMBER); break; case 'check': @@ -158,13 +161,11 @@ function initConfig() { log.warn('结束运行', '5秒后自动退出'); await delay(5 * 1000); } else { - clearLotteryInfo(); while (loop_wait) { log.info('程序休眠', `${loop_wait / 1000}秒后再次启动`) await delay(loop_wait) if (initEnv() || initConfig()) return; await main() - clearLotteryInfo(); } log.info('结束运行', '未在config.js中设置休眠时间') }