feat(douyu): 完成账号平台阶段一到四并接入最新协议
This commit is contained in:
+196
-36
@@ -118,8 +118,17 @@ class DouyuActivityClient:
|
||||
ESPORTS_EXCHANGE_API = (
|
||||
"https://www.douyu.com/wgapi/ordnc/activity/peacematch/exchange"
|
||||
)
|
||||
GOLD_QR_API = "https://cz.douyu.com/m/gold/getQrCode"
|
||||
GOLD_ACCOUNT_API = "https://cz.douyu.com/item/gold/account"
|
||||
# 2026-11-21 抓包:鱼翅充值/余额已迁到 www.douyu.com 的 japi/web/gold。
|
||||
GOLD_INIT_API = "https://www.douyu.com/japi/web/gold/init"
|
||||
GOLD_QR_API = "https://www.douyu.com/japi/web/gold/getQrCode"
|
||||
GOLD_QR_STATE_API = "https://www.douyu.com/japi/web/gold/getQrState"
|
||||
GOLD_ACCOUNT_API = "https://www.douyu.com/japi/web/gold/account"
|
||||
CREDIT_BALANCE_DETAIL_API = (
|
||||
"https://www.douyu.com/japi/oms/web/peace/credit/balanceDetail"
|
||||
)
|
||||
CREDIT_JOURNAL_API = (
|
||||
"https://www.douyu.com/japi/oms/web/peace/credit/colJournal"
|
||||
)
|
||||
EXCHANGE_BALANCE_API = "https://www.douyu.com/wjapi/nc/exchange/fim"
|
||||
DONATE_API = "https://www.douyu.com/japi/gift/donate/mainsite/v3"
|
||||
|
||||
@@ -349,16 +358,21 @@ class DouyuActivityClient:
|
||||
|
||||
def acf_ccn(self, *, refresh_subscribe: bool = True) -> str:
|
||||
"""获取 acf_ccn;必要时通过订阅接口刷新一次。"""
|
||||
response = self._request(
|
||||
"get",
|
||||
self.ACF_CCN_API,
|
||||
source="获取 acf_ccn",
|
||||
headers={
|
||||
"Origin": "",
|
||||
"Referer": "https://www.douyu.com/pages/ord-task-center?clientType=web&panelSource=1&rid=0",
|
||||
},
|
||||
)
|
||||
# 2026-11-21 抓包:普通活动和积分查询直接复用 Cookie 中的 acf_ccn,
|
||||
# 不会每次先请求 getCsrfCookie。只有缺失时才补,避免无意义请求和限流。
|
||||
ctn = cookie_value(self.cookie, "acf_ccn")
|
||||
response: requests.Response | None = None
|
||||
if not ctn:
|
||||
response = self._request(
|
||||
"get",
|
||||
self.ACF_CCN_API,
|
||||
source="获取 acf_ccn",
|
||||
headers={
|
||||
"Origin": "",
|
||||
"Referer": "https://www.douyu.com/pages/ord-task-center?clientType=web&panelSource=1&rid=0",
|
||||
},
|
||||
)
|
||||
ctn = cookie_value(self.cookie, "acf_ccn")
|
||||
if not ctn:
|
||||
response = self._request(
|
||||
"get",
|
||||
@@ -368,7 +382,8 @@ class DouyuActivityClient:
|
||||
)
|
||||
ctn = cookie_value(self.cookie, "acf_ccn")
|
||||
if not ctn:
|
||||
raise DouyuActivityError(f"获取 acf_ccn 失败: {response.text[:200]}")
|
||||
detail = response.text[:200] if response is not None else "Cookie 缺失"
|
||||
raise DouyuActivityError(f"获取 acf_ccn 失败: {detail}")
|
||||
|
||||
if refresh_subscribe:
|
||||
payload = self._request_json(
|
||||
@@ -832,33 +847,104 @@ class DouyuActivityClient:
|
||||
"raw": payload,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _gold_qr_product(amount: int) -> tuple[str, int]:
|
||||
"""2026-11-21 新链路按商品面值选择 productId,普通金额退回 1 鱼翅商品。"""
|
||||
fixed_products = {
|
||||
1: "DYTV_Product_Gold_1",
|
||||
10: "DYTV_Product_Gold_10",
|
||||
50: "DYTV_Product_Gold_50",
|
||||
100: "DYTV_Product_Gold_100",
|
||||
500: "DYTV_Product_Gold_500",
|
||||
1000: "DYTV_Product_Gold_1000",
|
||||
5000: "DYTV_Product_Gold_5000",
|
||||
10000: "DYTV_Product_Gold_10000",
|
||||
}
|
||||
value = max(1, int(amount))
|
||||
if value in fixed_products:
|
||||
return fixed_products[value], 1
|
||||
return fixed_products[1], value
|
||||
|
||||
def create_gold_qr(
|
||||
self, *, amount: int, pay_type: int = 1, product_id: str = "DYTV_Product_Gold_1"
|
||||
self,
|
||||
*,
|
||||
amount: int,
|
||||
pay_type: int = 1,
|
||||
) -> dict[str, Any]:
|
||||
"""生成鱼翅充值支付二维码。"""
|
||||
payload = self._request_json(
|
||||
"""按 2026-11-21 最新链路生成鱼翅充值支付二维码。"""
|
||||
ctn = self.acf_ccn(refresh_subscribe=False)
|
||||
init_payload = self._request_json(
|
||||
"post",
|
||||
self.GOLD_QR_API,
|
||||
"生成鱼翅充值码",
|
||||
self.GOLD_INIT_API,
|
||||
"初始化鱼翅充值",
|
||||
data={
|
||||
"pay_type": str(pay_type),
|
||||
"product_id": product_id,
|
||||
"number": str(amount),
|
||||
"toname": "",
|
||||
"source": "dy_box",
|
||||
"ctn": ctn,
|
||||
"scene": "web",
|
||||
},
|
||||
headers={
|
||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||
"Origin": "https://cz.douyu.com",
|
||||
"Referer": "https://cz.douyu.com/",
|
||||
"Referer": "https://www.douyu.com/",
|
||||
},
|
||||
)
|
||||
if init_payload.get("code") not in (0, "0") or not init_payload.get(
|
||||
"success", True
|
||||
):
|
||||
raise DouyuActivityError(init_payload.get("msg") or "初始化鱼翅充值失败")
|
||||
|
||||
product_id, number = self._gold_qr_product(amount)
|
||||
payload = self._request_json(
|
||||
"get",
|
||||
self.GOLD_QR_API,
|
||||
"生成鱼翅充值码",
|
||||
params={
|
||||
"payType": str(pay_type),
|
||||
"productId": product_id,
|
||||
"number": str(number),
|
||||
"source": "",
|
||||
"toName": "",
|
||||
},
|
||||
headers={"Referer": "https://www.douyu.com/"},
|
||||
)
|
||||
if payload.get("code") not in (0, "0") or not payload.get("success", True):
|
||||
raise DouyuActivityError(payload.get("msg") or "生成鱼翅充值码失败")
|
||||
data = payload.get("data") or {}
|
||||
pay_url = data.get("code_url") or payload.get("code_url") or ""
|
||||
token = data.get("token") or payload.get("token") or ""
|
||||
if not pay_url:
|
||||
raise DouyuActivityError(payload.get("msg") or "生成鱼翅充值码失败")
|
||||
return {"pay_url": pay_url, "token": token, "raw": payload}
|
||||
return {
|
||||
"pay_url": pay_url,
|
||||
"token": token,
|
||||
"polling_url": data.get("polling_url") or "",
|
||||
"product_id": product_id,
|
||||
"product_number": number,
|
||||
"init": {
|
||||
"uid": (init_payload.get("data") or {}).get("uid"),
|
||||
"nickname": (init_payload.get("data") or {}).get("nickName"),
|
||||
"scene": (init_payload.get("data") or {}).get("scene"),
|
||||
},
|
||||
"raw": payload,
|
||||
}
|
||||
|
||||
def query_gold_qr_state(self, token: str) -> dict[str, Any]:
|
||||
"""查询 2026-11-21 新版鱼翅支付码状态。"""
|
||||
payload = self._request_json(
|
||||
"get",
|
||||
self.GOLD_QR_STATE_API,
|
||||
"查询鱼翅充值状态",
|
||||
params={"token": token},
|
||||
headers={"Referer": "https://www.douyu.com/"},
|
||||
)
|
||||
if payload.get("code") not in (0, "0") or not payload.get("success", True):
|
||||
raise DouyuActivityError(payload.get("msg") or "查询鱼翅充值状态失败")
|
||||
data = payload.get("data") or {}
|
||||
return {
|
||||
"state": data.get("state"),
|
||||
"nickname": data.get("nickname") or "",
|
||||
"quantity": data.get("quantity"),
|
||||
"order_id": data.get("orderId") or "",
|
||||
"raw": payload,
|
||||
}
|
||||
|
||||
def _donate_gift(
|
||||
self,
|
||||
@@ -925,17 +1011,29 @@ class DouyuActivityClient:
|
||||
source=f"赠送{gift_name}",
|
||||
)
|
||||
|
||||
def query_points(self, *, uid: str, ctn: str) -> dict[str, Any]:
|
||||
"""查询手册积分余额。"""
|
||||
def query_points(self, *, uid: str, ctn: str = "") -> dict[str, Any]:
|
||||
"""查询手册积分余额(2026-11-21 对齐浏览器 multipart 请求)。"""
|
||||
if ctn:
|
||||
self._set_cookie_value("acf_ccn", ctn)
|
||||
payload = self._request_json(
|
||||
"post",
|
||||
self.CREDIT_BALANCE_API,
|
||||
"查询手册积分",
|
||||
json={"uid": str(uid), "ctn": ctn},
|
||||
headers={"Content-Type": "application/json;charset=UTF-8"},
|
||||
files=self._multipart_fields({"uid": str(uid)}),
|
||||
headers={
|
||||
"Referer": (
|
||||
"https://www.douyu.com/pages/live-peace-handbook/web/"
|
||||
"point-detail?ditchname=pass0"
|
||||
)
|
||||
},
|
||||
)
|
||||
data = payload.get("data") or {}
|
||||
return {"points": data.get("balance"), "raw": payload}
|
||||
return {
|
||||
"points": data.get("balance"),
|
||||
"recently_expiring_points": data.get("recentlyWillExpireBalance"),
|
||||
"will_expire_time": data.get("willExpireTime"),
|
||||
"raw": payload,
|
||||
}
|
||||
|
||||
def query_points_diff(self, *, uid: str, ctn: str) -> dict[str, Any]:
|
||||
"""查询手册积分差额。"""
|
||||
@@ -949,6 +1047,60 @@ class DouyuActivityClient:
|
||||
data = payload.get("data") or {}
|
||||
return {"diff": data.get("diff"), "raw": payload}
|
||||
|
||||
def query_points_expirations(
|
||||
self,
|
||||
*,
|
||||
user_id: str,
|
||||
current: int = 0,
|
||||
page_size: int = 20,
|
||||
) -> dict[str, Any]:
|
||||
"""查询积分到期批次(2026-11-21 页面 point-detail 使用)。"""
|
||||
payload = self._request_json(
|
||||
"post",
|
||||
self.CREDIT_BALANCE_DETAIL_API,
|
||||
"查询积分到期批次",
|
||||
json={
|
||||
"userId": int(user_id),
|
||||
"current": max(0, int(current)),
|
||||
"pageSize": max(1, int(page_size)),
|
||||
},
|
||||
headers={
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
"Referer": (
|
||||
"https://www.douyu.com/pages/live-peace-handbook/web/"
|
||||
"point-detail?ditchname=pass0"
|
||||
),
|
||||
},
|
||||
)
|
||||
return {"data": payload.get("data") or {}, "raw": payload}
|
||||
|
||||
def query_points_ledger(
|
||||
self,
|
||||
*,
|
||||
uid: str,
|
||||
page_num: int = 1,
|
||||
page_size: int = 15,
|
||||
) -> dict[str, Any]:
|
||||
"""查询积分收支流水,用于赠送/兑换后的精确核对。"""
|
||||
payload = self._request_json(
|
||||
"post",
|
||||
self.CREDIT_JOURNAL_API,
|
||||
"查询积分流水",
|
||||
json={
|
||||
"uid": str(uid),
|
||||
"pageNum": max(1, int(page_num)),
|
||||
"pageSize": max(1, int(page_size)),
|
||||
},
|
||||
headers={
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
"Referer": (
|
||||
"https://www.douyu.com/pages/live-peace-handbook/web/"
|
||||
"point-detail?ditchname=pass0"
|
||||
),
|
||||
},
|
||||
)
|
||||
return {"data": payload.get("data") or {}, "raw": payload}
|
||||
|
||||
def list_goods(self, *, manual_id: str, rid: str) -> dict[str, Any]:
|
||||
"""刷新活动商品列表。"""
|
||||
payload = self._request_json(
|
||||
@@ -1409,18 +1561,26 @@ class DouyuActivityClient:
|
||||
return {"data": payload.get("data") or {}, "raw": payload}
|
||||
|
||||
def gold_account(self) -> dict[str, Any]:
|
||||
"""查询 cz 侧鱼翅余额。"""
|
||||
"""查询鱼翅余额(2026-11-21 使用 www 新接口)。"""
|
||||
payload = self._request_json(
|
||||
"get",
|
||||
self.GOLD_ACCOUNT_API,
|
||||
"查询鱼翅余额",
|
||||
headers={
|
||||
"Origin": "https://cz.douyu.com",
|
||||
"Referer": "https://cz.douyu.com/",
|
||||
},
|
||||
headers={"Referer": "https://www.douyu.com/"},
|
||||
)
|
||||
data = payload.get("data") or {}
|
||||
return {"gold": data.get("gold"), "raw": payload}
|
||||
amount_cents = data.get("amount")
|
||||
if amount_cents is None:
|
||||
# 兼容部分旧容器仍返回 gold 字段。
|
||||
gold = data.get("gold")
|
||||
return {"gold": gold, "amount_cents": None, "raw": payload}
|
||||
try:
|
||||
cents = int(amount_cents)
|
||||
except (TypeError, ValueError):
|
||||
gold = None
|
||||
else:
|
||||
gold = cents // 100 if cents % 100 == 0 else cents / 100
|
||||
return {"gold": gold, "amount_cents": amount_cents, "raw": payload}
|
||||
|
||||
def exchange_balance(self) -> dict[str, Any]:
|
||||
"""查询钱包兑换中心余额。"""
|
||||
|
||||
Reference in New Issue
Block a user