-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
create-everything-list.js
148 lines (128 loc) 路 4.28 KB
/
create-everything-list.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
/*
*
* ENG: This script is not functioning correctly. It duplicates domains when a new one is added to templates.
* POL: Ten skrypt nie dzia艂a poprawnie. Duplikuje on domeny, gdy do szablon贸w dodawana jest nowa.
*
*/
const fs = require('fs').promises;
const path = require('path');
const date = require('./functions/date.js');
const defaultFolder = path.join(__dirname, '..', 'blocklists', 'templates');
const generatedFolder = path.join(__dirname, '..', 'blocklists', 'generated');
const outputFile = path.join(generatedFolder, 'everything.txt');
const parseDomain = line => {
const domain = line.trim().replace(/^0\.0\.0\.0\s+/, '');
return domain === '' ? null : domain;
};
const generateHeader = size => {
return `# 禄 Domains : ${size.toLocaleString('en-US')}
# 禄 Version : ${date.timestamp}
# 禄 Last update : ${date.hours}:${date.minutes}:${date.seconds}.${date.milliseconds}, ${date.day}.${date.month}.${date.year} [GMT+2 HH:MM:SS.MS, DD.MM.YYYY]\n`;
};
const excludedFiles = [
'myanimelist.net.txt',
'shinden.pl.txt',
'discord.txt',
'spotify.txt',
'steam.txt',
'steam-extended.txt',
'whatsapp.txt',
'all-valve-games.txt',
'booth.pm.txt',
'gamebanana.txt',
'patreon.txt',
'pinterest.txt',
'youtube.txt',
'youtube-extended.txt',
'facebook.txt',
'instagram.txt',
'twitter.txt'
];
const getAllFiles = async folder => {
const files = await fs.readdir(folder);
const filePaths = [];
for (const file of files) {
const fullPath = path.join(folder, file);
const stat = await fs.stat(fullPath);
if (stat.isDirectory()) {
const subFolderFiles = await getAllFiles(fullPath);
filePaths.push(...subFolderFiles);
} else {
filePaths.push(fullPath);
}
}
return filePaths;
};
const worker = async () => {
try {
await fs.access(generatedFolder);
} catch (err) {
await fs.mkdir(generatedFolder, { recursive: true });
console.log('馃搧 Created \'generated\' folder');
}
const files = await getAllFiles(defaultFolder);
const domains = new Set();
await Promise.all(
files.map(async file => {
if (!excludedFiles.includes(path.basename(file))) {
const fileContents = await fs.readFile(file, 'utf8');
fileContents.split('\n').forEach(line => {
if (line.startsWith('0.0.0.0 ')) {
const domain = parseDomain(line);
if (domain !== null && !domain.startsWith('#')) {
domains.add(domain);
}
}
});
}
})
);
const allDomainsSize = domains.size;
const sortedDomains = [...domains].sort((a, b) => a.localeCompare(b));
const newContent = sortedDomains.map(domain => `0.0.0.0 ${domain}`).join('\n');
const newHeader = generateHeader(allDomainsSize);
try {
const savedContent = await fs.readFile(outputFile, 'utf8');
const lines = savedContent.trim().split('\n');
const existingHeaderIndex = lines.findIndex(line => line.startsWith('# 禄 Domains'));
let outputString = '';
if (existingHeaderIndex !== -1) {
lines.splice(existingHeaderIndex, 1, newHeader);
} else {
outputString = newHeader + '\n';
}
outputString += newContent;
const savedDomains = new Set(
lines
.slice(existingHeaderIndex + 1)
.map(parseDomain)
.filter(domain => domain !== null)
);
const newDomains = sortedDomains.filter(
domain => !savedDomains.has(domain) && !domain.startsWith('#')
);
const removedDomains = [...savedDomains].filter(
domain => !domains.has(domain) && !domain.startsWith('#')
);
if (newDomains.length > 0 || removedDomains.length > 0) {
outputString += '\n' + newDomains.map(domain => `0.0.0.0 ${domain}`).join('\n');
// Create or update everything.txt
await fs.writeFile(outputFile, `${newHeader}${outputString}`, 'utf8');
console.log(
`馃摑 Updated everything.txt with ${newDomains.length} new domains and removed ${removedDomains.length} domains (total: ${allDomainsSize.toLocaleString('en-US')}) in ${generatedFolder}\n`
);
} else {
console.log(`鉁旓笍 everything.txt is up to date with ${allDomainsSize.toLocaleString('en-US')} domains\n`);
}
} catch (err) {
console.warn(`鈿狅笍 ${err.message}`);
await fs.writeFile(outputFile, `${newHeader}${newContent}`, 'utf8');
console.log(
`馃摑 Saved new file everything.txt with ${allDomainsSize.toLocaleString('en-US')} domains in ${generatedFolder}\n`
);
}
};
(async () => {
await worker();
})();
module.exports = worker;