forked from hello-earth/cloudflare-better-ip
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
70 lines (59 loc) · 2.33 KB
/
app.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
const fs = require('fs');
function readAllFilenames(directoryPath) {
// 读取目录内容
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error('读取目录出错: ', err);
return;
}
// 遍历每个文件名并输出
files.forEach((file) => {
// 读取文件内容
fs.readFile(directoryPath+"/"+file, 'utf8', (err, data) => {
if (err) {
console.error('读取文件出错: ', err);
return;
}
// 将文件内容按行分割为数组
const lines = data.split('\n');
// 创建用于聚合国家结果的对象
const countries = {};
// 遍历每一行数据
lines.forEach((line) => {
if(line == ''){
return;
}
// 提取国家代码
const countryCode = line.split('|')[2].trim();
// 将数据写入对应的国家文件
if (countries[countryCode]) {
countries[countryCode].push(line);
} else {
countries[countryCode] = [line];
}
});
// 将结果写入不同的文件
for (const countryCode in countries) {
const fileName = countryCode + '.txt';
const fileContent = countries[countryCode].join('\n');
if(!fs.existsSync("result")) {
fs.mkdirSync("result")
}
if(!fs.existsSync("result/"+directoryPath)) {
fs.mkdirSync("result/"+directoryPath)
}
fs.writeFile("result/"+directoryPath+"/"+fileName, fileContent, 'utf8', (err) => {
if (err) {
console.error('写入文件出错: ', err);
return;
}
console.log(fileName + ' 文件已保存');
});
}
});
});
});
}
// 调用函数读取文件名
readAllFilenames('cloudflare');
readAllFilenames('cloudfront');