feat(auth): print a scannable ASCII QR in the terminal

WSL and --no-browser never showed the PNG, and placeholder Chinese cookies crashed urllib latin-1 headers before the code was issued.
This commit is contained in:
kairee committed 2026-11-02 10:35:56 +08:00
1 parent 12732f6316
commit 2641ba6f87
5 files changed
+183 -11

No files matched your search

+19 -1
View File
@@ -45,12 +45,30 @@ def strip_identity(cookie: str) -> str:
return "; ".join(kept)
def _is_latin1(value: str) -> bool:
try:
value.encode("latin-1")
except UnicodeEncodeError:
return False
return True
def cookie_header(cookie: str, *, auth: bool = False) -> str:
"""生成可放入 HTTP 头的 Cookie。占位中文、缺 ``=`` 的片段和非 latin-1 字段都会丢掉。"""
source = cookie if auth else strip_identity(cookie)
return "; ".join(
f"{key}={value}"
for key, value in parse_cookie(source)
if _is_latin1(key) and _is_latin1(value)
)
def make_headers(cfg: dict, auth: bool = False, *, referer: str | None = None) -> dict[str, str]:
"""构造与 ``data/raw-request.txt`` 对齐的 B 站 Web 请求头。"""
return {
"accept": "*/*",
"accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"cookie": cfg["cookie"] if auth else strip_identity(cfg["cookie"]),
"cookie": cookie_header(str(cfg.get("cookie") or ""), auth=auth),
"dnt": "1",
"origin": "https://space.bilibili.com",
"priority": "u=1, i",