"""虎牙 Cookie 规范化工具。""" from __future__ import annotations from collections.abc import Iterable, Mapping import requests def cookie_pairs(cookie: str) -> list[tuple[str, str]]: """把浏览器 Cookie 字符串拆成 key/value 对。""" pairs: list[tuple[str, str]] = [] for part in (cookie or "").split(";"): item = part.strip() if not item or "=" not in item: continue key, value = item.split("=", 1) key = key.strip() if not key: continue pairs.append((key, value.strip())) return pairs def normalize_cookie_pairs(pairs: Iterable[tuple[str, str]]) -> str: """按 Cookie key 去重,保留最后一次出现的值。""" ordered_keys: list[str] = [] values: dict[str, str] = {} for raw_key, raw_value in pairs: key = str(raw_key).strip() if not key: continue if key not in values: ordered_keys.append(key) values[key] = "" if raw_value is None else str(raw_value).strip() return "; ".join(f"{key}={values[key]}" for key in ordered_keys) def normalize_huya_cookie(cookie: str | Mapping[str, str] | requests.cookies.RequestsCookieJar | None) -> str: """把 Cookie 字符串、dict 或 CookieJar 转成去重后的浏览器 Cookie 字符串。""" if not cookie: return "" if isinstance(cookie, str): return normalize_cookie_pairs(cookie_pairs(cookie)) return normalize_cookie_pairs(cookie.items()) def cookie_value(cookie: str | Mapping[str, str] | requests.cookies.RequestsCookieJar | None, key: str) -> str: """从 Cookie 中读取 key;有重复时以最后一次出现为准。""" if not cookie or not key: return "" if isinstance(cookie, str): pairs = cookie_pairs(cookie) else: pairs = list(cookie.items()) value = "" for item_key, item_value in pairs: if item_key == key: value = "" if item_value is None else str(item_value).strip() return value