Transmission-block-xunlei-pcdn/process_blacklist.py

44 lines
1.6 KiB
Python
Raw Normal View History

2024-07-30 11:02:10 +08:00
import gzip
2024-07-30 10:30:19 +08:00
import ipaddress
import re
import requests
output_file_name = "transmission_blacklist"
2024-07-30 10:30:19 +08:00
upstream_urls = [
"https://raw.githubusercontent.com/PBH-BTN/BTN-Collected-Rules/main/combine/all.txt",
"https://raw.githubusercontent.com/zealic/autorosvpn/master/isp/cn/route-isp-chinanet.txt", # 中国电信
"https://raw.githubusercontent.com/zealic/autorosvpn/master/isp/cn/route-isp-cn-cmcc.txt", # 中国移动
"https://raw.githubusercontent.com/zealic/autorosvpn/master/isp/cn/route-isp-cncgroup.txt", # 中国联通
"https://raw.githubusercontent.com/zealic/autorosvpn/master/isp/cn/route-isp-cn-crtc.txt", # 中国铁通
]
2024-07-30 10:30:19 +08:00
processed_lines = set()
2024-07-30 10:30:19 +08:00
for url in upstream_urls:
response = requests.get(url)
lines = response.text.splitlines()
2024-07-30 10:30:19 +08:00
for line in lines:
line = line.strip()
if not line or line.startswith("#"):
continue
# 过滤掉注释
if re.match(r"^\d", line):
2024-07-30 10:30:19 +08:00
if "/" in line:
network = ipaddress.ip_network(line, strict=False)
ip_range = f"{network.network_address}-{network.broadcast_address}"
elif "-" in line:
ip_range = line
2024-07-30 10:30:19 +08:00
else:
ip_range = f"{line}-{line}"
processed_lines.add(f"btn:{ip_range}")
2024-07-30 11:02:10 +08:00
with open(f"{output_file_name}.txt", "w", encoding="utf-8") as file:
for line in processed_lines:
file.write(line + "\n")
2024-07-30 10:30:19 +08:00
with gzip.open(f"{output_file_name}.gz", "wt", encoding="utf-8") as file:
for line in processed_lines:
file.write(line + "\n")