-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (99 loc) · 2.78 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
const axios = require('axios');
const fetch = async (url, options = {}) => {
const { query, body, headers, timeout } = options;
const method = options.method || 'GET';
const params = query || {};
const _body = options.method === 'GET' ? undefined : (body || {});
const config = {
method,
url,
params,
body: _body,
headers: {
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
...headers,
},
timeout,
};
const res = await axios(config);
return res.data;
};
const csv = `https://raw.githubusercontent.com/timqian/chinese-independent-blogs/master/blogs-original.csv`;
const checkAPI = `https://zhblogs.ohyee.cc/api/blogs`;
const addAPI = `https://zhblogs.ohyee.cc/api/blog`;
async function main() {
const data = await fetch(csv).catch(err => {
console.log(err);
throw new Error("获取数据失败")
});
console.log("获取 chinese-independent-blogs 上游数据成功");
const lastLine = data.split('\n').pop().length > 0 ? data.split('\n').pop() : data.split('\n')[data.split('\n').length - 2];
if (!lastLine) throw new Error("数据为空");
const blog = lastLine.split(',');
const blogsTags = blog[3].split(';');
const name = blog[0];
const url = blog[1].trim(); // 去除 前后空格
const tags = blogsTags.map(tag => tag.trim());
const repeat = await fetch(checkAPI, {
query: {
search: url,
},
// headers: {
// "x-zhblogs-verify": "chinese-independent-blogs-upstream",
// "user-agent": "zhblogs/1.0.0"
// },
timeout: 300000 // 5min
})
.then(res => {
return res.data.total > 0;
})
.catch(err => {
console.log(err.message);
throw new Error(`查重 ${name} 失败`)
});
if (repeat) {
console.log(`${name} 已存在于 zhblogs 数据库,跳过`);
return;
}
console.log(`${name} 不存在于 zhblogs 数据库,进入添加流程`);
await fetch(addAPI, {
headers: {
'Content-type': "application/json",
},
method: "PUT",
body: {
"token": "",
"blog": {
"name": name,
"url": url,
"status": "unknown",
"tags": tags,
"repeat": false,
"enabled": false,
"saveweb_id": "",
"recommend": false
}
},
timeout: 10000
})
.catch(err => {
console.log(err);
console.log({
"token": "",
"blog": {
"name": name,
"url": url,
"status": "unknown",
"tags": tags,
"repeat": false,
"enabled": false,
"saveweb_id": "",
"recommend": false
}
})
throw new Error(`添加 ${name} 失败 - ${err}`)
});
console.log(`${name} 已添加到 zhblogs 数据库`);
return true;
}
main();