重构通知功能,替换 pushPlusNotify 为 sendNotify,移除不必要的代码

This commit is contained in:
LingandRX 2024-11-30 22:38:36 +08:00
parent 504bdb77bd
commit 41f0c9045f
2 changed files with 1376 additions and 93 deletions

View File

@ -1,89 +1,4 @@
const got = require('got');
const timeout = 15000;
const push_config = {
// 官方文档http://www.pushplus.plus/
PUSH_PLUS_TOKEN: '79e01a66036642e5969d52a5952b5cb7', // push+ 微信推送的用户令牌
PUSH_PLUS_USER: '', // push+ 微信推送的群组编码
};
const $ = {
post: (params, callback) => {
const { url, ...others } = params;
got.post(url, others).then(
(res) => {
let body = res.body;
try {
body = JSON.parse(body);
} catch (error) {}
callback(null, res, body);
},
(err) => {
callback(err?.response?.body || err);
}
);
},
get: (params, callback) => {
const { url, ...others } = params;
got.get(url, others).then(
(res) => {
let body = res.body;
try {
body = JSON.parse(body);
} catch (error) {}
callback(null, res, body);
},
(err) => {
callback(err?.response?.body || err);
}
);
},
logErr: console.log,
};
function pushPlusNotify(text, desp) {
return new Promise((resolve) => {
const { PUSH_PLUS_TOKEN, PUSH_PLUS_USER } = push_config;
if (PUSH_PLUS_TOKEN) {
desp = desp.replace(/[\n\r]/g, '<br>'); // 默认为html, 不支持plaintext
const body = {
token: `${PUSH_PLUS_TOKEN}`,
title: `${text}`,
content: `${desp}`,
topic: `${PUSH_PLUS_USER}`,
};
const options = {
url: `https://www.pushplus.plus/send`,
body: JSON.stringify(body),
headers: {
'Content-Type': ' application/json',
},
timeout,
};
$.post(options, (err, resp, data) => {
try {
if (err) {
console.log(`Push+ 发送${PUSH_PLUS_USER ? '一对多' : '一对一'}通知消息失败😞\n`, err);
} else {
if (data.code === 200) {
console.log(`Push+ 发送${PUSH_PLUS_USER ? '一对多' : '一对一'}通知消息完成🎉\n`);
} else {
console.log(
`Push+ 发送${PUSH_PLUS_USER ? '一对多' : '一对一'}通知消息异常 ${data.msg}\n`
);
}
}
} catch (e) {
$.logErr(e, resp);
} finally {
resolve(data);
}
});
} else {
resolve();
}
});
}
const { sendNotify } = require('./sendNotify');
const myHeaders = new Headers();
{
@ -129,8 +44,6 @@ const requestOptions = {
redirect: 'follow',
};
const fs = require('fs');
function getcurrentDate() {
return `${new Date().getFullYear()}-${
new Date().getMonth() + 1
@ -146,8 +59,6 @@ class zhihu_hot {
}
}
const saveFile = `${new Date().getTime()}_zhihu_hot.json`;
(async () => {
console.log('fetching zhihu hot list...');
@ -168,12 +79,11 @@ const saveFile = `${new Date().getTime()}_zhihu_hot.json`;
link: item.target.link.url,
})
);
fs.writeFileSync(saveFile, JSON.stringify(zhihu_hot_list, null, 2));
})
.catch((error) => console.error(error));
if (!zhihu_hot_list) {
await pushPlusNotify('知乎热榜更新失败', '请查看日志获取详细信息');
await sendNotify('知乎热榜更新失败', '请查看日志获取详细信息');
}
function setDiv(item, index) {
@ -200,7 +110,7 @@ const saveFile = `${new Date().getTime()}_zhihu_hot.json`;
</style>
<ul id="item-list">${content}</ul>`;
await pushPlusNotify('知乎热榜已更新', `${getcurrentDate()} 知乎热榜已更新` + html);
await sendNotify('知乎热榜已更新', `${getcurrentDate()} 知乎热榜已更新` + html);
console.log('fetching zhihu hot list done!');
})();

1373
sendNotify.js Normal file

File diff suppressed because it is too large Load Diff