-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
120 lines (101 loc) · 2.96 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
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const nodemailer = require('nodemailer');
const moment = require('moment');
const [user, pass, tomail] = process.argv.slice(2);
console.log('user, pass, tomail: ', user, pass, tomail);
const daySTR = moment().format('yyyy-MM-DD');
/**
* 根据请求配置 读取数据
*/
async function handleReqConf({ method, url, header, data = {}, contentField }) {
const axios = require('axios');
const config = {
method,
url,
headers: header,
data,
};
const result = await axios(config);
return _.get(result, contentField, []);
}
function readReqFlow() {
const filePath = path.resolve(__dirname, './src/reqConf.json');
const reqJsonSTR = fs.readFileSync(filePath, {
encoding: 'utf-8',
});
return JSON.parse(reqJsonSTR).requestConfList;
}
function writeData2MD(itemList, { parse, title }) {
const daySTR = moment().format('yyyy-MM-DD');
const exists = fs.existsSync(
path.resolve(__dirname, './data/', './' + daySTR)
);
if (!exists) {
fs.mkdirSync(path.resolve(__dirname, './data/', './' + daySTR));
}
const STR = itemList.reduce((acc, item) => {
const title = _.get(item, parse.title);
const jumpURL = (parse.baseURL || '') + _.get(item, parse.jumpURL);
return acc + `\n- [${title}](${jumpURL})`;
}, `## ${title} \n`);
fs.writeFileSync(
path.resolve(__dirname, `./data/${daySTR}/` + title + '.md'),
STR
);
}
async function transferMD2Html() {
const daySTR = moment().format('yyyy-MM-DD');
const fileList = fs.readdirSync(path.resolve(__dirname, './data/' + daySTR));
let htmlSTR = '';
for (const file of fileList) {
const mdSTR = fs.readFileSync(
path.resolve(__dirname, './data/' + daySTR + '/' + file),
{
encoding: 'utf8',
}
);
const md = require('markdown').markdown;
const mdHTML = md.toHTML(mdSTR);
htmlSTR += mdHTML + '\n';
}
await sendMail(tomail, htmlSTR);
fs.writeFileSync(path.resolve(__dirname, './schedule-news.html'), htmlSTR);
}
async function sendMail(tomail = '[email protected]', HTML) {
// Create a SMTP transporter object
let transporter = nodemailer.createTransport({
secure: true,
host: 'smtp.163.com',
auth: {
user: user,
//这里是授权密码而不是邮件密码
pass: pass,
},
});
// Message object
let message = {
from: user,
// Comma separated list of recipients
to: tomail,
// Subject of the message
subject: '每日新闻 ' + daySTR,
// HTML body
html: HTML,
};
let info = await transporter.sendMail(message);
console.log('Message sent successfully as %s', info.messageId);
}
(async function () {
const reqFlow = readReqFlow();
for (const reqConf of [...reqFlow]) {
try {
const result = await handleReqConf(reqConf);
writeData2MD(result, reqConf);
} catch (e) {
console.error('读取失败', e.message);
}
}
await transferMD2Html();
})();