forked from Vincedream/NodeMail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
204 lines (189 loc) · 5.91 KB
/
main.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
const superagent = require("superagent"); //发送网络请求获取DOM
const cheerio = require("cheerio"); //能够像Jquery一样方便获取DOM节点
const nodemailer = require("nodemailer"); //发送邮件的node插件
const ejs = require("ejs"); //ejs模版引擎
const fs = require("fs"); //文件读写
const path = require("path"); //路径配置
const schedule = require("node-schedule"); //定时器任务库
//配置项
//纪念日
let startDay = "2016/6/24";
//当地拼音,需要在下面的墨迹天气url确认
const local = "hangzhou";
//发送者邮箱厂家
let EmianService = "126";
//发送者邮箱账户SMTP授权码
let EamilAuth = {
user: "[email protected]",
pass: "xxxx"
};
//发送者昵称与邮箱地址
let EmailFrom = '"vince" <[email protected]>';
//接收者邮箱地
let EmailTo = "[email protected]";
//邮件主题
let EmailSubject = "一封暖暖的小邮件";
//每日发送时间
let EmailHour = 5;
let EmialMinminute= 20;
// 爬取数据的url
const OneUrl = "http://wufazhuce.com/";
const WeatherUrl = "https://tianqi.moji.com/weather/china/zhejiang/" + local;
// 获取ONE内容
function getOneData(){
let p = new Promise(function(resolve,reject){
superagent.get(OneUrl).end(function(err, res) {
if (err) {
reject(err);
}
let $ = cheerio.load(res.text);
let selectItem = $("#carousel-one .carousel-inner .item");
let todayOne = selectItem[0];
let todayOneData = {
imgUrl: $(todayOne)
.find(".fp-one-imagen")
.attr("src"),
type: $(todayOne)
.find(".fp-one-imagen-footer")
.text()
.replace(/(^\s*)|(\s*$)/g, ""),
text: $(todayOne)
.find(".fp-one-cita")
.text()
.replace(/(^\s*)|(\s*$)/g, "")
};
resolve(todayOneData)
});
})
return p
}
// 获取天气提醒
function getWeatherTips(){
let p = new Promise(function(resolve,reject){
superagent.get(WeatherUrl).end(function(err, res) {
if (err) {
reject(err);
}
let threeDaysData = [];
let weatherTip = "";
let $ = cheerio.load(res.text);
$(".wea_tips").each(function(i, elem) {
weatherTip = $(elem)
.find("em")
.text();
});
resolve(weatherTip)
});
})
return p
}
// 获取天气预报
function getWeatherData(){
let p = new Promise(function(resolve,reject){
superagent.get(WeatherUrl).end(function(err, res) {
if (err) {
reject(err);
}
let threeDaysData = [];
let weatherTip = "";
let $ = cheerio.load(res.text);
$(".forecast .days").each(function(i, elem) {
const SingleDay = $(elem).find("li");
threeDaysData.push({
Day: $(SingleDay[0])
.text()
.replace(/(^\s*)|(\s*$)/g, ""),
WeatherImgUrl: $(SingleDay[1])
.find("img")
.attr("src"),
WeatherText: $(SingleDay[1])
.text()
.replace(/(^\s*)|(\s*$)/g, ""),
Temperature: $(SingleDay[2])
.text()
.replace(/(^\s*)|(\s*$)/g, ""),
WindDirection: $(SingleDay[3])
.find("em")
.text()
.replace(/(^\s*)|(\s*$)/g, ""),
WindLevel: $(SingleDay[3])
.find("b")
.text()
.replace(/(^\s*)|(\s*$)/g, ""),
Pollution: $(SingleDay[4])
.text()
.replace(/(^\s*)|(\s*$)/g, ""),
PollutionLevel: $(SingleDay[4])
.find("strong")
.attr("class")
});
});
resolve(threeDaysData)
});
});
return p
}
// 发动邮件
function sendMail(HtmlData) {
const template = ejs.compile(
fs.readFileSync(path.resolve(__dirname, "email.ejs"), "utf8")
);
const html = template(HtmlData);
let transporter = nodemailer.createTransport({
service: EmianService,
port: 465,
secureConnection: true,
auth: EamilAuth
});
let mailOptions = {
from: EmailFrom,
to: EmailTo,
subject: EmailSubject,
html: html
};
transporter.sendMail(mailOptions, (error, info={}) => {
if (error) {
console.log(error);
sendMail(HtmlData); //再次发送
}
console.log("邮件发送成功", info.messageId);
console.log("静等下一次发送");
});
}
// 聚合
function getAllDataAndSendMail(){
let HtmlData = {};
// how long with
let today = new Date();
console.log(today)
let initDay = new Date(startDay);
let lastDay = Math.floor((today - initDay) / 1000 / 60 / 60 / 24);
let todaystr =
today.getFullYear() +
" / " +
(today.getMonth() + 1) +
" / " +
today.getDate();
HtmlData["lastDay"] = lastDay;
HtmlData["todaystr"] = todaystr;
Promise.all([getOneData(),getWeatherTips(),getWeatherData()]).then(
function(data){
HtmlData["todayOneData"] = data[0];
HtmlData["weatherTip"] = data[1];
HtmlData["threeDaysData"] = data[2];
sendMail(HtmlData)
}
).catch(function(err){
getAllDataAndSendMail() //再次获取
console.log('获取数据失败: ',err);
})
}
let rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(1, 6)];
rule.hour = EmailHour;
rule.minute = EmialMinminute;
console.log('NodeMail: 开始等待目标时刻...')
let j = schedule.scheduleJob(rule, function() {
console.log("执行任务");
getAllDataAndSendMail();
});