-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
69 lines (57 loc) · 2.51 KB
/
main.py
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
import os
import subprocess
import http.client
from urllib.parse import urlparse
RESOURCES = {
'abpvn.txt': 'https://raw.githubusercontent.com/abpvn/abpvn/master/filter/abpvn.txt',
'easylist.txt': 'https://easylist.to/easylist/easylist.txt',
'annoyance.txt': 'https://easylist-downloads.adblockplus.org/fanboy-annoyance.txt',
'my-rules.txt': 'https://manhduonghn.github.io/custom-filters/my-rules.txt',
'adguard.txt': 'https://raw.githubusercontent.com/AdguardTeam/AdguardFilters/master/BaseFilter/sections/adservers.txt'
}
MERGED_FILE = "merged.txt"
OUTPUT_FILE = "filters.dat"
def download_resource(url, filename):
parsed_url = urlparse(url)
conn = http.client.HTTPSConnection(parsed_url.netloc)
conn.request("GET", parsed_url.path)
response = conn.getresponse()
if response.status != 200:
raise Exception(f"Failed to download {url}, status code: {response.status}")
with open(filename, 'wb') as file:
file.write(response.read())
conn.close()
def download_resources():
for filename, url in RESOURCES.items():
download_resource(url, filename)
def merge_files():
with open(MERGED_FILE, 'w') as merged_file:
for filename in RESOURCES:
with open(filename, 'r') as file:
for line in file:
if not line.strip().startswith(("[", "!")):
merged_file.write(line)
lines = open(MERGED_FILE, 'r').readlines()
lines = sorted(set(lines))
with open(MERGED_FILE, 'w') as merged_file:
merged_file.writelines(lines)
def convert_filters():
subprocess.run(["chmod", "+x", "ruleset_converter"])
subprocess.run(["./ruleset_converter",
"--input_format=filter-list",
"--output_format=unindexed-ruleset",
"--input_files=" + MERGED_FILE,
"--output_file=" + OUTPUT_FILE])
def cleanup():
for filename in RESOURCES.keys():
os.remove(filename)
os.remove(MERGED_FILE)
download_resources()
merge_files()
convert_filters()
cleanup()
os.system(f'git config --global user.email "${{GITHUB_ACTOR_ID}}+${{GITHUB_ACTOR}}@users.noreply.github.com"')
os.system(f'git config --global user.name "$(gh api /users/${{GITHUB_ACTOR}} | jq .name -r)"')
os.system('git add filters.dat || error "Failed to add the filters bromite to repo"')
os.system('git commit -m "Update filters " --author=.')
os.system('git push origin main || error "Failed to push the filters bromite to repo"')