feat: gate media downloads by group and retry douyu checks
This commit is contained in:
@@ -50,6 +50,8 @@ class DouyuDanmuRecorder:
|
||||
self._latest_vip_count: Optional[int] = None
|
||||
self._latest_diamond_count: Optional[int] = None
|
||||
self._last_stats_signature: Tuple[Optional[int], Optional[int]] = (None, None)
|
||||
self._connect_retry_count = 3
|
||||
self._connect_retry_delay_seconds = 1
|
||||
|
||||
def _encode(self, msg: str) -> bytes:
|
||||
content = msg.encode("utf-8") + b"\x00"
|
||||
@@ -223,24 +225,51 @@ class DouyuDanmuRecorder:
|
||||
for url in ws_urls:
|
||||
if self._stop_event.is_set():
|
||||
break
|
||||
try:
|
||||
self._ws = websocket.WebSocketApp(
|
||||
url,
|
||||
on_open=self._on_open,
|
||||
on_message=self._on_message,
|
||||
on_error=self._on_error,
|
||||
on_close=self._on_close,
|
||||
header=headers,
|
||||
)
|
||||
self._ws.run_forever(sslopt=sslopt, ping_interval=30, ping_timeout=10)
|
||||
except Exception as e:
|
||||
logger.error(f"斗鱼弹幕连接失败({self.room_id}): {e}")
|
||||
continue
|
||||
finally:
|
||||
self._ws = None
|
||||
for attempt in range(1, self._connect_retry_count + 1):
|
||||
if self._stop_event.is_set():
|
||||
break
|
||||
reconnect_needed = False
|
||||
try:
|
||||
self._ws = websocket.WebSocketApp(
|
||||
url,
|
||||
on_open=self._on_open,
|
||||
on_message=self._on_message,
|
||||
on_error=self._on_error,
|
||||
on_close=self._on_close,
|
||||
header=headers,
|
||||
)
|
||||
self._ws.run_forever(sslopt=sslopt, ping_interval=30, ping_timeout=10)
|
||||
if self._stop_event.is_set():
|
||||
break
|
||||
reconnect_needed = True
|
||||
except Exception as e:
|
||||
if attempt < self._connect_retry_count:
|
||||
logger.warning(
|
||||
f"斗鱼弹幕连接失败({self.room_id}),第{attempt}/{self._connect_retry_count}次重试: "
|
||||
f"url={url} err={e}"
|
||||
)
|
||||
time.sleep(self._connect_retry_delay_seconds)
|
||||
continue
|
||||
logger.error(
|
||||
f"斗鱼弹幕连接失败({self.room_id}),已重试{self._connect_retry_count}次: "
|
||||
f"url={url} err={e}"
|
||||
)
|
||||
finally:
|
||||
self._ws = None
|
||||
if reconnect_needed and attempt < self._connect_retry_count:
|
||||
logger.warning(
|
||||
f"斗鱼弹幕连接中断({self.room_id}),第{attempt}/{self._connect_retry_count}次重试: url={url}"
|
||||
)
|
||||
time.sleep(self._connect_retry_delay_seconds)
|
||||
continue
|
||||
if reconnect_needed and attempt >= self._connect_retry_count:
|
||||
logger.error(
|
||||
f"斗鱼弹幕连接中断({self.room_id}),已重试{self._connect_retry_count}次: url={url}"
|
||||
)
|
||||
break
|
||||
if self._stop_event.is_set():
|
||||
break
|
||||
time.sleep(1)
|
||||
time.sleep(self._connect_retry_delay_seconds)
|
||||
finally:
|
||||
self._ws = None
|
||||
|
||||
@@ -490,6 +519,8 @@ class DouyuPlugin(MessagePluginInterface):
|
||||
self._daily_report_max_length = 1800
|
||||
self._daily_report_send_image = True
|
||||
self._audience_stats_sample_interval_seconds = 60
|
||||
self._status_check_retry_count = 3
|
||||
self._status_check_retry_delay_seconds = 1
|
||||
self._daily_report_llm_client: Optional[UnifiedLLMClient] = None
|
||||
self._danmu_recorders: Dict[str, DouyuDanmuRecorder] = {}
|
||||
async_job.every_minutes(self._check_interval)(self._scheduled_unified_check_job)
|
||||
@@ -502,6 +533,34 @@ class DouyuPlugin(MessagePluginInterface):
|
||||
return f"{type(exc).__name__}: {message}"
|
||||
return type(exc).__name__
|
||||
|
||||
async def _fetch_json_with_retries(self, session: aiohttp.ClientSession, url: str,
|
||||
headers: Dict[str, str], context: str,
|
||||
params: Optional[Dict[str, Any]] = None) -> Any:
|
||||
last_error: Optional[Exception] = None
|
||||
for attempt in range(1, self._status_check_retry_count + 1):
|
||||
try:
|
||||
async with session.get(
|
||||
url,
|
||||
headers=headers,
|
||||
params=params,
|
||||
timeout=aiohttp.ClientTimeout(total=10)
|
||||
) as resp:
|
||||
resp.raise_for_status()
|
||||
return await resp.json(content_type=None)
|
||||
except Exception as e:
|
||||
last_error = e
|
||||
if attempt < self._status_check_retry_count:
|
||||
logger.warning(
|
||||
f"{context}失败,第{attempt}/{self._status_check_retry_count}次重试: "
|
||||
f"{self._format_exception(e)}"
|
||||
)
|
||||
await asyncio.sleep(self._status_check_retry_delay_seconds)
|
||||
continue
|
||||
raise
|
||||
if last_error:
|
||||
raise last_error
|
||||
raise RuntimeError(f"{context}失败,未获取到有效响应")
|
||||
|
||||
async def _scheduled_unified_check_job(self):
|
||||
"""统一检查直播和鱼吧动态"""
|
||||
await self._scheduled_check_job()
|
||||
@@ -696,9 +755,12 @@ class DouyuPlugin(MessagePluginInterface):
|
||||
"User-Agent": self._user_agent,
|
||||
"Referer": f"https://www.douyu.com/{room_id}"
|
||||
}
|
||||
async with session.get(url, headers=headers, timeout=aiohttp.ClientTimeout(total=10)) as resp:
|
||||
resp.raise_for_status()
|
||||
data = await resp.json(content_type=None)
|
||||
data = await self._fetch_json_with_retries(
|
||||
session,
|
||||
url,
|
||||
headers,
|
||||
context=f"斗鱼在线检查(room_id={room_id})"
|
||||
)
|
||||
room_info = data.get("room", {}) if isinstance(data, dict) else {}
|
||||
show_status = room_info.get("show_status")
|
||||
nickname = room_info.get("nickname", "")
|
||||
@@ -825,10 +887,13 @@ class DouyuPlugin(MessagePluginInterface):
|
||||
"User-Agent": self._user_agent,
|
||||
"Referer": f"https://yuba.douyu.com/member/{hash_id}/main/news",
|
||||
}
|
||||
async with session.get(self._yuba_api, headers=headers, params=params,
|
||||
timeout=aiohttp.ClientTimeout(total=10)) as resp:
|
||||
resp.raise_for_status()
|
||||
data = await resp.json(content_type=None)
|
||||
data = await self._fetch_json_with_retries(
|
||||
session,
|
||||
self._yuba_api,
|
||||
headers,
|
||||
context=f"斗鱼鱼吧检查(hash_id={hash_id})",
|
||||
params=params
|
||||
)
|
||||
|
||||
if data.get("error") != 0:
|
||||
logger.error(f"斗鱼鱼吧 API 错误 ({hash_id}): {data.get('msg')}")
|
||||
|
||||
Reference in New Issue
Block a user