完成 Ruff 全量清理
This commit is contained in:
@@ -44,7 +44,7 @@ class CookieEnricher:
|
||||
self.generate_acf_ccn_cookie()
|
||||
logger.info("补CK完成")
|
||||
return
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
last_error = e
|
||||
logger.warning(f"补CK失败 {attempt}/{max_attempts}: {e}")
|
||||
if attempt < max_attempts:
|
||||
|
||||
@@ -5,7 +5,7 @@ import os
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import requests
|
||||
from loguru import logger
|
||||
@@ -68,8 +68,8 @@ class EmailVerifier:
|
||||
f"{self.roundcube_url}?_task=logout",
|
||||
timeout=5,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"Roundcube logout failed: {exc}")
|
||||
self._rc_session = None
|
||||
self._rc_logged_in = False
|
||||
|
||||
@@ -176,7 +176,7 @@ class EmailVerifier:
|
||||
logger.debug(f"Roundcube: {password_name}登录成功 ({self.username})")
|
||||
return True, False
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
logger.warning(f"Roundcube: 登录异常: {e}")
|
||||
return False, False
|
||||
|
||||
@@ -230,7 +230,7 @@ class EmailVerifier:
|
||||
|
||||
return messages
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
logger.warning(f"Roundcube: 获取邮件列表失败: {e}")
|
||||
return []
|
||||
|
||||
@@ -252,7 +252,7 @@ class EmailVerifier:
|
||||
)
|
||||
return resp.text
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
logger.warning(f"Roundcube: 读取邮件 {uid} 失败: {e}")
|
||||
return None
|
||||
|
||||
@@ -270,7 +270,7 @@ class EmailVerifier:
|
||||
- "2026-06-20" → 直接解析
|
||||
- "06-20" → 今年的该日期
|
||||
"""
|
||||
now = datetime.now()
|
||||
now = datetime.now(UTC)
|
||||
|
||||
# 今天
|
||||
if date_str.startswith("今天"):
|
||||
@@ -322,32 +322,36 @@ class EmailVerifier:
|
||||
|
||||
# 日期格式 2026-06-20
|
||||
try:
|
||||
return datetime.strptime(date_str.strip(), "%Y-%m-%d")
|
||||
return datetime.strptime(date_str.strip(), "%Y-%m-%d").replace(tzinfo=UTC)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# 日期格式 2026-06-20 14:30
|
||||
try:
|
||||
return datetime.strptime(date_str.strip(), "%Y-%m-%d %H:%M")
|
||||
return datetime.strptime(date_str.strip(), "%Y-%m-%d %H:%M").replace(
|
||||
tzinfo=UTC
|
||||
)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# 日期格式 2026-06-20 14:30:00
|
||||
try:
|
||||
return datetime.strptime(date_str.strip(), "%Y-%m-%d %H:%M:%S")
|
||||
return datetime.strptime(date_str.strip(), "%Y-%m-%d %H:%M:%S").replace(
|
||||
tzinfo=UTC
|
||||
)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# 日期格式 06-20
|
||||
try:
|
||||
dt = datetime.strptime(date_str.strip(), "%m-%d")
|
||||
dt = datetime.strptime(date_str.strip(), "%m-%d").replace(tzinfo=UTC)
|
||||
return dt.replace(year=now.year)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# 日期格式 06-20 14:30
|
||||
try:
|
||||
dt = datetime.strptime(date_str.strip(), "%m-%d %H:%M")
|
||||
dt = datetime.strptime(date_str.strip(), "%m-%d %H:%M").replace(tzinfo=UTC)
|
||||
return dt.replace(year=now.year)
|
||||
except ValueError:
|
||||
pass
|
||||
@@ -395,7 +399,7 @@ class EmailVerifier:
|
||||
return code
|
||||
except EmailLoginError:
|
||||
raise
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
last_error = str(e)
|
||||
logger.warning(f"Roundcube 读邮件异常: {e}")
|
||||
|
||||
|
||||
+4
-4
@@ -398,7 +398,7 @@ class DouyuLogin:
|
||||
return LoginResult(
|
||||
success=False, message=str(e), code="email_login_failed"
|
||||
)
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
elapsed = time.monotonic() - start_time
|
||||
if self.max_login_retries > 0:
|
||||
logger.error(
|
||||
@@ -475,7 +475,7 @@ class DouyuLogin:
|
||||
return LoginResult(
|
||||
success=False, message=str(e), code="email_login_failed"
|
||||
)
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
elapsed = time.monotonic() - start_time
|
||||
if self.max_login_retries > 0:
|
||||
logger.error(
|
||||
@@ -901,7 +901,7 @@ class DouyuLogin:
|
||||
|
||||
if response2.status_code == 200:
|
||||
logger.info("WebLogin成功")
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
logger.warning(f"WebLogin请求失败(不影响登录): {e}")
|
||||
|
||||
# 登录成功后补齐 Web 侧 Cookie。补 CK 失败只影响完整度,不回滚已成功的登录态。
|
||||
@@ -915,7 +915,7 @@ class DouyuLogin:
|
||||
).enrich_with_retry()
|
||||
except InterruptedError:
|
||||
raise
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
self._cookie_enrich_error = self._truncate_error(str(e), 160)
|
||||
logger.warning(
|
||||
f"补CK最终失败,本次仍按登录成功保存基础CK: {self._cookie_enrich_error}"
|
||||
|
||||
@@ -137,7 +137,7 @@ class BaseWhitelistAdapter(ABC):
|
||||
|
||||
return False, f"白名单添加失败,API响应: {resp}"
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
msg = f"白名单同步失败: {e}"
|
||||
logger.error(msg)
|
||||
return False, msg
|
||||
@@ -165,7 +165,8 @@ def _get_local_exit_ip() -> str | None:
|
||||
match = re.search(r"(\d{1,3}(?:\.\d{1,3}){3})", text)
|
||||
if match:
|
||||
return match.group(1)
|
||||
except Exception:
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"本机出口 IP 查询失败: {url}: {exc}")
|
||||
continue
|
||||
return None
|
||||
|
||||
@@ -197,6 +198,7 @@ def get_exit_ip_via_proxy(proxy: str) -> str | None:
|
||||
match = re.search(r"(\d{1,3}(?:\.\d{1,3}){3})", text)
|
||||
if match:
|
||||
return match.group(1)
|
||||
except Exception:
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"代理出口 IP 查询失败: {url}: {exc}")
|
||||
continue
|
||||
return None
|
||||
|
||||
@@ -75,7 +75,7 @@ class XiequAdapter(BaseWhitelistAdapter):
|
||||
if r.get("IP") or r.get("ip")
|
||||
]
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
logger.error(f"获取白名单失败: {e}")
|
||||
return []
|
||||
|
||||
@@ -120,7 +120,7 @@ class XiequAdapter(BaseWhitelistAdapter):
|
||||
logger.warning(f"白名单添加结果: {text}")
|
||||
return False, text
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
logger.error(f"添加白名单失败: {e}")
|
||||
return False, str(e)
|
||||
|
||||
@@ -141,7 +141,7 @@ class XiequAdapter(BaseWhitelistAdapter):
|
||||
logger.warning(f"白名单删除结果: {text}")
|
||||
return False, text
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
logger.error(f"删除白名单失败: {e}")
|
||||
return False, str(e)
|
||||
|
||||
@@ -160,7 +160,7 @@ class XiequAdapter(BaseWhitelistAdapter):
|
||||
logger.info(msg)
|
||||
return True, msg
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
msg = f"连接失败: {e}"
|
||||
logger.error(msg)
|
||||
return False, msg
|
||||
|
||||
@@ -115,7 +115,7 @@ class XkdailiAdapter(BaseWhitelistAdapter):
|
||||
logger.warning(f"星空白名单添加失败: {msg}")
|
||||
return ok, msg
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
logger.error(f"星空添加白名单失败: {e}")
|
||||
return False, str(e)
|
||||
|
||||
@@ -136,7 +136,7 @@ class XkdailiAdapter(BaseWhitelistAdapter):
|
||||
logger.warning(f"星空白名单删除失败: {msg}")
|
||||
return ok, msg
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
logger.error(f"星空删除白名单失败: {e}")
|
||||
return False, str(e)
|
||||
|
||||
@@ -159,5 +159,5 @@ class XkdailiAdapter(BaseWhitelistAdapter):
|
||||
# 其他错误(如无效IP),说明认证通过了
|
||||
return True, f"连接成功,API可访问: {msg}"
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return False, f"连接失败: {e}"
|
||||
|
||||
@@ -167,7 +167,7 @@ class ProxyResolver:
|
||||
f"代理预检 {attempt}/{max_attempts}: {last_error}: {text[:80]}",
|
||||
)
|
||||
|
||||
except Exception as exc:
|
||||
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
last_error = f"代理API请求失败: {exc}"
|
||||
self._log("warning", f"代理预检 {attempt}/{max_attempts}: {last_error}")
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (3, 5)) -> tuple[bool, str
|
||||
)
|
||||
response.raise_for_status()
|
||||
return True, "代理可用 → 斗鱼可达"
|
||||
except Exception as exc:
|
||||
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
err_msg = str(exc)
|
||||
if "Tunnel connection failed" in err_msg or "503" in err_msg:
|
||||
detail = "代理拒绝连接(白名单可能未生效)"
|
||||
@@ -75,7 +75,8 @@ def verify_proxies_concurrent(
|
||||
ok, _ = future.result()
|
||||
if ok:
|
||||
available.append(future_map[future])
|
||||
except Exception:
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"代理并发验证失败: {future_map[future]}: {exc}")
|
||||
continue
|
||||
if available:
|
||||
logger.success(
|
||||
@@ -99,7 +100,8 @@ def verify_proxies_concurrent(
|
||||
if item != future:
|
||||
item.cancel()
|
||||
return proxy_url, msg
|
||||
except Exception:
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"代理并发验证失败: {proxy_url}: {exc}")
|
||||
continue
|
||||
|
||||
return None, f"共 {len(proxy_urls)} 个代理均不可用"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
"""代理模块使用的白名单适配器。"""
|
||||
|
||||
|
||||
from .proxy_platforms import create_adapter
|
||||
from .proxy_platforms.base import BaseWhitelistAdapter, _get_local_exit_ip
|
||||
|
||||
|
||||
Reference in New Issue
Block a user