-
Notifications
You must be signed in to change notification settings - Fork 3
/
perform.js
217 lines (185 loc) · 5.26 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
let Parser = require('rss-parser')
let fs = require('fs')
let querystring = require('querystring')
let urlMod = require('url')
let URL = urlMod.URL
let jsonText = fs.readFileSync('./subs.json');
let feedxUrls = JSON.parse(jsonText);
let content = JSON.stringify(feedxUrls, undefined, 4);
fs.writeFileSync(`./subs.json`, content)
async function fetchArticles(site) {
let articles
if (site['url']) {
articles = await fetchFeedx(site['site'], site['url'])
} else if (site == '中国数字时代') {
articles = await fetchCDT()
}
articles.sort((x, y) => x.pubDate - y.pubDate)
return articles
}
async function fetchFeedx(site, url) {
let parser = new Parser({customFields: {
item: [
['media:group', 'media:group'],
]
}
})
let feed = await parser.parseURL(url)
return feed.items.map(item => {
let content;
let link;
if(item['content:encoded']){
content = item['content:encoded']
}else if (item['media:group']) {
content = item['media:group']['media:description'][0]
}else if (item['content']) {
content = item['content']
}else{
content = item.description
}
if (item['link']){
link = item.link
}else{
link = item.guid
}
let author = site
if (item['author']){
author = item.author
}else if (item['dc:creator']){
author = item['dc:creator']
}
return {
title: item.title.replace(/[\x00-\x1F\x7F-\x9F]/g, ""),
content: content.replace(/[\x00-\x1F\x7F-\x9F]/g, ""),
link: link,
pubDate: Date.parse(item.pubDate),
site: site,
author: author
}
})
}
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 validArticles.map(item => {
return {
title: item.title,
content: item['content:encoded'],
link: item.link,
guid: item.guid,
pubDate: Date.parse(item.pubDate),
site: '中国数字时代',
author: '中国数字时代'
}
})
}
async function performCDT() {
let site = '中国数字时代'
try {
// let siteFolder = `./news/${site}/_posts`
let siteFolder = `./_posts`
fs.mkdirSync(siteFolder, { recursive: true })
let articles = await fetchCDT()
articles.map(a => {
generateArticle(a)
})
// generateList(site)
} catch(e) {
console.log([site, e])
}
}
async function perform() {
// let sites = Object.keys(feedxUrls)
let sites = feedxUrls
sites.map(site => {
performSite(site)
})
// performCDT()
// performSite('自由亚洲电台')
}
async function performSite(site) {
try {
// let siteFolder = `./news/${site}/_posts`
let siteFolder = `./_posts`
fs.mkdirSync(siteFolder, { recursive: true })
let articles = await fetchArticles(site)
articles.map(a => {
generateArticle(a)
})
// generateList(site)
} catch(e) {
console.log([site['site'], e])
}
}
function generateArticle(article) {
let today = new Date()
let md = renderMD(article)
let pubDate = timeConverter(article.pubDate)
if (today < pubDate) {
pubDate = today
}
let dateString = pubDate.toISOString()
let titletext = article.title.toString().replace(/"/g, '\\"').replace("...", '')
let articlelink = new URL(article.link).href
let header = `---
layout: post
title: "${titletext}"
date: ${dateString}
author: ${article.author}
from: ${articlelink}
tags: [ ${article.site} ]
categories: [ ${article.site} ]
---
`
md = header + md
let filename = `${dateString.substring(0, 10)}-${titletext.substring(0, 50)}.md`.replace(/\//g, '--')
if (!fs.existsSync(`./_posts/${filename}`)) {
fs.writeFileSync(`./_posts/${filename}`, md)
console.log(`add ./_posts/${filename}`)
}
}
function generateList(site) {
let siteFolder = `./lists/${site}`
if (!fs.existsSync(siteFolder)){
fs.mkdirSync(siteFolder);
}
let files = fs.readdirSync(siteFolder).slice(0, 100)
let listItems = files.map(item => {
let title = item.match(/^\d+_([\s\S]+)\.md$/)[1]
let timestamp = fs.readFileSync(`${siteFolder}/${item}`, 'utf8').match(/<!--(\d+)-/)
let date = ''
if (timestamp) {
let gmtPlus8 = new Date(+timestamp[1] + 8 * 60 * 60 * 1000)
date = `${gmtPlus8.getUTCMonth() + 1}-${gmtPlus8.getUTCDate()} `
}
return `${date}[${strip(title)}](/lists/${urlMod.resolve('', `${site}/${item}`)})\n`
})
let list = listItems.join("\n")
let md = `${site}
------
${list}
[查看更多](/lists/${site})`
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})
------
<div>
${item.content.split("\n").map(line => strip(line)).join('')}
</div>
`
}
function timeConverter(UNIX_timestamp){
var a = new Date(UNIX_timestamp);
return a
}
perform()