-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (111 loc) · 3.69 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
130
131
const http = require('https');
const fs = require('fs');
const xlsx = require('node-xlsx');
const currentDate = new Date();
const fileHeader =
'! Title: Suspicious sites and web pages companies of Russia\n' +
'! Description: Filters of suspicious companies recognized by the Bank of Russia.\n' +
'! Expires: 1 day\n' +
'! Homepage: https://github.com/PrographerMan/cbr-ublock-rules\n' +
'! Last modified: ' + currentDate.toLocaleDateString('en-US').toString() + '\n\n';
const currentYear = currentDate.getFullYear();
const currentMonth = currentDate.getMonth() + 1;
const currentDay = currentDate.getDate();
const dateAsParameter = `${currentMonth}/${currentDay}/${currentYear}`;
const outputXSLXFileName = 'file.xlsx';
const outputRulesFileName = 'filters.txt';
const searchParams = [
{
name: 'FromDate',
value: dateAsParameter
},
{
name: 'ToDate',
value: dateAsParameter
},
{
name: 'posted',
value: false
}
];
const downloadableFileURL = new URL('https://cbr.ru/Queries/UniDbQuery/DownloadExcel/123126');
searchParams.forEach((param) => {
downloadableFileURL.searchParams.append(param.name, param.value);
});
const readyUrl = downloadableFileURL.toString();
const file = fs.createWriteStream(outputXSLXFileName);
class BlockRule {
static addressBeginning = '||';
static separator = '^';
static defaultProtocol = 'https://';
static pageBlockModifier = '$document';
static metricParams = {
src: ['www.avito.ru'],
utm_campaign: ['www.avito.ru'],
utm_medium: ['www.avito.ru'],
utm_source: ['www.avito.ru'],
igshid: ['instagram.com'],
from: ['vk.com']
}
constructor(url) {
if (!url) return;
let address;
try {
address = new URL(url);
} catch (error) {
try {
address = new URL(`${BlockRule.defaultProtocol}${url}`);
} catch (error) {
return;
}
}
this.hasSeparator = true;
if (address.pathname !== '/') {
this.pathname = address.pathname;
this.hasSeparator = false;
this.blockPage = true;
}
this.verbatim = address.hostname;
this.searchParams = new URLSearchParams();
Object.assign(this.searchParams, address.searchParams);
Object.keys(BlockRule.metricParams).forEach((key) => {
const searchParamsHasMetrics = this.searchParams.has(key);
const currentHostnameHasMetricKey = BlockRule.metricParams[key].includes(address.hostname);
if (searchParamsHasMetrics && currentHostnameHasMetricKey) {
this.searchParams.delete(key);
}
});
}
toString() {
const separator = this.hasSeparator ? BlockRule.separator : '';
const pathname = this.pathname ? this.pathname : '';
const hasParams = this.searchParams?.toString().length > 0;
const searchParams = hasParams ? `?${this.searchParams.toString()}` : '';
const blockPage = this.blockPage ? BlockRule.pageBlockModifier : '';
return `${BlockRule.addressBeginning}${this.verbatim}${pathname}${searchParams}${separator}${blockPage}`;
}
}
http.get(readyUrl, function (response) {
response.pipe(file);
file.on("finish", () => {
file.close();
const columnNumber = 4;
let rules = [];
let i = 1;
const worksheet = xlsx.parse(`${__dirname}/${file.path}`)[0].data;
while (i < worksheet.length) {
const cellValue = worksheet[i][columnNumber];
if (!cellValue) {
i++;
continue;
};
let newRules = cellValue.split(',').map((url) => {
return new BlockRule(url.trim());
});
rules = rules.concat(newRules);
i++;
}
fs.writeFile(outputRulesFileName, fileHeader, { flag: 'a+' }, err => {});
fs.writeFile(outputRulesFileName, rules.join('\n'), { flag: 'a+' }, err => {});
});
});