diff --git a/main.py b/main.py index 5fc87e7..28d3dd6 100755 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import sys import json import importlib import subprocess -from datetime import datetime +import datetime SCRIPT_PATH = os.path.join(os.getcwd(), "script") RULE_PATH = os.path.join(os.getcwd(), "rule") @@ -12,8 +12,8 @@ domain_file=RULE_PATH + "/domain.txt" regex_file=RULE_PATH + "/domain_regex.txt" ip_file=RULE_PATH + "/ip.txt" ip6_file=RULE_PATH + "/ip6.txt" -current_time = datetime.now() -format_time = current_time.strftime("%Y-%m-%d") +current_time = datetime.datetime.now(datetime.timezone.utc) +format_time = current_time.isoformat() process = subprocess.Popen('git tag | tail -1', stdout=subprocess.PIPE, shell=True) output, error = process.communicate() tag = output.decode().strip() @@ -22,6 +22,7 @@ if not os.path.exists(OUT_PATH): print(f"{OUT_PATH} 目录不存在!") sys.exit(1) + class RuleList: def __init__(self, domain_file, regex_file, ip_file, ip6_file): with open(domain_file, 'r') as file: @@ -40,10 +41,21 @@ class RuleList: rule = RuleList(domain_file, regex_file, ip_file, ip6_file) -def WriteFile(name, text, suffix, comment): +def WriteFile(name, text, suffix, comment, module_total): try: with open(OUT_PATH + "/AWAvenue-Ads-Rule-" + name + suffix, 'w', encoding="utf-8") as file: - file.write(f"{comment}Title: AWAvenue 秋风广告规则(AWAvenue-Ads-Rule)\n\n{comment}版本号: {tag}\n{comment}上次更新日期: {format_time}\n\n{comment}项目地址:https://github.com/TG-Twilight/AWAvenue-Ads-Rule\n\n{comment}如果需要在其它规则中混合此规则,请在您的规则显眼处注明本规则的出处,谢谢!\n{comment}加入Telegram群组 秋風がく山道 (@AWAvenueAdsChat) 与编写者交流,期待着您的到来!\n{comment}群组链接:https://t.me/AWAvenueAdsChat\n{comment}订阅Telegram频道 AWAvenue Ads Rule (@AWAvenueAdsRule) 获取最新公告,期待着您的订阅!\n{comment}频道链接:https://t.me/AWAvenueAdsRule\n\n{comment}This project is licensed under the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license.\n\n{comment}AD:\n{comment}倾城极速 - 畅游世界,高速互联!官网:https://panel.qqcjs.top 官网2:https://n3b53vrt.wcnmdmht.biz\n\n\n") + title = f"""{comment}Title: AWAvenue Ads Rule +{comment}Last modified: {format_time} +{comment}-------------------------------------- +{comment}Total lines: {module_total} +{comment}Version: 1.5.0-release-Patch-2 + +{comment}Homepage: https://github.com/TG-Twilight/AWAvenue-Ads-Rule +{comment}License: https://github.com/TG-Twilight/AWAvenue-Ads-Rule/blob/main/LICENSE + + +""" + file.write(title) for line in text: file.write(line + "\n") except Exception as e: @@ -56,8 +68,8 @@ def RunScript(): full_module_name = f"script.{module_name}" try: module = importlib.import_module(full_module_name) - module_list, module_suffix, module_comment = module.build(rule) - WriteFile(module_name, module_list, module_suffix, module_comment) + module_list, module_suffix, module_comment, module_total = module.build(rule) + WriteFile(module_name, module_list, module_suffix, module_comment, str(module_total)) except Exception as e: print(f"转换插件:{module_name}执行失败: {e}") diff --git a/script/AdClose.py b/script/AdClose.py index 9cdf8b3..a8ae401 100755 --- a/script/AdClose.py +++ b/script/AdClose.py @@ -15,4 +15,4 @@ def format_regex(List): def build(rule): Adclose_list = format_domain(rule.domain_list) + format_regex(rule.regex_list) - return Adclose_list, ".txt", "!" \ No newline at end of file + return Adclose_list, ".txt", "!", len(Adclose_list) \ No newline at end of file diff --git a/script/Adblock.py b/script/Adblock.py index f6fd3a3..6e281c3 100755 --- a/script/Adblock.py +++ b/script/Adblock.py @@ -15,4 +15,4 @@ def format_regex(List): def build(rule): Adblock_list = format_domain(rule.domain_list) + format_regex(rule.regex_list) - return Adblock_list, ".txt", "!" \ No newline at end of file + return Adblock_list, ".txt", "!", len(Adblock_list) \ No newline at end of file diff --git a/script/Adguard.py b/script/Adguard.py index ce05e39..79424de 100755 --- a/script/Adguard.py +++ b/script/Adguard.py @@ -15,4 +15,4 @@ def format_regex(List): def build(rule): Adguard_list = format_domain(rule.domain_list) + format_regex(rule.regex_list) - return Adguard_list, ".txt", "!" \ No newline at end of file + return Adguard_list, ".txt", "!", len(Adguard_list) \ No newline at end of file diff --git a/script/Clash-classical.py b/script/Clash-classical.py index 3f4c231..28e7650 100755 --- a/script/Clash-classical.py +++ b/script/Clash-classical.py @@ -22,4 +22,4 @@ def format_ip(List): def build(rule): clash_list = ["payload:"] + format_ip(rule.ip_list) + format_domain(rule.domain_list) + format_regex(rule.regex_list) - return clash_list, ".yaml", "#" \ No newline at end of file + return clash_list, ".yaml", "#", len(clash_list) \ No newline at end of file diff --git a/script/Clash.py b/script/Clash.py index f424273..d35a25b 100755 --- a/script/Clash.py +++ b/script/Clash.py @@ -16,4 +16,4 @@ def format_regex(List): def build(rule): clash_list = ["payload:"] + format_domain(rule.domain_list) + format_regex(rule.regex_list) - return clash_list, ".yaml", "#" + return clash_list, ".yaml", "#", len(clash_list) diff --git a/script/Mosdns_v5.py b/script/Mosdns_v5.py index 070f432..ce2b128 100644 --- a/script/Mosdns_v5.py +++ b/script/Mosdns_v5.py @@ -9,4 +9,4 @@ def format_domain(List): def build(rule): - return format_domain(rule.domain_list), ".txt", "#" + return format_domain(rule.domain_list), ".txt", "#", len(rule.domain_list) diff --git a/script/QuantumultX.py b/script/QuantumultX.py index d47423a..c61aab3 100755 --- a/script/QuantumultX.py +++ b/script/QuantumultX.py @@ -21,4 +21,5 @@ def format_ip(List): return ip def build(rule): - return format_ip(rule.ip_list) + format_domain(rule.domain_list) + format_regex(rule.regex_list), ".list", "#" \ No newline at end of file + quantumultx_list = format_ip(rule.ip_list) + format_domain(rule.domain_list) + format_regex(rule.regex_list) + return quantumultx_list, ".list", "#", len(quantumultx_list) \ No newline at end of file diff --git a/script/README.md b/script/README.md index 3ba5ee3..6e15ff3 100755 --- a/script/README.md +++ b/script/README.md @@ -7,22 +7,47 @@ `Adguard.py`: ```python -["||xxx.com^","||xxx.cc^"], ".txt", "!" +["||xxx.com^","||xxx.cc^"], ".txt", "!", xxx ``` -> **有三个返回值** + +> 处理后的域名列表(list), 文件后缀名(string), 注释符号(string), 规则数量(int|string) API: - rule.domain_list(获取域名列表) - rule.regex_list(获取正则表达式的域名列表) - rule.ip_list(获取ip列表) +- rule.ip6_list(获取ipv6列表) > 所有变量均为列表 无需读取文件 模板: -``` -# 主函数并且引入rule(对象) -def build(rule): - list = rule.domain_list + rule.regex_list + rule.ip_list - return list, ".yaml", "#" # 返回处理后的列表和文件后缀名和文件注释符号 +```python + +def format_domain(List): # 转换域名规则 + domain = [] + for line in List: + domain_lines = f" - DOMAIN,{line.strip()}" + domain.append(domain_lines) + return domain + +def format_regex(List): # 转换正则表达式规则 + regex = [] + for line in List: + regex_lines = f" - DOMAIN-REGEX,'{line.strip()}'" + regex.append(regex_lines) + return regex + +def format_ip(List): # 转换ip列表 + ip = [] + for line in List: + ip_lines = f" - IP-CIDR,{line.strip()}" + ip.append(ip_lines) + return ip + +def build(rule): # 入口函数 + clash_list = ["payload:"] + format_ip(rule.ip_list) + format_domain(rule.domain_list) + format_regex(rule.regex_list) 合并规则 + return clash_list, ".yaml", "#", len(clash_list) + # + ``` > 输出文件名=插件名 \ No newline at end of file diff --git a/script/RouterOS-Adlist.py b/script/RouterOS-Adlist.py index bc51af5..e9fad6b 100755 --- a/script/RouterOS-Adlist.py +++ b/script/RouterOS-Adlist.py @@ -9,4 +9,4 @@ def format_domain(List): def build(rule): - return format_domain(rule.domain_list), ".txt", "#" \ No newline at end of file + return format_domain(rule.domain_list), ".txt", "#", len(rule.domain_list) \ No newline at end of file diff --git a/script/RouterOS.py b/script/RouterOS.py index f0d732b..69f0f26 100755 --- a/script/RouterOS.py +++ b/script/RouterOS.py @@ -8,4 +8,4 @@ def format_domain(List): return domain def build(rule): - return format_domain(rule.regex_list), ".txt", "#" + return format_domain(rule.regex_list), ".txt", "#", len(rule.regex_list) diff --git a/script/Singbox-regex.py b/script/Singbox-regex.py index c127e94..c5c9474 100755 --- a/script/Singbox-regex.py +++ b/script/Singbox-regex.py @@ -29,5 +29,5 @@ def build(rule): ] } - json_data = json.dumps(List, indent=2) - return [json_data], ".json", "//" \ No newline at end of file + json_data = [json.dumps(List, indent=2)] + return json_data, ".json", "//", len(json_data) \ No newline at end of file diff --git a/script/Singbox.py b/script/Singbox.py index 5cba21f..b396e1c 100755 --- a/script/Singbox.py +++ b/script/Singbox.py @@ -20,5 +20,5 @@ def build(rule): ] } - json_data = json.dumps(rule, indent=2) - return [json_data], ".json", "//" \ No newline at end of file + json_data = [json.dumps(rule, indent=2)] + return json_data, ".json", "//", len(json_data) \ No newline at end of file diff --git a/script/Surge-RULE-SET.py b/script/Surge-RULE-SET.py index 29090a2..423398f 100755 --- a/script/Surge-RULE-SET.py +++ b/script/Surge-RULE-SET.py @@ -20,4 +20,5 @@ def format_ip(List): return ip def build(rule): - return format_ip(rule.ip_list) + format_domain(rule.domain_list) + format_regex(rule.regex_list), ".list", "#" \ No newline at end of file + surge_list = format_ip(rule.ip_list) + format_domain(rule.domain_list) + format_regex(rule.regex_list) + return surge_list, ".list", "#", len(surge_list) \ No newline at end of file diff --git a/script/Surge.py b/script/Surge.py index a4deec7..880f9ce 100755 --- a/script/Surge.py +++ b/script/Surge.py @@ -7,4 +7,4 @@ def format_domain(List): def build(rule): surge_list = format_domain(rule.domain_list) - return surge_list, ".list", "#" \ No newline at end of file + return surge_list, ".list", "#", len(surge_list) \ No newline at end of file diff --git a/script/hosts.py b/script/hosts.py index e42cdb1..f73d49f 100755 --- a/script/hosts.py +++ b/script/hosts.py @@ -8,4 +8,4 @@ def format_domain(List): def build(rule): - return format_domain(rule.domain_list), ".txt", "!" \ No newline at end of file + return format_domain(rule.domain_list), ".txt", "!", len(rule.domain_list) \ No newline at end of file