-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.js
61 lines (53 loc) · 2.31 KB
/
notification.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
exports = module.exports = {};
let mongo = require('./mongo');
let config = require('./config');
let utils = require('./utils');
let agent = require('superagent');
let xmlParser = require('xml-js')
let _ = require('lodash');
const sendNews = async () => {
let ignoreRegExpList = config.notification.ignoreRegExpList;
let rssRes;
try {
rssRes = await agent.get(`${config.notification.rssHubUrl}/bilibili/user/dynamic/353840826`)
} catch (err) {
// rss 获取失败, 等下次再获取
return;
}
// 过期半天的新闻就没有价值了
let beforeRoundTime = new Date();
beforeRoundTime.setHours(beforeRoundTime.getHours() - 12);
// rss 处理
let opt = xmlParser.xml2js(rssRes.body.toString());
let fullNewsList = opt.elements[0].elements[0].elements
.filter(el => el.name === 'item')
.map(el => ({
message: el.elements.find(n => n.name === 'description').elements[0].cdata
.split(/(\/\/\@|\/\/转发自)/g)[0]
.replace(/\<br\>/g, '')
.replace(/\<img.*?src="(.*?)".*?\>/g, (match, p1) => `[CQ:image,file=${p1}]`)
.replace(/\<.*?\>/g, ''),
id: el.elements.find(n => n.name === 'guid').elements[0].text,
date: new Date(el.elements.find(n => n.name === 'pubDate').elements[0].text),
}));
let archiveList = await mongo.TempData.find({
type: 'news',
"data.id": { $in: fullNewsList.map(news => news.id) },
}).toArray();
// 同步推送到镜像
let archiveDiff = _.differenceBy(fullNewsList, archiveList.map(archive => archive.data), el => el.id);
if (!archiveDiff.length) return;
await mongo.TempData.insertMany(archiveDiff.map(messageBody => ({ type: 'news', data: messageBody, date: messageBody.date })));
// 筛选消息
let newsList = fullNewsList
.filter(el => el.date > beforeRoundTime)
.filter(m => !ignoreRegExpList.reduce((pre, cur) => pre || m.message.match(cur), false));
let unsendMessageList = _.differenceBy(newsList, archiveList.map(archive => archive.data), el => el.id);
if (!unsendMessageList.length) return;
// 发送推送
let groupList = await mongo.Group.find({ needNotification: true }).toArray();
for (let messageBody of unsendMessageList) {
await Promise.all(groupList.map(group => utils.sendMessage({ group_id: group.group_id, message: messageBody.message })));
}
};
exports.sendNews = sendNews;