xiaoyukkkk commited on
Commit
d72552e
·
verified ·
1 Parent(s): f8b6b2a

Upload 9 files

Browse files
Files changed (2) hide show
  1. core/account.py +7 -0
  2. core/google_api.py +14 -12
core/account.py CHANGED
@@ -257,6 +257,13 @@ class MultiAccountManager:
257
  self._session_locks[conv_key] = asyncio.Lock()
258
  return self._session_locks[conv_key]
259
 
 
 
 
 
 
 
 
260
  def add_account(self, config: AccountConfig, http_client, user_agent: str, account_failure_threshold: int, rate_limit_cooldown_seconds: int, global_stats: dict):
261
  """添加账户"""
262
  manager = AccountManager(config, http_client, user_agent, account_failure_threshold, rate_limit_cooldown_seconds)
 
257
  self._session_locks[conv_key] = asyncio.Lock()
258
  return self._session_locks[conv_key]
259
 
260
+ def update_http_client(self, http_client):
261
+ """更新所有账户使用的 http_client(用于代理变更后重建客户端)"""
262
+ for account_mgr in self.accounts.values():
263
+ account_mgr.http_client = http_client
264
+ if account_mgr.jwt_manager is not None:
265
+ account_mgr.jwt_manager.http_client = http_client
266
+
267
  def add_account(self, config: AccountConfig, http_client, user_agent: str, account_failure_threshold: int, rate_limit_cooldown_seconds: int, global_stats: dict):
268
  """添加账户"""
269
  manager = AccountManager(config, http_client, user_agent, account_failure_threshold, rate_limit_cooldown_seconds)
core/google_api.py CHANGED
@@ -69,8 +69,9 @@ async def make_request_with_jwt_retry(
69
  headers = get_common_headers(jwt, user_agent)
70
 
71
  # 合并用户提供的headers(如果有)
72
- if "headers" in kwargs:
73
- headers.update(kwargs.pop("headers"))
 
74
 
75
  # 发起请求
76
  if method.upper() == "GET":
@@ -84,8 +85,8 @@ async def make_request_with_jwt_retry(
84
  if resp.status_code == 401:
85
  jwt = await account_mgr.get_jwt(request_id)
86
  headers = get_common_headers(jwt, user_agent)
87
- if "headers" in kwargs:
88
- headers.update(kwargs["headers"])
89
 
90
  if method.upper() == "GET":
91
  resp = await http_client.get(url, headers=headers, **kwargs)
@@ -252,10 +253,9 @@ async def download_image_with_jwt(
252
 
253
  for attempt in range(max_retries):
254
  try:
255
- # 3分钟超时(180秒)
256
- async with asyncio.timeout(180):
257
- # 使用通用JWT刷新函数
258
- resp = await make_request_with_jwt_retry(
259
  account_mgr,
260
  "GET",
261
  url,
@@ -263,11 +263,13 @@ async def download_image_with_jwt(
263
  user_agent,
264
  request_id,
265
  follow_redirects=True
266
- )
 
 
267
 
268
- resp.raise_for_status()
269
- logger.info(f"[IMAGE] [{account_mgr.config.account_id}] [req_{request_id}] 图片下载成功: {file_id[:8]}... ({len(resp.content)} bytes)")
270
- return resp.content
271
 
272
  except asyncio.TimeoutError:
273
  logger.warning(f"[IMAGE] [{account_mgr.config.account_id}] [req_{request_id}] 图片下载超时 (尝试 {attempt + 1}/{max_retries}): {file_id[:8]}...")
 
69
  headers = get_common_headers(jwt, user_agent)
70
 
71
  # 合并用户提供的headers(如果有)
72
+ extra_headers = kwargs.pop("headers", None)
73
+ if extra_headers:
74
+ headers.update(extra_headers)
75
 
76
  # 发起请求
77
  if method.upper() == "GET":
 
85
  if resp.status_code == 401:
86
  jwt = await account_mgr.get_jwt(request_id)
87
  headers = get_common_headers(jwt, user_agent)
88
+ if extra_headers:
89
+ headers.update(extra_headers)
90
 
91
  if method.upper() == "GET":
92
  resp = await http_client.get(url, headers=headers, **kwargs)
 
253
 
254
  for attempt in range(max_retries):
255
  try:
256
+ # 3分钟超时(180秒)- 使用 wait_for 兼容 Python 3.10
257
+ resp = await asyncio.wait_for(
258
+ make_request_with_jwt_retry(
 
259
  account_mgr,
260
  "GET",
261
  url,
 
263
  user_agent,
264
  request_id,
265
  follow_redirects=True
266
+ ),
267
+ timeout=180
268
+ )
269
 
270
+ resp.raise_for_status()
271
+ logger.info(f"[IMAGE] [{account_mgr.config.account_id}] [req_{request_id}] 图片下载成功: {file_id[:8]}... ({len(resp.content)} bytes)")
272
+ return resp.content
273
 
274
  except asyncio.TimeoutError:
275
  logger.warning(f"[IMAGE] [{account_mgr.config.account_id}] [req_{request_id}] 图片下载超时 (尝试 {attempt + 1}/{max_retries}): {file_id[:8]}...")