-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.js
300 lines (285 loc) · 13.8 KB
/
mail.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
name: Server-Side Mailing Function of Virus Notificator
author: TonyHe
link: https://www.ouorz.com
last Update: 19-2-2020
*/
const request = require("request");
const mongo = require("mongodb").MongoClient;
const nodemailer = require("nodemailer");
var databaseUrl = process.env.MONGODB_URL;
/*
name: 邮件发送模板
*/
async function sendEmail(titleContent, textContent, htmlContent, receiver) {
let mailer = nodemailer.createTransport({
host: "smtp.163.com",
port: 465,
secure: true,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD,
},
});
let message = {
from: process.env.SMTP_USER,
to: receiver,
subject: titleContent,
text: textContent,
html: htmlContent,
};
let result = await new Promise(function (resolve, reject) {
setTimeout(
() =>
mailer.sendMail(message, function (err, msg) {
if (err) {
return reject({
msg: {
response: err.response,
code: err.responseCode,
},
});
} else {
return resolve({
msg: "已接收到每日推送邮件: " + msg.accepted,
});
}
}),
1000
);
});
console.log(result.msg);
}
/*
name: 取得 PHP 爬取数据
*/
async function mailDaily() {
global.dataObject = await new Promise((resolve, reject) => {
request(
"https://files.snapaper.com/virus",
function (error, response, data) {
if (!error && response.statusCode == 200) {
let dataObject = JSON.parse(data.toString()); //获取
return resolve(dataObject);
} else {
let dataObject = {};
return reject(dataObject);
}
}
);
});
//连接 MongoDB 数据库
mongo.connect(
databaseUrl,
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
function (err, db) {
if (err) {
console.log(err);
return;
} else {
var coll = db.db("notificator");
//获取数据
coll
.collection("mail_users")
.find()
.toArray(function (err, result) {
if (err) {
console.log(err);
} else {
if (result.length) {
for (var i = 0; i < result.length; ++i) {
//订阅用户信息结构
let receiverData = {
email: null,
province: null,
city: null,
data: {
country: {
name: "中国",
confirm: 0,
death: 0,
cured: 0,
},
province: {
name: null,
confirm: 0,
death: 0,
cured: 0,
},
city: {
name: null,
confirm: 0,
death: 0,
cured: 0,
},
},
};
//基础信息
receiverData.email = result[i]["email"].toString();
receiverData.province = result[i]["pro"];
receiverData.city = result[i]["city"];
//数据获取状态
var dataStatus = {
total: false,
country: false,
province: false,
city: false,
};
if (typeof global.dataObject !== undefined) {
//获取到总数据
let dataNow = global.dataObject;
//全国信息获取
if (dataNow.total_confirmed !== 0) {
//获取到全国数据
receiverData.data.country = {
//得到全国数据对象
confirm: parseInt(dataNow.total_confirmed),
death: parseInt(dataNow.total_death),
cured: parseInt(dataNow.total_cured),
};
dataStatus.country = true;
} else {
//未获取到全国数据
dataStatus.country = false;
}
//省份信息获取
if (
receiverData.province !== undefined &&
typeof dataNow.provinces_data[receiverData.province] !==
undefined
) {
//获取到省份数据
let dataNowTemp =
dataNow.provinces_data[receiverData.province];
receiverData.data.province = {
//得到省份数据对象
name: receiverData.province,
confirm: parseInt(dataNowTemp.confirmed),
death: parseInt(dataNowTemp.death),
cured: parseInt(dataNowTemp.cured),
};
dataStatus.province = true;
} else {
//未获取到省份数据
dataStatus.province = false;
}
//城市信息获取
if (
receiverData.city !== undefined &&
typeof dataNow.cities_data[receiverData.city] !==
undefined
) {
//获取到省份数据
let dataNowTemp = dataNow.cities_data[receiverData.city];
if (receiverData.city !== "无城市") {
receiverData.data.city = {
//得到省份数据对象
name: receiverData.city,
confirm: parseInt(dataNowTemp.confirmed),
death: parseInt(dataNowTemp.death),
cured: parseInt(dataNowTemp.cured),
};
dataStatus.city = true;
} else {
dataStatus.city = false;
}
} else {
//未获取到省份数据
dataStatus.city = false;
}
if (
dataStatus.city &&
dataStatus.province &&
dataStatus.country
) {
//全部信息都可获取
var htmlContent =
`<div style="width:100%;padding-bottom:50px"><div style="border:1px solid #eee;margin-bottom:40px;width:80%;margin:40px auto;border-radius:10px;box-shadow:0 1px 5px 0 rgba(0,0,0,.12)"><img src="https://i.loli.net/2020/02/05/b3adxQsVHX6voY4.jpg" style="max-width:100%;height:auto"></div><div><div style="border:1px solid #eee;width:80%;margin:0 auto;border-radius:10px"><h3 style="text-align:center;background:#f1f2f3;font-size:1.4rem;font-weight:500;color:#666;padding:5px 0 8px 0;letter-spacing:1px;margin-top: 0px;">全国数据</h3><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#f5a623;border-radius:5px">确诊人数</b><em style="color:#999"> |</em> ` +
receiverData.data.country.confirm +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#ff0100;border-radius:5px">死亡人数</b><em style="color:#999"> |</em> ` +
receiverData.data.country.death +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#006ff4;border-radius:5px">治愈人数</b><em style="color:#999"> |</em> ` +
receiverData.data.country.cured +
` 人</p></div><div style="border:1px solid #eee;width:80%;margin:0 auto;border-radius:10px;margin-top:20px"><h3 style="text-align:center;background:#f1f2f3;font-size:1.4rem;font-weight:500;color:#666;padding:5px 0 8px 0;letter-spacing:1px;margin-top: 0px;">` +
receiverData.province +
`数据</h3><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#f5a623;border-radius:5px">确诊人数</b><em style="color:#999"> |</em> ` +
receiverData.data.province.confirm +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#ff0100;border-radius:5px">死亡人数</b><em style="color:#999"> |</em> ` +
receiverData.data.province.death +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#006ff4;border-radius:5px">治愈人数</b><em style="color:#999"> |</em> ` +
receiverData.data.province.cured +
` 人</p></div><div style="border:1px solid #eee;width:80%;margin:0 auto;border-radius:10px;margin-top:20px"><h3 style="text-align:center;background:#f1f2f3;font-size:1.4rem;font-weight:500;color:#666;padding:5px 0 8px 0;letter-spacing:1px;margin-top: 0px;">` +
receiverData.city +
`数据</h3><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#f5a623;border-radius:5px">确诊人数</b><em style="color:#999"> |</em> ` +
receiverData.data.city.confirm +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#ff0100;border-radius:5px">死亡人数</b><em style="color:#999"> |</em> ` +
receiverData.data.city.death +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#006ff4;border-radius:5px">治愈人数</b><em style="color:#999"> |</em> ` +
receiverData.data.city.cured +
` 人</p></div></div></div>`;
sendEmail(
"今日新型冠状病毒疫情数据推送",
"今日新型冠状病毒肺炎疫情数据",
htmlContent,
receiverData.email
);
} else if (dataStatus.province && dataStatus.country) {
//全国和省份信息可获取
var htmlContent =
`<div style="width:100%;padding-bottom:50px"><div style="border:1px solid #eee;margin-bottom:40px;width:80%;margin:40px auto;border-radius:10px;box-shadow:0 1px 5px 0 rgba(0,0,0,.12)"><img src="https://i.loli.net/2020/02/05/b3adxQsVHX6voY4.jpg" style="max-width:100%;height:auto"></div><div><div style="border:1px solid #eee;width:80%;margin:0 auto;border-radius:10px"><h3 style="text-align:center;background:#f1f2f3;font-size:1.4rem;font-weight:500;color:#666;padding:5px 0 8px 0;letter-spacing:1px;margin-top: 0px;">全国数据</h3><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#f5a623;border-radius:5px">确诊人数</b><em style="color:#999"> |</em> ` +
receiverData.data.country.confirm +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#ff0100;border-radius:5px">死亡人数</b><em style="color:#999"> |</em> ` +
receiverData.data.country.death +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#006ff4;border-radius:5px">治愈人数</b><em style="color:#999"> |</em> ` +
receiverData.data.country.cured +
` 人</p></div><div style="border:1px solid #eee;width:80%;margin:0 auto;border-radius:10px;margin-top:20px"><h3 style="text-align:center;background:#f1f2f3;font-size:1.4rem;font-weight:500;color:#666;padding:5px 0 8px 0;letter-spacing:1px;margin-top: 0px;">` +
receiverData.province +
`数据</h3><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#f5a623;border-radius:5px">确诊人数</b><em style="color:#999"> |</em> ` +
receiverData.data.province.confirm +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#ff0100;border-radius:5px">死亡人数</b><em style="color:#999"> |</em> ` +
receiverData.data.province.death +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#006ff4;border-radius:5px">治愈人数</b><em style="color:#999"> |</em> ` +
receiverData.data.province.cured +
` 人</p></div></div></div>`;
sendEmail(
"今日新型冠状病毒疫情数据推送",
"今日新型冠状病毒肺炎疫情数据",
htmlContent,
receiverData.email
);
} else if (dataStatus.country) {
//只全国信息可获取
var htmlContent =
`<div style="width:100%;padding-bottom:50px"><div style="border:1px solid #eee;margin-bottom:40px;width:80%;margin:40px auto;border-radius:10px;box-shadow:0 1px 5px 0 rgba(0,0,0,.12)"><img src="https://i.loli.net/2020/02/05/b3adxQsVHX6voY4.jpg" style="max-width:100%;height:auto"></div><div><div style="border:1px solid #eee;width:80%;margin:0 auto;border-radius:10px"><h3 style="text-align:center;background:#f1f2f3;font-size:1.4rem;font-weight:500;color:#666;padding:5px 0 8px 0;letter-spacing:1px;margin-top: 0px;">全国数据</h3><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#f5a623;border-radius:5px">确诊人数</b><em style="color:#999"> |</em> ` +
receiverData.data.country.confirm +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#ff0100;border-radius:5px">死亡人数</b><em style="color:#999"> |</em> ` +
receiverData.data.country.death +
` 人</p><p style="text-align:center;color:#666;font-size:1.2rem"><b style="color:#006ff4;border-radius:5px">治愈人数</b><em style="color:#999"> |</em> ` +
receiverData.data.country.cured +
` 人</p></div><div style="border:1px solid #eee;width:80%;margin:0 auto;border-radius:10px;margin-top:20px"><h3 style="text-align:center;background:#f1f2f3;font-size:1.4rem;font-weight:500;color:#666;padding:5px 0 8px 0;letter-spacing:1px;margin-top: 0px;">数据指南</h3><p style="text-align:center;color:#666;font-size:1.2rem">你可以前往 <a href="https://virus-notificator-vue.onrender.com" target="_blank">https://virus-notificator-vue.onrender.com</a> 增加省份/城市订阅源</p></div></div></div>`;
sendEmail(
"今日新型冠状病毒疫情数据推送",
"今日新型冠状病毒肺炎疫情数据",
htmlContent,
receiverData.email
);
} else {
console.log("未发送:" + receiverData.email);
}
} else {
dataStatus.total = false;
}
}
} else {
console.log("无人订阅,停止推送");
}
}
});
}
}
);
}
module.exports = mailDaily;