-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
209 lines (176 loc) · 4.67 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
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
const Crawler = require("crawler");
const fs = require("fs");
const cheerio = require("cheerio");
const TurndownService = require("turndown");
const turndownService = new TurndownService();
const showdown = require("showdown");
const converter = new showdown.Converter();
const { isDate, parseDate, formatDate, formatDate2, formatDate3 } = require("./util");
const args = process.argv.slice(2);
const date1 = +new Date(2010, 0, 1);
const date2 = +new Date(2016, 01, 03);
const updateReadme = require("./readme").update;
const today = new Date().setHours(23, 59, 59);
console.log("Today is", formatDate(today));
const oneDay = 24 * 60 * 60 * 1000;
const options = {
jQuery: {
name: "cheerio",
options: {
decodeEntities: false,
normalizeWhitespace: true,
xmlMode: true,
},
},
maxConnections: 8,
};
function getUrl(date) {
if (!isDate(date)) {
date = new Date(date);
}
if (+date < date1) {
console.log(formatDate(date), "< date1", formatDate(date1));
return "";
}
if (+date > today) {
console.log(formatDate(date), "> today", formatDate(today));
return "";
}
const str = formatDate2(date);
if (+date < date2) {
// 暂时不解析
console.log(formatDate(date), "< date2", formatDate(date2));
return "";
return `http://news.cctv.com/program/xwlb/${str}.shtml`;
}
return `https://tv.cctv.com/lm/xwlb/day/${str}.shtml`;
}
function getNewsDetailQueues(date) {
const urls = [];
const url = getUrl(date);
if (!url) {
return urls;
}
return new Promise((resolve) => {
const c = new Crawler({
...options,
callback: (error, res, done) => {
const uri = res.request.uri;
const href = uri.href;
console.log(formatDate(date), href);
if (error) {
console.error(error);
done();
return;
}
const $ = res.$;
$("a").each((i, ele) => {
if (i > 0) {
urls.push($(ele).attr("href").replace("http://news.cntv.cn/", "http://tv.cctv.com/"));
}
});
done();
},
});
c.queue(url);
c.on("drain", () => {
resolve(urls);
});
});
}
function getNewsDetail(date, queues) {
const result = [];
return new Promise((resolve) => {
const c = new Crawler({
...options,
callback: (error, res, done) => {
const uri = res.request.uri;
const href = uri.href;
if (error) {
console.log(formatDate(date), href);
console.error(error);
done();
return;
}
const $ = res.$;
const title = $(".cnt_nav h3").text().trim().replace("[视频]", "");
// const content = $(".cnt_bd").text();
const html = $(".cnt_bd").html();
const markdown = turndownService.turndown(html);
// console.log(title);
// console.log(html);
// console.log(markdown);
const index = queues.indexOf(href);
result[index] = {
href,
title,
html,
markdown,
};
done();
},
});
c.queue(queues);
c.on("drain", () => {
resolve(result);
});
});
}
async function toFile(date, result, cb) {
const main = result
.map((r) => {
return `## ${r.title}\n\n${r.markdown}\n
*[原文](${r.href})*
`;
})
.join("\n");
// const { year, month, day } = parseDate(date);
const dateStr = formatDate(date);
const content = `# 新闻联播 ${dateStr}\n\n${main}\n
更新于 ${formatDate3(new Date())}
`;
const html = converter.makeHtml(content);
fs.writeFileSync(`./news/${dateStr}.md`, content);
fs.writeFileSync(`./html/${dateStr}.html`, html);
}
async function main(startDate, endDate) {
if (!endDate) {
endDate = startDate;
}
days = (+endDate - +startDate) / oneDay;
const dates = new Array(days + 1).fill(0).map((v, i) => {
return new Date(+startDate + i * oneDay);
});
console.log(
"dates",
dates.map((d) => {
return formatDate(d);
})
);
const ps = dates.map((nowDate) => {
const p = (async (date) => {
const queues = await getNewsDetailQueues(date);
console.log(queues);
if (!queues.length) {
console.log(`${formatDate(nowDate)} 暂无新闻联播`);
return;
}
const result = await getNewsDetail(date, queues);
await toFile(date, result);
})(nowDate);
return p;
});
await Promise.all(ps);
updateReadme();
console.log("done");
// return;
}
if (args.length) {
const date1 = new Date(args[0]);
const date2 = args[1] ? new Date(args[1]) : date1;
main(date1, date2);
} else {
let date1 = new Date();
const date2 = new Date(+date1);
main(date1, date2);
}