-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (167 loc) · 5.43 KB
/
index.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
const superagent = require("superagent");
const cheerio = require("cheerio");
const path = require("path");
const nodemailer = require("nodemailer");
const fs = require("fs");
const nunjucks = require("nunjucks");
const config = require("./config");
const schedule = require('node-schedule');
const moment = require('moment')
/* 当前工作目录 */
const dir = path.resolve(__dirname);
/**
* @param {dir} 接收多个文件目录名
* @return {type} 返回文件目录名所对应的地址
*/
function resolve(...dir) {
return path.resolve(...dir);
}
async function sendMail(type) {
const resData = await getAllData(type);
const njkString = fs.readFileSync(
resolve(dir, "template/model.html"),
"utf8"
);
//
const htmlData = nunjucks.renderString(njkString, resData);
let transporter = nodemailer.createTransport({
host: config.sendInfo.host,
port: 587,
secure: false,
auth: {
user: config.sendInfo.user,
pass: config.sendInfo.pass,
},
});
let info = {
from: config.sendInfo.user, // sender address
to: config.emailToArr[0].TO, // list of receivers
subject: config.sendInfo.subject, // Subject line
text: "Hello world?", // plain text body
html: htmlData, // html body
};
await transporter.sendMail(info, (error, info = {}) => {
if (error) {
console.log(error);
sendMail(type); //再次发送
}
console.log("邮件发送成功", info.messageId);
console.log("静等下一次发送~");
});
}
//获取天气
async function getWeather() {
//获取墨迹天气
let url = `${config.moji_host}${config.PROVINCE}/${config.CITY}`; // 根据配置得到天气url
let res = await superagent.get(url);
let $ = cheerio.load(res.text);
//获取墨迹天气地址
let addressText = $(".search_default")
.text()
.trim()
.split(", ")
.reverse()
.join("-");
//获取墨迹天气提示
let weatherTip = $(".wea_tips em").text();
// 获取现在的天气数据
const now = $(".wea_weather.clearfix");
let nowInfo = {
Temp: now.find("em").text(),
WeatherText: now.find("b").text(),
FreshText: now.find(".info_uptime").text(),
};
// 循环获取未来三天数据
let threeDaysData = [];
$(".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"),
});
});
return {
moji: {
addressText,
weatherTip,
nowInfo,
threeDaysData,
},
};
}
//获取one数据
async function getOne() {
let res = await superagent.get(config.ONE_URL);
let $ = cheerio.load(res.text); //转化成类似jquery结构
let todayOneList = $("#carousel-one .carousel-inner .item");
// 通过查看DOM获取今日句子
let info = $(todayOneList[0])
.find(".fp-one-cita")
.text()
.replace(/(^\s*)|(\s*$)/g, "");
let imgSrc = $(todayOneList[0]).find(".fp-one-imagen").attr("src");
return {
// 抛出 one 对象
one: {
info,
imgSrc,
},
};
}
async function getAllData(type) {
let oneData = await getOne();
let weatherData = await getWeather();
let date = new Date();
let today = parseTime(date)
let day1 = moment('2019-01-26', "YYYY-MM-DD");
let day2 = moment(today, "YYYY-MM-DD");
let dayNums = day2.diff(day1, 'days');
const msg = type === 'morning' ? '早安,元气满满的一天开始了喔!' : '元气满满的一天即将结束,准备好明天的衣服,记得早点休息哦!'
const allData = {
today: `今天是${today},在一起的第${dayNums}天`,
describe: msg,
...oneData,
...weatherData,
};
console.log(`今天是${today},在一起的第${dayNums}天`)
return allData;
}
function parseTime(d) {
const newDate = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
return newDate;
}
const scheduleCronstyle = () => {
//秒、分、时、日、月、周几
console.log('任务开始*******')
schedule.scheduleJob('59 59 06 * * *', () => {
console.log('等候早安问候:' + parseTime(new Date()));
sendMail('morning')
});
schedule.scheduleJob('59 59 20 * * *', () => {
console.log('等候晚安问候:' + parseTime(new Date()));
sendMail('night')
});
}
//秒、分、时、日、月、周几
scheduleCronstyle()