style: 统一 Ruff 代码格式
This commit is contained in:
@@ -15,7 +15,9 @@ class CookieEnricher:
|
||||
CSRF_API = "https://www.douyu.com/japi/carnival/nc/common/generateCsrf"
|
||||
CSRF_REFERER = "https://www.douyu.com/pages/live-peace-handbook/web/shop?ditchname=pass0&roomId=9263298"
|
||||
ACF_CCN_API = "https://www.douyu.com/curl/csrfNlApi/getCsrfCookie"
|
||||
ACF_CCN_REFERER = "https://www.douyu.com/pages/ord-task-center?clientType=web&panelSource=1&rid=0"
|
||||
ACF_CCN_REFERER = (
|
||||
"https://www.douyu.com/pages/ord-task-center?clientType=web&panelSource=1&rid=0"
|
||||
)
|
||||
TIMEOUT = (5, 10)
|
||||
RETRIES = 3
|
||||
|
||||
@@ -47,25 +49,27 @@ class CookieEnricher:
|
||||
logger.warning(f"补CK失败 {attempt}/{max_attempts}: {e}")
|
||||
if attempt < max_attempts:
|
||||
self.sleep_interruptible(1)
|
||||
raise ValueError(f"已重试 {max_attempts} 次仍未补齐CK: {last_error}") from last_error
|
||||
raise ValueError(
|
||||
f"已重试 {max_attempts} 次仍未补齐CK: {last_error}"
|
||||
) from last_error
|
||||
|
||||
def generate_csrf_cookie(self) -> str:
|
||||
"""访问 generateCsrf 接口,从 Set-Cookie 中同步 cvl_csrf_token。"""
|
||||
logger.info("补齐CSRF Cookie...")
|
||||
headers = {
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.6,en;q=0.5',
|
||||
'Origin': 'https://www.douyu.com',
|
||||
'Referer': self.CSRF_REFERER,
|
||||
'Sec-Fetch-Dest': 'empty',
|
||||
'Sec-Fetch-Mode': 'cors',
|
||||
'Sec-Fetch-Site': 'same-origin',
|
||||
"Accept": "application/json, text/plain, */*",
|
||||
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.6,en;q=0.5",
|
||||
"Origin": "https://www.douyu.com",
|
||||
"Referer": self.CSRF_REFERER,
|
||||
"Sec-Fetch-Dest": "empty",
|
||||
"Sec-Fetch-Mode": "cors",
|
||||
"Sec-Fetch-Site": "same-origin",
|
||||
# 覆盖登录接口的默认表单头,尽量贴近浏览器抓包。
|
||||
'Content-Type': None,
|
||||
'X-Requested-With': None,
|
||||
"Content-Type": None,
|
||||
"X-Requested-With": None,
|
||||
}
|
||||
response = self.request(
|
||||
'post',
|
||||
"post",
|
||||
self.CSRF_API,
|
||||
headers=headers,
|
||||
timeout=self.TIMEOUT,
|
||||
@@ -82,11 +86,11 @@ class CookieEnricher:
|
||||
preview = body[:200].replace("\n", "\\n")
|
||||
raise ValueError(f"生成CSRF失败: 响应不是有效 JSON: {preview}") from exc
|
||||
|
||||
if payload.get('error') != 0:
|
||||
if payload.get("error") != 0:
|
||||
raise ValueError(f"生成CSRF失败: {payload.get('msg', '未知错误')}")
|
||||
|
||||
cookies = self.session.cookies.get_dict()
|
||||
csrf_token = cookies.get('cvl_csrf_token', '')
|
||||
csrf_token = cookies.get("cvl_csrf_token", "")
|
||||
if not csrf_token:
|
||||
raise ValueError("生成CSRF失败: 响应没有 cvl_csrf_token")
|
||||
|
||||
@@ -97,25 +101,25 @@ class CookieEnricher:
|
||||
"""访问 getCsrfCookie 接口,从 Set-Cookie 中同步 acf_ccn。"""
|
||||
logger.info("补齐 acf_ccn Cookie...")
|
||||
headers = {
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Pragma': 'no-cache',
|
||||
'Priority': 'u=1, i',
|
||||
'Referer': self.ACF_CCN_REFERER,
|
||||
'Sec-CH-UA': '"Google Chrome";v="149", "Chromium";v="149", "Not)A;Brand";v="24"',
|
||||
'Sec-CH-UA-Mobile': '?0',
|
||||
'Sec-CH-UA-Platform': '"macOS"',
|
||||
'Sec-Fetch-Dest': 'empty',
|
||||
'Sec-Fetch-Mode': 'cors',
|
||||
'Sec-Fetch-Site': 'same-origin',
|
||||
"Accept": "application/json, text/plain, */*",
|
||||
"Accept-Language": "zh-CN,zh;q=0.9",
|
||||
"Cache-Control": "no-cache",
|
||||
"Pragma": "no-cache",
|
||||
"Priority": "u=1, i",
|
||||
"Referer": self.ACF_CCN_REFERER,
|
||||
"Sec-CH-UA": '"Google Chrome";v="149", "Chromium";v="149", "Not)A;Brand";v="24"',
|
||||
"Sec-CH-UA-Mobile": "?0",
|
||||
"Sec-CH-UA-Platform": '"macOS"',
|
||||
"Sec-Fetch-Dest": "empty",
|
||||
"Sec-Fetch-Mode": "cors",
|
||||
"Sec-Fetch-Site": "same-origin",
|
||||
# 该接口抓包没有 Origin、表单 Content-Type 和 X-Requested-With。
|
||||
'Origin': None,
|
||||
'Content-Type': None,
|
||||
'X-Requested-With': None,
|
||||
"Origin": None,
|
||||
"Content-Type": None,
|
||||
"X-Requested-With": None,
|
||||
}
|
||||
response = self.request(
|
||||
'get',
|
||||
"get",
|
||||
self.ACF_CCN_API,
|
||||
headers=headers,
|
||||
timeout=self.TIMEOUT,
|
||||
@@ -123,10 +127,10 @@ class CookieEnricher:
|
||||
response.raise_for_status()
|
||||
|
||||
cookies = self.session.cookies.get_dict()
|
||||
acf_ccn = cookies.get('acf_ccn', '') or response.cookies.get('acf_ccn', '')
|
||||
if acf_ccn and not cookies.get('acf_ccn'):
|
||||
acf_ccn = cookies.get("acf_ccn", "") or response.cookies.get("acf_ccn", "")
|
||||
if acf_ccn and not cookies.get("acf_ccn"):
|
||||
# 极少数情况下响应 Cookie 未合入 get_dict,手动补到斗鱼域名下。
|
||||
self.session.cookies.set('acf_ccn', acf_ccn, domain='.douyu.com', path='/')
|
||||
self.session.cookies.set("acf_ccn", acf_ccn, domain=".douyu.com", path="/")
|
||||
|
||||
if not acf_ccn:
|
||||
raise ValueError("补齐 acf_ccn 失败: 响应没有 acf_ccn")
|
||||
|
||||
Reference in New Issue
Block a user