Skip to content

Commit

Permalink
Reimplement sort method.
Browse files Browse the repository at this point in the history
  • Loading branch information
xkww3n committed Nov 4, 2023
1 parent 2032910 commit ed25fbb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions Utils/geosite.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ def batch_convert(categories: list, tools: list, exclusions=None) -> None:
for category in categories:
src_geosite = set(open(const.PATH_SOURCE_V2FLY/category, mode="r", encoding="utf-8").read().splitlines())
ruleset_geosite = parse(src_geosite, exclusions)
ruleset_geosite.sort()
rule.dump(ruleset_geosite, tool, const.PATH_DIST/tool, category)
12 changes: 4 additions & 8 deletions Utils/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def includes(self, other):


class RuleSet:
Type: str # DOMAIN / IP / CLASSIC
Type: str # DOMAIN / IP / CLASSIC
Payload: list[Rule]

def __init__(self, ruleset_type: str, payload: list):
Expand Down Expand Up @@ -69,6 +69,9 @@ def add(self, rule):
def remove(self, rule):
self.Payload.remove(rule)

def sort(self):
self.Payload.sort(key=lambda item: str(item))


def custom_convert(src: Path) -> RuleSet:
src_custom = open(src, mode="r", encoding="utf-8").read().splitlines()
Expand Down Expand Up @@ -216,13 +219,6 @@ def batch_dump(src: RuleSet, targets: list, dst_path: Path, filename: str) -> No
dump(src, target, dst_path/target, filename)


def set_to_ruleset(ruleset_type: str, src: set) -> RuleSet:
list_sorted = [item for item in src]
list_sorted.sort(key=lambda item: str(item))
ruleset = RuleSet(ruleset_type, list_sorted)
return ruleset


def apply_patch(src: RuleSet, name: str) -> RuleSet:
try:
patch = open(const.PATH_SOURCE_PATCH/(name + ".txt"), mode="r").read().splitlines()
Expand Down
11 changes: 11 additions & 0 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
logger.info(f"Generated {len(ruleset_rejections)} reject rules.")
logger.info(f"Generated {len(ruleset_exclusions)} exclude rules.")

ruleset_rejections.sort()
ruleset_exclusions.sort()

rule.batch_dump(ruleset_rejections, const.TARGETS, const.PATH_DIST, "reject")
rule.batch_dump(ruleset_exclusions, const.TARGETS, const.PATH_DIST, "exclude")

Expand Down Expand Up @@ -112,6 +115,7 @@
logger.debug(f"{item} removed for having a overseas TLD.")
logger.info(f"Generated {len(ruleset_domestic)} domestic rules.")

ruleset_domestic.sort()
rule.batch_dump(ruleset_domestic, const.TARGETS, const.PATH_DIST, "domestic")

END_TIME = time_ns()
Expand All @@ -126,6 +130,8 @@
if not line.startswith("#"):
ruleset_cidr.add(rule.Rule("IPCIDR", line))
logger.info(f"Generated {len(ruleset_cidr)} domestic IPv4 rules.")

ruleset_cidr.sort()
rule.batch_dump(ruleset_cidr, const.TARGETS, const.PATH_DIST, "domestic_ip")

src_cidr6 = connection.get(const.URL_CHNROUTES_V6).text.splitlines()
Expand All @@ -139,7 +145,10 @@
for cidr in list_cidr6_raw:
ruleset_cidr6.add(rule.Rule("IPCIDR6", cidr))
logger.info(f"Generated {len(ruleset_cidr6)} domestic IPv6 rules.")

ruleset_cidr6.sort()
rule.batch_dump(ruleset_cidr6, const.TARGETS, const.PATH_DIST, "domestic_ip6")

END_TIME = time_ns()
logger.info(f"Finished. Total time: {format((END_TIME - START_TIME) / 1e9, '.3f')}s\n")

Expand Down Expand Up @@ -175,6 +184,7 @@
if filename.is_file():
logger.debug(f'Start converting "{filename.name}".')
ruleset_custom = rule.custom_convert(filename)
ruleset_custom.sort()
rule.batch_dump(ruleset_custom, const.TARGETS, const.PATH_DIST, filename.stem)
logger.debug(f"Converted {len(ruleset_custom)} rules.")

Expand All @@ -183,6 +193,7 @@
for filename in list_file_personal:
logger.debug(f'Start converting "{filename.name}".')
ruleset_personal = rule.custom_convert(filename)
ruleset_personal.sort()
rule.batch_dump(ruleset_personal, ["text", "text-plus", "yaml", "surge-compatible", "clash-compatible"],
const.PATH_DIST/"personal", filename.stem)
rule.dump(ruleset_personal, "geosite", const.PATH_DIST/"geosite", ("personal-" + filename.stem))
Expand Down

0 comments on commit ed25fbb

Please sign in to comment.