-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathperform.js
267 lines (224 loc) · 6.23 KB
/
perform.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
let Parser = require('rss-parser')
let fs = require('fs')
let querystring = require('querystring')
let urlMod = require('url')
let URL = urlMod.URL
let { JSDOM } = require('jsdom')
let fetch = require('node-fetch')
let sitesInfo = [
{
name: '路透',
abbr: 'reuters',
feed: 'https://feedx.net/rss/reuters.xml'
},
{
name: '纽约时报',
abbr: 'nyt',
feed: 'https://feedx.net/rss/nytimes.xml'
},
{
name: '美国之音',
abbr: 'voa',
feed: 'https://feedx.net/rss/mgzy1.xml'
},
{
name: '金融时报',
abbr: 'ft',
feed: 'https://feedx.net/rss/bbc.xml'
},
{
name: 'BBC',
abbr: 'bbc',
feed: 'https://feedx.net/rss/bbc.xml'
},
{
name: '法广',
abbr: 'rfi',
feed: 'https://feedx.net/rss/rfi.xml'
},
{
name: '德国之声',
abbr: 'dw',
feed: 'https://feedx.net/rss/dw.xml'
},
{
name: '自由亚洲电台',
abbr: 'rfa'
},
{
name: '中国数字时代',
abbr: 'cdt'
}
]
let feedxUrls = {
'路透': 'https://feedx.net/rss/reuters.xml',
'纽约时报': 'https://feedx.net/rss/nytimes.xml',
'美国之音': 'https://feedx.net/rss/mgzy1.xml',
'金融时报': 'https://feedx.net/rss/ft.xml',
'BBC': 'https://feedx.net/rss/bbc.xml',
'法广': 'https://feedx.net/rss/rfi.xml',
'德国之声': 'https://feedx.net/rss/dw.xml'
}
async function fetchArticles(site) {
let articles
if (feedxUrls[site]) {
articles = await fetchFeedx(site, feedxUrls[site])
// } else if (site == '中国数字时代') {
// articles = await fetchCDT()
} else if (site == '自由亚洲电台') {
articles = await fetchRFA()
}
articles.sort((x, y) => x.pubDate - y.pubDate)
return articles
}
async function fetchFeedx(site, url) {
let parser = new Parser()
let feed = await parser.parseURL(url)
return feed.items.map(item => {
return {
title: item.title,
content: item.content,
link: item.link,
pubDate: Date.parse(item.pubDate),
site: site
}
})
}
async function fetchCDT() {
let parser = new Parser()
let feed = await parser.parseURL('https://chinadigitaltimes.net/chinese/feed/')
let emojiRegexp = /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/
let validArticles = feed.items.filter(item => {
let categories = item.categories.filter(c => c.match(emojiRegexp))
return categories.length > 0
})
return await Promise.all(validArticles.map(async (item) => {
let res = await fetch(item.link)
let html = await res.text()
let document = new JSDOM(html).window.document
let content = document.querySelector('div.post-content').innerHTML.split("\n").map(line => strip(line)).join('')
return {
title: item.title,
//content: item['content:encoded'],
content: content,
link: item.link,
guid: item.guid,
pubDate: Date.parse(item.pubDate),
site: '中国数字时代'
}
}))
}
async function performCDT() {
let site = '中国数字时代'
try {
let siteFolder = `./articles/cdt`
fs.mkdirSync(siteFolder, { recursive: true })
let articles = await fetchCDT()
articles.map(a => {
let id = 0
if (match = a.guid.match(/\?p=(\d+)/)) {
id = 0xFFFFF ^ (+match[1])
}
generateArticle(a, id)
})
generateList(site)
} catch(e) {
console.log([site, e])
}
}
async function fetchRFA() {
let parser = new Parser()
let feed = await parser.parseURL('https://www.rfa.org/mandarin/yataibaodao/rss2.xml')
return feed.items.map(item => {
return {
title: item.title,
content: item['content:encoded'],
link: item.guid,
pubDate: Date.parse(item.pubDate),
site: '自由亚洲电台'
}
})
}
async function perform() {
let sites = Object.keys(feedxUrls)
sites.map(site => {
performSite(site)
})
performCDT()
performSite('自由亚洲电台')
}
async function performSite(site) {
try {
let siteFolder = `./articles/${siteFolderBySiteName(site)}`
fs.mkdirSync(siteFolder, { recursive: true })
let files = fs.readdirSync(siteFolder)
let lastId, lastDate
if (files.length > 0) {
let lastArticle = fs.readFileSync(`${siteFolder}/${files[0]}`, 'utf8')
lastDate = +lastArticle.match(/<!--(\d+)-/)[1]
lastId = +files[0].match(/(\d+)/)[1]
} else {
lastId = 0xFFFFF
lastDate = 0
}
let articles = await fetchArticles(site)
articles.filter(x => x.pubDate > lastDate).map(a => {
lastId -= 1
generateArticle(a, lastId, siteFolder, files)
})
generateList(site)
} catch(e) {
console.log([site, e])
}
}
function siteFolderBySiteName(site) {
return sitesInfo.find(x => x.name === site).abbr
}
function generateArticle(article, id, siteFolder, files) {
let filename = `${id}.md`
if (files) {
files.slice(0, 30).map(file => {
let article = fs.readFileSync(`${siteFolder}/${file}`, 'utf8')
let match = article.match(/\[([\s\S]*?)\]\(.+?\)/)
let title = match[1]
let url = match[2]
if (article.link == url || article.title == title) {
filename = file
}
})
}
let md = renderMD(article)
fs.writeFileSync(`./articles/${siteFolderBySiteName(article.site)}/${filename}`, md)
}
function generateList(site) {
let siteFolder = `./articles/${siteFolderBySiteName(site)}`
let files = fs.readdirSync(siteFolder).slice(0, 300)
let listItems = files.map(item => {
let content = fs.readFileSync(`${siteFolder}/${item}`, 'utf8')
let timestamp = content.match(/<!--(\d+)-/)
let title = content.match(/\[([\s\S]*?)\]\(/)[1]
let date = ''
if (timestamp) {
let gmtPlus8 = new Date(+timestamp[1] + 8 * 60 * 60 * 1000)
date = `${gmtPlus8.getUTCMonth() + 1}-${gmtPlus8.getUTCDate()} `
}
return `${date}[${strip(title)}](/articles/${urlMod.resolve('', `${siteFolderBySiteName(site)}/${item}`)})\n`
})
let list = listItems.join("\n")
let md = `${site}
------
${list}
`
fs.writeFileSync(`./lists/${site}.md`, md)
}
function strip(str) {
return str.replace(/(^\s*|\s*$)/g, '')
}
function renderMD(item) {
return `<!--${item.pubDate}-->
[${strip(item.title)}](${new URL(item.link).href})
------
${item.content.split("\n").map(line => strip(line)).join('')}
`
}
perform()