- 新增 activity_client.py:封装斗鱼活动/兑换/充值/送礼接口 - 新增 cookie_utils.py:Cookie 解析与规范化工具 - 新增 douyu_service/douyu_runner:斗鱼任务服务层与批量执行器 - 新增 douyu 路由:任务类型查询、账号列表、配置管理、商品管理、批量任务、WebSocket 日志 - 新增 models/schemas:DouyuTask/DouyuConfig/DouyuGoodsSnapshot 模型,Account 扩展点数/鱼翅/绑定状态等字段 - 新增数据库迁移:斗鱼活动相关表与 accounts 字段补充 - 新增前端 DouyuTasksPage 任务操作台页面 - 兑换商品请求添加 sec-ch-ua 反检测头 - 兑换商品支持最多 8 次重试 + csrf_token 自动刷新 - 注册 douyu:task / douyu:config 权限点 - 侧边栏新增斗鱼分组与任务操作台菜单入口
62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
"""斗鱼 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 key:
|
|
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_douyu_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 ""
|
|
pairs = cookie_pairs(cookie) if isinstance(cookie, str) else 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
|