-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
129 lines (109 loc) · 3.25 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
const fetch = require('node-fetch');
const AbortController = require('abort-controller');
const { url } = require('./sina.js');
const Feed = require('feed').Feed;
const fs = require('fs/promises');
const process = require('process');
const controller = new AbortController();
// 30 秒后取消请求
const timeout = setTimeout(
() => { controller.abort(); },
30000,
);
const feed = new Feed({
title: '新浪新闻',
description: '新浪全球实时财经新闻直播',
// link: 'https://ruanyf.github.io/sina-news/',
link: 'https://sina-news.vercel.app/',
language: 'zh-CN',
generator: 'sina news feed generator',
feedLinks: {
json: 'https://sina-news.vercel.app/rss.json',
rss: 'https://sina-news.vercel.app/rss.xml'
},
});
const filterArr = [
'比特币',
'以太坊',
'莱特币',
'瑞波币',
'疫苗',
'疫情',
'新冠',
'央行',
'联储',
'中央银行',
'财长',
'财政部',
'参议院',
'众议院',
];
async function main() {
const response = await fetch(url, {
headers: {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10130'},
signal: controller.signal
});
if (response.status < 200 || response.status >= 300) {
throw new Error('wrong status code');
}
const json = await response.json();
console.log(`successfully fetch the feed.`);
const result = json.result || {};
if (!result.status || result.status.code !== 0) return;
const items = result.data.feed.list;
console.log(`successfully parse the feed.`);
items.forEach(item => {
if (!item.rich_text) return;
// text filter
for (let i = 0; i < filterArr.length; i++) {
if (item.rich_text.includes(filterArr[i])) {
// console.log(item.rich_text);
return;
}
}
// tag filter
if (item.tag && Array.isArray(item.tag) && item.tag.length) {
const tags = item.tag;
const tagFilterArr = ['5', '6', '7', '9'];
for (let i = 0; i < tags.length; i++) {
const tag = tags[i].id;
if (tagFilterArr.includes(tag)) {
// console.log(item.rich_text);
return;
}
}
}
feed.addItem({
title: item.rich_text,
id: item.id,
link: item.docurl,
content: '',
date: new Date(item.create_time + '+08:00'),
});
});
console.log(`successfully generating new feed.`);
try {
await fs.access('./dist', fs.constants.R_OK | fs.constants.W_OK);
await fs.rm('./dist', { recursive: true });
console.log(`successfully deleted ./dist`);
} catch {
// ...
}
await fs.mkdir('./dist');
console.log(`successfully create ./dist`);
await fs.writeFile('./dist/rss.json', feed.json1());
console.log(`successfully write rss.json`);
await fs.writeFile('./dist/rss.xml', feed.rss2());
console.log(`successfully write rss.xml`);
await fs.copyFile('./template/index.html', `./dist/index.html`);
await fs.copyFile('./template/page.js', `./dist/page.js`);
console.log(`successfully copy asset files`);
}
main()
.catch(err => {
console.log(err);
process.exit(1);
})
.finally(() => {
clearTimeout(timeout);
});