29 lines
802 B
Python
29 lines
802 B
Python
"""网抑云 —— 业务逻辑(Service 层)。"""
|
|||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import re
|
||
|
|
|
||
|
|
import httpx
|
||
|
|
|
||
|
|
from .. import config as _cfg
|
||
|
|
|
||
|
|
|
||
|
|
async def fetch_hitokoto() -> str | None:
|
||
|
|
"""带超时与重试地拉取网抑云热评文本;多次失败后抛出最后一次异常。"""
|
||
|
|
last_err: Exception | None = None
|
||
|
|
for _ in range(3):
|
||
|
|
try:
|
||
|
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
||
|
|
response = await client.get(_cfg.API_URL)
|
||
|
|
response.raise_for_status()
|
||
|
|
match = re.search(r"<p>(.*?)</p>", response.text)
|
||
|
|
if match:
|
||
|
|
return match.group(1)
|
||
|
|
return None
|
||
|
|
except Exception as e: # noqa: BLE001
|
||
|
|
last_err = e
|
||
|
|
if last_err:
|
||
|
|
raise last_err
|
||
|
|
return None
|