|
@@ -43,18 +43,66 @@ class BaijiahaoPublisher(BasePublisher):
|
|
|
try:
|
|
try:
|
|
|
# 解析 cookies
|
|
# 解析 cookies
|
|
|
cookie_list = self.parse_cookies(cookies)
|
|
cookie_list = self.parse_cookies(cookies)
|
|
|
- cookie_str = '; '.join([f"{c['name']}={c['value']}" for c in cookie_list])
|
|
|
|
|
|
|
+ cookie_dict = {c['name']: c['value'] for c in cookie_list}
|
|
|
|
|
+
|
|
|
|
|
+ # 重要:百家号需要先访问主页建立会话上下文
|
|
|
|
|
+ print(f"[{self.platform_name}] 第一步:访问主页建立会话...")
|
|
|
|
|
+ session_headers = {
|
|
|
|
|
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
|
|
|
|
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
|
|
|
+ # Cookie 由 session 管理,不手动设置
|
|
|
|
|
+ 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
|
|
|
+ 'Accept-Encoding': 'gzip, deflate, br',
|
|
|
|
|
+ 'Connection': 'keep-alive',
|
|
|
|
|
+ 'Upgrade-Insecure-Requests': '1',
|
|
|
|
|
+ 'Sec-Fetch-Dest': 'document',
|
|
|
|
|
+ 'Sec-Fetch-Mode': 'navigate',
|
|
|
|
|
+ 'Sec-Fetch-Site': 'none',
|
|
|
|
|
+ 'Sec-Fetch-User': '?1',
|
|
|
|
|
+ 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
|
|
|
|
|
+ 'sec-ch-ua-mobile': '?0',
|
|
|
|
|
+ 'sec-ch-ua-platform': '"Windows"'
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
headers = {
|
|
headers = {
|
|
|
'Accept': 'application/json, text/plain, */*',
|
|
'Accept': 'application/json, text/plain, */*',
|
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
|
- 'Cookie': cookie_str,
|
|
|
|
|
- 'Referer': 'https://baijiahao.baidu.com/builder/rc/home'
|
|
|
|
|
|
|
+ # Cookie 由 session 管理,不手动设置
|
|
|
|
|
+ 'Referer': 'https://baijiahao.baidu.com/builder/rc/home',
|
|
|
|
|
+ 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
|
|
|
+ 'Accept-Encoding': 'gzip, deflate, br',
|
|
|
|
|
+ 'Connection': 'keep-alive',
|
|
|
|
|
+ 'Sec-Fetch-Dest': 'empty',
|
|
|
|
|
+ 'Sec-Fetch-Mode': 'cors',
|
|
|
|
|
+ 'Sec-Fetch-Site': 'same-origin',
|
|
|
|
|
+ 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
|
|
|
|
|
+ 'sec-ch-ua-mobile': '?0',
|
|
|
|
|
+ 'sec-ch-ua-platform': '"Windows"'
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async with aiohttp.ClientSession() as session:
|
|
|
|
|
|
|
+ # 使用 cookies 参数初始化 session,让 aiohttp 自动管理 cookie 更新
|
|
|
|
|
+ async with aiohttp.ClientSession(cookies=cookie_dict) as session:
|
|
|
|
|
+ # 步骤 0: 先访问主页建立会话上下文(关键步骤!)
|
|
|
|
|
+ print(f"[{self.platform_name}] [0/4] 访问主页建立会话上下文...")
|
|
|
|
|
+ async with session.get(
|
|
|
|
|
+ 'https://baijiahao.baidu.com/builder/rc/home',
|
|
|
|
|
+ headers=session_headers,
|
|
|
|
|
+ timeout=aiohttp.ClientTimeout(total=30)
|
|
|
|
|
+ ) as home_response:
|
|
|
|
|
+ home_status = home_response.status
|
|
|
|
|
+ print(f"[{self.platform_name}] 主页访问状态: {home_status}")
|
|
|
|
|
+
|
|
|
|
|
+ # 获取响应头中的新cookies(如果有)
|
|
|
|
|
+ if 'Set-Cookie' in home_response.headers:
|
|
|
|
|
+ new_cookies = home_response.headers['Set-Cookie']
|
|
|
|
|
+ print(f"[{self.platform_name}] 获取到新的会话Cookie")
|
|
|
|
|
+ # 这里可以处理新的cookies,但暂时跳过复杂处理
|
|
|
|
|
+
|
|
|
|
|
+ # 短暂等待确保会话建立
|
|
|
|
|
+ await asyncio.sleep(1)
|
|
|
|
|
+
|
|
|
# 步骤 1: 获取账号基本信息
|
|
# 步骤 1: 获取账号基本信息
|
|
|
- print(f"[{self.platform_name}] [1/3] 调用 appinfo API...")
|
|
|
|
|
|
|
+ print(f"[{self.platform_name}] [1/4] 调用 appinfo API...")
|
|
|
async with session.get(
|
|
async with session.get(
|
|
|
'https://baijiahao.baidu.com/builder/app/appinfo',
|
|
'https://baijiahao.baidu.com/builder/app/appinfo',
|
|
|
headers=headers,
|
|
headers=headers,
|
|
@@ -79,6 +127,41 @@ class BaijiahaoPublisher(BasePublisher):
|
|
|
"need_login": True
|
|
"need_login": True
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ # errno 10001402 表示分散认证问题,尝试重新访问主页后重试
|
|
|
|
|
+ if errno == 10001402:
|
|
|
|
|
+ print(f"[{self.platform_name}] 检测到分散认证问题,尝试重新访问主页...")
|
|
|
|
|
+ await asyncio.sleep(2)
|
|
|
|
|
+
|
|
|
|
|
+ # 重新访问主页
|
|
|
|
|
+ async with session.get(
|
|
|
|
|
+ 'https://baijiahao.baidu.com/builder/rc/home',
|
|
|
|
|
+ headers=session_headers,
|
|
|
|
|
+ timeout=aiohttp.ClientTimeout(total=30)
|
|
|
|
|
+ ) as retry_home_response:
|
|
|
|
|
+ print(f"[{self.platform_name}] 重新访问主页状态: {retry_home_response.status}")
|
|
|
|
|
+
|
|
|
|
|
+ await asyncio.sleep(1)
|
|
|
|
|
+
|
|
|
|
|
+ # 重试 API 调用
|
|
|
|
|
+ async with session.get(
|
|
|
|
|
+ 'https://baijiahao.baidu.com/builder/app/appinfo',
|
|
|
|
|
+ headers=headers,
|
|
|
|
|
+ timeout=aiohttp.ClientTimeout(total=30)
|
|
|
|
|
+ ) as retry_response:
|
|
|
|
|
+ retry_result = await retry_response.json()
|
|
|
|
|
+
|
|
|
|
|
+ if retry_result.get('errno') == 0:
|
|
|
|
|
+ print(f"[{self.platform_name}] 分散认证问题已解决")
|
|
|
|
|
+ # 使用重试成功的结果继续处理
|
|
|
|
|
+ appinfo_result = retry_result
|
|
|
|
|
+ else:
|
|
|
|
|
+ print(f"[{self.platform_name}] 重试仍然失败")
|
|
|
|
|
+ return {
|
|
|
|
|
+ "success": False,
|
|
|
|
|
+ "error": f"分散认证问题: {error_msg}",
|
|
|
|
|
+ "need_login": True
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return {
|
|
return {
|
|
|
"success": False,
|
|
"success": False,
|
|
|
"error": error_msg,
|
|
"error": error_msg,
|
|
@@ -146,7 +229,7 @@ class BaijiahaoPublisher(BasePublisher):
|
|
|
headers={
|
|
headers={
|
|
|
'accept': '*/*',
|
|
'accept': '*/*',
|
|
|
'user-agent': 'PostmanRuntime/7.51.0',
|
|
'user-agent': 'PostmanRuntime/7.51.0',
|
|
|
- 'cookie': cookie_str,
|
|
|
|
|
|
|
+ # cookie 由 session 管理
|
|
|
'referer': 'https://baijiahao.baidu.com/builder/rc/content',
|
|
'referer': 'https://baijiahao.baidu.com/builder/rc/content',
|
|
|
'connection': 'keep-alive',
|
|
'connection': 'keep-alive',
|
|
|
'accept-encoding': 'gzip, deflate, br',
|
|
'accept-encoding': 'gzip, deflate, br',
|
|
@@ -165,10 +248,17 @@ class BaijiahaoPublisher(BasePublisher):
|
|
|
print(f"[{self.platform_name}] 分散认证问题 (errno=10001402),3秒后重试...")
|
|
print(f"[{self.platform_name}] 分散认证问题 (errno=10001402),3秒后重试...")
|
|
|
await asyncio.sleep(3)
|
|
await asyncio.sleep(3)
|
|
|
|
|
|
|
|
- # 重试一次
|
|
|
|
|
|
|
+ # 重试一次,使用更完整的请求头
|
|
|
|
|
+ retry_headers = headers.copy()
|
|
|
|
|
+ retry_headers.update({
|
|
|
|
|
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
|
|
|
|
+ 'Cache-Control': 'max-age=0',
|
|
|
|
|
+ 'Upgrade-Insecure-Requests': '1',
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
async with session.get(
|
|
async with session.get(
|
|
|
list_url,
|
|
list_url,
|
|
|
- headers=headers,
|
|
|
|
|
|
|
+ headers=retry_headers,
|
|
|
timeout=aiohttp.ClientTimeout(total=30)
|
|
timeout=aiohttp.ClientTimeout(total=30)
|
|
|
) as retry_response:
|
|
) as retry_response:
|
|
|
retry_text = await retry_response.text()
|
|
retry_text = await retry_response.text()
|
|
@@ -749,19 +839,28 @@ class BaijiahaoPublisher(BasePublisher):
|
|
|
try:
|
|
try:
|
|
|
# 解析 cookies
|
|
# 解析 cookies
|
|
|
cookie_list = self.parse_cookies(cookies)
|
|
cookie_list = self.parse_cookies(cookies)
|
|
|
- cookie_str = '; '.join([f"{c['name']}={c['value']}" for c in cookie_list])
|
|
|
|
|
|
|
+ cookie_dict = {c['name']: c['value'] for c in cookie_list}
|
|
|
|
|
|
|
|
headers = {
|
|
headers = {
|
|
|
'Accept': 'application/json, text/plain, */*',
|
|
'Accept': 'application/json, text/plain, */*',
|
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
|
- 'Cookie': cookie_str,
|
|
|
|
|
- 'Referer': 'https://baijiahao.baidu.com/builder/rc/content'
|
|
|
|
|
|
|
+ # Cookie 由 session 管理
|
|
|
|
|
+ 'Referer': 'https://baijiahao.baidu.com/builder/rc/content',
|
|
|
|
|
+ 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
|
|
|
+ 'Accept-Encoding': 'gzip, deflate, br',
|
|
|
|
|
+ 'Connection': 'keep-alive',
|
|
|
|
|
+ 'Sec-Fetch-Dest': 'empty',
|
|
|
|
|
+ 'Sec-Fetch-Mode': 'cors',
|
|
|
|
|
+ 'Sec-Fetch-Site': 'same-origin',
|
|
|
|
|
+ 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
|
|
|
|
|
+ 'sec-ch-ua-mobile': '?0',
|
|
|
|
|
+ 'sec-ch-ua-platform': '"Windows"'
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
# 计算起始位置
|
|
# 计算起始位置
|
|
|
start = page * page_size
|
|
start = page * page_size
|
|
|
|
|
|
|
|
- async with aiohttp.ClientSession() as session:
|
|
|
|
|
|
|
+ async with aiohttp.ClientSession(cookies=cookie_dict) as session:
|
|
|
print(f"[{self.platform_name}] 调用 article/lists API (start={start}, count={page_size})...")
|
|
print(f"[{self.platform_name}] 调用 article/lists API (start={start}, count={page_size})...")
|
|
|
|
|
|
|
|
async with session.get(
|
|
async with session.get(
|
|
@@ -850,17 +949,58 @@ class BaijiahaoPublisher(BasePublisher):
|
|
|
try:
|
|
try:
|
|
|
# 解析 cookies
|
|
# 解析 cookies
|
|
|
cookie_list = self.parse_cookies(cookies)
|
|
cookie_list = self.parse_cookies(cookies)
|
|
|
- cookie_str = '; '.join([f"{c['name']}={c['value']}" for c in cookie_list])
|
|
|
|
|
|
|
+ cookie_dict = {c['name']: c['value'] for c in cookie_list}
|
|
|
|
|
+
|
|
|
|
|
+ # 重要:百家号需要先访问主页建立会话上下文
|
|
|
|
|
+ session_headers = {
|
|
|
|
|
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
|
|
|
|
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
|
|
|
+ # Cookie 由 session 管理
|
|
|
|
|
+ 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
|
|
|
+ 'Accept-Encoding': 'gzip, deflate, br',
|
|
|
|
|
+ 'Connection': 'keep-alive',
|
|
|
|
|
+ 'Upgrade-Insecure-Requests': '1',
|
|
|
|
|
+ 'Sec-Fetch-Dest': 'document',
|
|
|
|
|
+ 'Sec-Fetch-Mode': 'navigate',
|
|
|
|
|
+ 'Sec-Fetch-Site': 'none',
|
|
|
|
|
+ 'Sec-Fetch-User': '?1',
|
|
|
|
|
+ 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
|
|
|
|
|
+ 'sec-ch-ua-mobile': '?0',
|
|
|
|
|
+ 'sec-ch-ua-platform': '"Windows"'
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
headers = {
|
|
headers = {
|
|
|
'Accept': 'application/json, text/plain, */*',
|
|
'Accept': 'application/json, text/plain, */*',
|
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
|
- 'Cookie': cookie_str,
|
|
|
|
|
- 'Referer': 'https://baijiahao.baidu.com/builder/rc/home'
|
|
|
|
|
|
|
+ # Cookie 由 session 管理
|
|
|
|
|
+ 'Referer': 'https://baijiahao.baidu.com/builder/rc/home',
|
|
|
|
|
+ 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
|
|
|
+ 'Accept-Encoding': 'gzip, deflate, br',
|
|
|
|
|
+ 'Connection': 'keep-alive',
|
|
|
|
|
+ 'Sec-Fetch-Dest': 'empty',
|
|
|
|
|
+ 'Sec-Fetch-Mode': 'cors',
|
|
|
|
|
+ 'Sec-Fetch-Site': 'same-origin',
|
|
|
|
|
+ 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
|
|
|
|
|
+ 'sec-ch-ua-mobile': '?0',
|
|
|
|
|
+ 'sec-ch-ua-platform': '"Windows"'
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async with aiohttp.ClientSession() as session:
|
|
|
|
|
- print(f"[{self.platform_name}] 调用 appinfo API 检查登录状态...")
|
|
|
|
|
|
|
+ async with aiohttp.ClientSession(cookies=cookie_dict) as session:
|
|
|
|
|
+ # 步骤 0: 先访问主页建立会话上下文(关键步骤!)
|
|
|
|
|
+ print(f"[{self.platform_name}] [0/2] 访问主页建立会话上下文...")
|
|
|
|
|
+ async with session.get(
|
|
|
|
|
+ 'https://baijiahao.baidu.com/builder/rc/home',
|
|
|
|
|
+ headers=session_headers,
|
|
|
|
|
+ timeout=aiohttp.ClientTimeout(total=30)
|
|
|
|
|
+ ) as home_response:
|
|
|
|
|
+ home_status = home_response.status
|
|
|
|
|
+ print(f"[{self.platform_name}] 主页访问状态: {home_status}")
|
|
|
|
|
+
|
|
|
|
|
+ # 短暂等待确保会话建立
|
|
|
|
|
+ await asyncio.sleep(1)
|
|
|
|
|
+
|
|
|
|
|
+ # 步骤 1: 调用 API 检查登录状态
|
|
|
|
|
+ print(f"[{self.platform_name}] [1/2] 调用 appinfo API 检查登录状态...")
|
|
|
|
|
|
|
|
async with session.get(
|
|
async with session.get(
|
|
|
'https://baijiahao.baidu.com/builder/app/appinfo',
|
|
'https://baijiahao.baidu.com/builder/app/appinfo',
|