refactor: 收口跨平台重复业务实现
This commit is contained in:
@@ -27,6 +27,7 @@ from ..models import (
|
||||
ProxyConfig as ProxyConfigModel,
|
||||
)
|
||||
from .huya_service import upsert_huya_cookie
|
||||
from .proxy_service import build_proxy_runtime
|
||||
|
||||
# 运行中状态:服务中断后视为未完成,可继续
|
||||
RUNNING_ITEM_STATUSES = frozenset({"sending", "waiting", "changing", "logging"})
|
||||
@@ -423,42 +424,16 @@ class HuyaRegisterRunner:
|
||||
self.item_indices = item_indices # None 表示跑全部
|
||||
self._lock = threading.Lock()
|
||||
self._stop = threading.Event()
|
||||
self._proxy_runtime = build_proxy_runtime(
|
||||
proxy_config,
|
||||
enabled=self.batch.use_proxy,
|
||||
stop_event=self._stop,
|
||||
)
|
||||
self._shared_proxy_fetcher = self._create_proxy_fetcher()
|
||||
|
||||
def _create_proxy_fetcher(self) -> ProxyFetcher | None:
|
||||
"""按需创建 API 代理获取器。"""
|
||||
if (
|
||||
not self.batch.use_proxy
|
||||
or not self.proxy_config
|
||||
or not self.proxy_config.enabled
|
||||
):
|
||||
return None
|
||||
if not self.proxy_config.api_url:
|
||||
return None
|
||||
|
||||
wl_platform = "xiequ"
|
||||
wl_credentials = None
|
||||
if self.proxy_config.whitelist_enabled:
|
||||
wl_platform = (
|
||||
getattr(self.proxy_config, "whitelist_platform", None) or "xiequ"
|
||||
)
|
||||
wl_credentials = getattr(self.proxy_config, "whitelist_credentials", None)
|
||||
if (
|
||||
not wl_credentials
|
||||
and self.proxy_config.whitelist_uid
|
||||
and self.proxy_config.whitelist_ukey
|
||||
):
|
||||
wl_credentials = {
|
||||
"uid": self.proxy_config.whitelist_uid,
|
||||
"ukey": self.proxy_config.whitelist_ukey,
|
||||
}
|
||||
|
||||
return ProxyFetcher(
|
||||
api_url=self.proxy_config.api_url,
|
||||
whitelist_platform=wl_platform,
|
||||
whitelist_credentials=wl_credentials,
|
||||
stop_event=self._stop,
|
||||
)
|
||||
return self._proxy_runtime.api_fetcher
|
||||
|
||||
def stop(self):
|
||||
self._stop.set()
|
||||
@@ -613,9 +588,8 @@ class HuyaRegisterRunner:
|
||||
if not self.proxy_config or not self.proxy_config.enabled:
|
||||
return None, "已开启代理,但代理配置未启用"
|
||||
|
||||
if self.proxy_config.http or self.proxy_config.https:
|
||||
proxy_url = self.proxy_config.http or self.proxy_config.https
|
||||
return {"http": proxy_url, "https": proxy_url}, ""
|
||||
if self._proxy_runtime.static_proxies:
|
||||
return self._proxy_runtime.static_proxies, ""
|
||||
|
||||
if self._shared_proxy_fetcher:
|
||||
proxy_url = self._shared_proxy_fetcher.fetch_new_proxy(max_attempts=3)
|
||||
|
||||
Reference in New Issue
Block a user