Skip to content

Commit

Permalink
Improve Geosite type handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
xkww3n committed Nov 3, 2023
1 parent 450a0d0 commit fb1f57f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
"action": "add",
"args": {
"name": "apns",
"uri": "./dists/yaml/apns.txt"
"uri": "./dists/yaml/apns.yaml"
}
}
],
Expand Down
5 changes: 1 addition & 4 deletions Utils/geosite.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,4 @@ def batch_convert(categories: list, tools: list, exclusions=None) -> None:
src_geosite = set(open(const.PATH_SOURCE_V2FLY/category, mode="r", encoding="utf-8").read().splitlines())
set_geosite = parse(src_geosite, exclusions)
list_geosite_sorted = rule.set_to_sorted_list(set_geosite)
if tool == "geosite":
rule.dump(list_geosite_sorted, tool, const.PATH_DIST/tool/category)
else:
rule.dump(list_geosite_sorted, tool, const.PATH_DIST/tool/(category + ".txt"))
rule.dump(list_geosite_sorted, tool, const.PATH_DIST/tool, category)
18 changes: 11 additions & 7 deletions Utils/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,18 @@ def is_domain(rule: Filter) -> bool:
return False


def dump(src: list, target: str, dst: Path) -> None:
def dump(src: list, target: str, dst: Path, filename: str) -> None:
try:
dist = open(dst, mode="w", encoding="utf-8")
if target == "yaml":
filename = filename + ".yaml"
elif target == "geosite":
filename = filename
else:
filename = filename + ".txt"
dist = open(dst/filename, mode="w", encoding="utf-8")
except FileNotFoundError:
dst.parent.mkdir(parents=True)
dist = open(dst, mode="w")
dst.mkdir(parents=True)
dist = open(dst/filename, mode="w")
match target:
case "text":
for rule in src:
Expand Down Expand Up @@ -161,9 +167,7 @@ def dump(src: list, target: str, dst: Path) -> None:

def batch_dump(src: list, targets: list, dst_path: Path, filename: str) -> None:
for target in targets:
if target == "geosite" and ".txt" in filename:
filename = filename.split(".")[0]
dump(src, target, dst_path/target/filename)
dump(src, target, dst_path/target, filename)


def set_to_sorted_list(src: set) -> list:
Expand Down
20 changes: 10 additions & 10 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
list_rejections_sorted = rule.set_to_sorted_list(set_rejections)
list_exclusions_sorted = rule.set_to_sorted_list(set_exclusions)

rule.batch_dump(list_rejections_sorted, const.TARGETS, const.PATH_DIST, "reject.txt")
rule.batch_dump(list_exclusions_sorted, const.TARGETS, const.PATH_DIST, "exclude.txt")
rule.batch_dump(list_rejections_sorted, const.TARGETS, const.PATH_DIST, "reject")
rule.batch_dump(list_exclusions_sorted, const.TARGETS, const.PATH_DIST, "exclude")

END_TIME = time_ns()
logger.info(f"Finished. Total time: {format((END_TIME - START_TIME) / 1e9, '.3f')}s\n")
Expand Down Expand Up @@ -116,7 +116,7 @@
logger.info(f"Generated {len(set_domestic)} domestic rules.")

list_domestic_sorted = rule.set_to_sorted_list(set_domestic)
rule.batch_dump(list_domestic_sorted, const.TARGETS, const.PATH_DIST, "domestic.txt")
rule.batch_dump(list_domestic_sorted, const.TARGETS, const.PATH_DIST, "domestic")

END_TIME = time_ns()
logger.info(f"Finished. Total time: {format((END_TIME - START_TIME) / 1e9, '.3f')}s\n")
Expand All @@ -131,7 +131,7 @@
list_cidr.append(rule.Rule("IPCIDR", line))
logger.info(f"Generated {len(list_cidr)} domestic IPv4 rules.")
rule.batch_dump(list_cidr, ["text", "text-plus", "yaml", "surge-compatible", "clash-compatible"],
const.PATH_DIST, "domestic_ip.txt")
const.PATH_DIST, "domestic_ip")

src_cidr6 = connection.get(const.URL_CHNROUTES_V6).text.splitlines()
list_cidr6_raw = []
Expand All @@ -145,7 +145,7 @@
list_cidr6.append(rule.Rule("IPCIDR6", cidr))
logger.info(f"Generated {len(list_cidr6)} domestic IPv6 rules.")
rule.batch_dump(list_cidr6, ["text", "text-plus", "yaml", "surge-compatible", "clash-compatible"],
const.PATH_DIST, "domestic_ip6.txt")
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 @@ -196,13 +196,13 @@
rule.batch_dump(list_custom_sorted,
["yaml", "surge-compatible", "clash-compatible"],
# Classical ruleset doesn't have plain text type.
const.PATH_DIST, filename.name)
const.PATH_DIST, filename.stem)
elif is_ipcidr:
rule.batch_dump(list_custom_sorted,
["text", "text-plus", "yaml", "surge-compatible", "clash-compatible"],
const.PATH_DIST, filename.name)
const.PATH_DIST, filename.stem)
else:
rule.batch_dump(list_custom_sorted, const.TARGETS, const.PATH_DIST, filename.name)
rule.batch_dump(list_custom_sorted, const.TARGETS, const.PATH_DIST, filename.stem)
logger.debug(f"Converted {len(list_custom_sorted)} rules.")

# There's no personal classical type ruleset. So no logic about that.
Expand All @@ -212,8 +212,8 @@
set_personal = rule.custom_convert(filename)
list_personal_sorted = rule.set_to_sorted_list(set_personal)
rule.batch_dump(list_personal_sorted, ["text", "text-plus", "yaml", "surge-compatible", "clash-compatible"],
const.PATH_DIST, "personal/" + filename.name)
rule.dump(list_personal_sorted, "geosite", const.PATH_DIST/"geosite"/("personal-" + filename.stem))
const.PATH_DIST/"personal", filename.stem)
rule.dump(list_personal_sorted, "geosite", const.PATH_DIST/"geosite", ("personal-" + filename.stem))
logger.debug(f"Converted {len(list_personal_sorted)} rules.")

END_TIME = time_ns()
Expand Down

0 comments on commit fb1f57f

Please sign in to comment.