diff --git a/walk_route.py b/walk_route.py index 379b2a2..ffec50e 100644 --- a/walk_route.py +++ b/walk_route.py @@ -113,7 +113,8 @@ def masked_credentials() -> dict: "sk_tail": sk[-4:] if sk else "", "enabled": bool(ak)} -def build_query(params: dict, sk: str = "", mode: str = "official") -> str: +def build_query(params: dict, sk: str = "", mode: str = "official", + path: str = None) -> str: """按百度 SN 规则拼查询串。 百度官方样例(geocoder/v2 那份)与社区常见写法算出的 sn 不一致, @@ -121,17 +122,18 @@ def build_query(params: dict, sk: str = "", mode: str = "official") -> str: - official:quote("path?query", safe=...) -> 追加 SK -> quote_plus -> md5 - simple :urlencode(按 key 升序, safe="") -> md5("path?query" + SK) """ + api_path = path or PATH # SN 是绑路径的,换接口必须换路径 items = sorted((k, v) for k, v in params.items() if k != "sn") if mode == "simple": query = urlencode(items, encoding="utf-8", safe="") if not sk: return query - digest = hashlib.md5((PATH + "?" + query + sk).encode("utf-8")).hexdigest() + digest = hashlib.md5((api_path + "?" + query + sk).encode("utf-8")).hexdigest() return query + "&sn=" + digest raw_query = "&".join(f"{k}={v}" for k, v in items) - encoded = quote(PATH + "?" + raw_query, safe="/:=&?#+!$,;'@()*[]") - query = encoded[len(PATH) + 1:] + encoded = quote(api_path + "?" + raw_query, safe="/:=&?#+!$,;'@()*[]") + query = encoded[len(api_path) + 1:] if not sk: return query digest = hashlib.md5(quote_plus(encoded + sk).encode("utf-8")).hexdigest()