mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-24 04:19:48 +01:00
修掉天气新闻插件的三处鲁棒性缺陷
城市查询不再写死国内范围,境外城市也能查到。实时天气和七天预报要都拿到才走 JSON,缺一个就整体退回网页路径,不产出残缺报告。网页抓取加上跟随跳转,旧链接的 302 也能拿到内容。城市查不到时不再让用户去核对地名,多数情况是密钥失效。报告补上气压和降水量。新闻 URL 拼接抽成纯函数,配了 23 项单测。
This commit is contained in:
parent
34cba2f6a5
commit
63f3860f29
3 changed files with 372 additions and 19 deletions
|
|
@ -55,6 +55,27 @@ CHANNEL_MAP = {
|
|||
# 默认新闻来源字典,当配置中没有指定时使用
|
||||
DEFAULT_NEWS_SOURCES = "澎湃新闻;百度热搜;财联社"
|
||||
|
||||
DEFAULT_NEWS_API_URL = "https://newsnow.busiyi.world/api/s?id="
|
||||
|
||||
|
||||
def _build_news_api_url(source, configured_url=None):
|
||||
"""拼出新闻接口的请求地址和查询参数。
|
||||
|
||||
配置里带 {source} 占位符就替换占位符;否则把 id 设成新闻源 id,
|
||||
这样配置里已经写了 id 时也不会拼出脏串。
|
||||
"""
|
||||
api_url = str(configured_url).strip() if configured_url else DEFAULT_NEWS_API_URL
|
||||
if "{source}" in api_url:
|
||||
return api_url.replace("{source}", quote(source, safe="")), None
|
||||
parts = urlsplit(api_url)
|
||||
query = dict(parse_qsl(parts.query, keep_blank_values=True))
|
||||
query["id"] = source
|
||||
return (
|
||||
urlunsplit((parts.scheme, parts.netloc, parts.path, "", parts.fragment)),
|
||||
query,
|
||||
)
|
||||
|
||||
|
||||
def _get_newsnow_config(conn):
|
||||
# 从连接配置获取
|
||||
plugins = conn.config.get("plugins", {})
|
||||
|
|
@ -116,21 +137,9 @@ GET_NEWS_FROM_NEWSNOW_FUNCTION_DESC = {
|
|||
async def fetch_news_from_api(conn: "ConnectionHandler", source="thepaper"):
|
||||
"""从API获取新闻列表"""
|
||||
try:
|
||||
api_url = "https://newsnow.busiyi.world/api/s?id="
|
||||
news_config = conn.config.get("plugins", {}).get("get_news_from_newsnow", {})
|
||||
configured_url = news_config.get("url") if isinstance(news_config, dict) else None
|
||||
if configured_url:
|
||||
api_url = str(configured_url).strip()
|
||||
|
||||
if "{source}" in api_url:
|
||||
api_url = api_url.replace("{source}", quote(source, safe=""))
|
||||
request_params = None
|
||||
else:
|
||||
parts = urlsplit(api_url)
|
||||
query = dict(parse_qsl(parts.query, keep_blank_values=True))
|
||||
query["id"] = source
|
||||
request_params = query
|
||||
api_url = urlunsplit((parts.scheme, parts.netloc, parts.path, "", parts.fragment))
|
||||
api_url, request_params = _build_news_api_url(source, configured_url)
|
||||
|
||||
headers = {"User-Agent": "Mozilla/5.0"}
|
||||
async with httpx.AsyncClient(timeout=httpx.Timeout(10.0, connect=3.0)) as client:
|
||||
|
|
|
|||
|
|
@ -121,7 +121,6 @@ async def fetch_city_info(location, api_key, api_host):
|
|||
"key": api_key,
|
||||
"location": location,
|
||||
"lang": "zh",
|
||||
"range": "cn",
|
||||
"number": 1,
|
||||
},
|
||||
headers=HEADERS,
|
||||
|
|
@ -133,7 +132,10 @@ async def fetch_city_info(location, api_key, api_host):
|
|||
f"获取天气城市失败:HTTP {response.status_code} 返回了非 JSON 内容"
|
||||
)
|
||||
return None
|
||||
if response.status_code != 200 or data.get("code") not in (None, "200", 200):
|
||||
if not isinstance(data, dict):
|
||||
logger.bind(tag=TAG).error("获取天气城市失败:响应不是 JSON 对象")
|
||||
return None
|
||||
if response.status_code != 200 or data.get("code") != "200":
|
||||
logger.bind(tag=TAG).error(
|
||||
f"获取天气城市失败:HTTP {response.status_code},代码 {data.get('code')}"
|
||||
)
|
||||
|
|
@ -170,9 +172,9 @@ async def fetch_weather_data(city_info, api_key, api_host):
|
|||
except Exception as e:
|
||||
logger.bind(tag=TAG).warning(f"获取天气 JSON 接口失败,将尝试网页接口: {e}")
|
||||
return None
|
||||
if now is None and daily is None:
|
||||
if now is None or daily is None:
|
||||
return None
|
||||
return now or {}, daily or {}
|
||||
return now, daily
|
||||
|
||||
|
||||
def build_weather_report(city_info, now_data, daily_data):
|
||||
|
|
@ -188,6 +190,8 @@ def build_weather_report(city_info, now_data, daily_data):
|
|||
"风向": now.get("windDir"),
|
||||
"风力": now.get("windScale"),
|
||||
"能见度": f"{now['vis']}公里" if now.get("vis") is not None else None,
|
||||
"气压": f"{now['pressure']}百帕" if now.get("pressure") is not None else None,
|
||||
"降水量": f"{now['precip']}毫米" if now.get("precip") is not None else None,
|
||||
}
|
||||
current_basic = {key: value for key, value in current_basic.items() if value}
|
||||
if current_basic:
|
||||
|
|
@ -206,7 +210,9 @@ def build_weather_report(city_info, now_data, daily_data):
|
|||
|
||||
|
||||
async def fetch_weather_page(url):
|
||||
async with httpx.AsyncClient(timeout=httpx.Timeout(10.0, connect=3.0)) as client:
|
||||
async with httpx.AsyncClient(
|
||||
timeout=httpx.Timeout(10.0, connect=3.0), follow_redirects=True
|
||||
) as client:
|
||||
response = await client.get(url, headers=HEADERS)
|
||||
return BeautifulSoup(response.text, "html.parser") if response.status_code == 200 else None
|
||||
|
||||
|
|
@ -282,7 +288,9 @@ async def get_weather(conn: "ConnectionHandler", location: str = None, lang: str
|
|||
city_info = await fetch_city_info(location, api_key, api_host)
|
||||
if not city_info:
|
||||
return ActionResponse(
|
||||
Action.REQLLM, f"未找到相关的城市: {location},请确认地点是否正确", None
|
||||
Action.REQLLM,
|
||||
f"暂时查不到 {location} 的天气(城市未找到或天气服务不可用),请稍后再试",
|
||||
None,
|
||||
)
|
||||
weather_data = await fetch_weather_data(city_info, api_key, api_host)
|
||||
if weather_data:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,336 @@
|
|||
"""Tests for the weather and newsnow plugin helpers.
|
||||
|
||||
The two plugins talk to external APIs, so the HTTP layer is replaced with a
|
||||
fake client and every request is recorded. Nothing here touches the network.
|
||||
"""
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from plugins_func.functions import get_news_from_newsnow as news_module
|
||||
from plugins_func.functions import get_weather as weather_module
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, status_code=200, payload=None, text="", json_error=None):
|
||||
self.status_code = status_code
|
||||
self.payload = payload
|
||||
self.text = text
|
||||
self.json_error = json_error
|
||||
|
||||
def json(self):
|
||||
if self.json_error is not None:
|
||||
raise self.json_error
|
||||
return self.payload
|
||||
|
||||
|
||||
class FakeAsyncClient:
|
||||
"""Stands in for httpx.AsyncClient and records what was requested."""
|
||||
|
||||
requests = []
|
||||
responder = None
|
||||
init_kwargs = {}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
FakeAsyncClient.init_kwargs = kwargs
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *exc_info):
|
||||
return False
|
||||
|
||||
async def get(self, url, params=None, headers=None):
|
||||
FakeAsyncClient.requests.append({"url": url, "params": params})
|
||||
return FakeAsyncClient.responder(url, params)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_http(monkeypatch):
|
||||
"""Replace httpx.AsyncClient with the recording fake."""
|
||||
FakeAsyncClient.requests = []
|
||||
FakeAsyncClient.responder = lambda url, params: FakeResponse(payload={"code": "200"})
|
||||
FakeAsyncClient.init_kwargs = {}
|
||||
monkeypatch.setattr(httpx, "AsyncClient", FakeAsyncClient)
|
||||
return FakeAsyncClient
|
||||
|
||||
|
||||
class FakeConn:
|
||||
def __init__(self, plugins=None):
|
||||
self.config = {"plugins": plugins or {}}
|
||||
self.client_ip = None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 城市查询
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_city_lookup_returns_none_on_error_code(fake_http):
|
||||
fake_http.responder = lambda url, params: FakeResponse(payload={"code": "402"})
|
||||
|
||||
result = await weather_module.fetch_city_info("杭州", "key", "host")
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
async def test_city_lookup_returns_none_on_non_json_body(fake_http):
|
||||
fake_http.responder = lambda url, params: FakeResponse(
|
||||
text="<html>oops</html>", json_error=ValueError("not json")
|
||||
)
|
||||
|
||||
result = await weather_module.fetch_city_info("杭州", "key", "host")
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
async def test_city_lookup_returns_none_on_non_dict_body(fake_http):
|
||||
fake_http.responder = lambda url, params: FakeResponse(payload=["not", "an", "object"])
|
||||
|
||||
result = await weather_module.fetch_city_info("杭州", "key", "host")
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
async def test_city_lookup_returns_none_on_empty_location(fake_http):
|
||||
fake_http.responder = lambda url, params: FakeResponse(
|
||||
payload={"code": "200", "location": []}
|
||||
)
|
||||
|
||||
result = await weather_module.fetch_city_info("杭州", "key", "host")
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
async def test_city_lookup_searches_all_cities(fake_http):
|
||||
"""不带 range 参数就是全球检索,写死 cn 会查不到境外城市。"""
|
||||
fake_http.responder = lambda url, params: FakeResponse(
|
||||
payload={"code": "200", "location": [{"id": "101210101", "name": "杭州"}]}
|
||||
)
|
||||
|
||||
result = await weather_module.fetch_city_info("杭州", "key", "host")
|
||||
|
||||
assert result == {"id": "101210101", "name": "杭州"}
|
||||
sent = fake_http.requests[0]["params"]
|
||||
assert "range" not in sent
|
||||
assert sent["location"] == "杭州"
|
||||
assert sent["number"] == 1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 天气数据
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _weather_responder(now_payload, daily_payload):
|
||||
def responder(url, params):
|
||||
if url.endswith("/now"):
|
||||
return now_payload if isinstance(now_payload, FakeResponse) else FakeResponse(
|
||||
payload=now_payload
|
||||
)
|
||||
return daily_payload if isinstance(daily_payload, FakeResponse) else FakeResponse(
|
||||
payload=daily_payload
|
||||
)
|
||||
|
||||
return responder
|
||||
|
||||
|
||||
async def test_weather_data_needs_both_now_and_daily(fake_http):
|
||||
fake_http.responder = _weather_responder(
|
||||
{"code": "200", "now": {"text": "晴"}},
|
||||
FakeResponse(status_code=401, payload={"code": "401"}),
|
||||
)
|
||||
|
||||
assert await weather_module.fetch_weather_data({"id": "1"}, "key", "host") is None
|
||||
|
||||
|
||||
async def test_weather_data_needs_daily_when_now_failed(fake_http):
|
||||
fake_http.responder = _weather_responder(
|
||||
FakeResponse(status_code=401, payload={"code": "401"}),
|
||||
{"code": "200", "daily": [{"fxDate": "2026-10-24"}]},
|
||||
)
|
||||
|
||||
assert await weather_module.fetch_weather_data({"id": "1"}, "key", "host") is None
|
||||
|
||||
|
||||
async def test_weather_data_returns_both_when_all_succeed(fake_http):
|
||||
fake_http.responder = _weather_responder(
|
||||
{"code": "200", "now": {"text": "晴"}},
|
||||
{"code": "200", "daily": [{"fxDate": "2026-10-24"}]},
|
||||
)
|
||||
|
||||
now, daily = await weather_module.fetch_weather_data({"id": "1"}, "key", "host")
|
||||
|
||||
assert now["now"]["text"] == "晴"
|
||||
assert daily["daily"][0]["fxDate"] == "2026-10-24"
|
||||
|
||||
|
||||
async def test_weather_page_follows_redirects(fake_http):
|
||||
"""旧形态 fxLink 会 302,不跟随重定向就拿不到内容。"""
|
||||
fake_http.responder = lambda url, params: FakeResponse(text="<html></html>")
|
||||
|
||||
await weather_module.fetch_weather_page("https://www.qweather.com/weather/101210101.html")
|
||||
|
||||
assert fake_http.init_kwargs.get("follow_redirects") is True
|
||||
|
||||
|
||||
def test_weather_report_keeps_pressure_and_precipitation():
|
||||
report = weather_module.build_weather_report(
|
||||
{"name": "杭州"},
|
||||
{"now": {"text": "多云", "temp": "24", "pressure": "1003", "precip": "0.0"}},
|
||||
{"daily": []},
|
||||
)
|
||||
|
||||
assert "气压: 1003百帕" in report
|
||||
assert "降水量: 0.0毫米" in report
|
||||
|
||||
|
||||
async def test_weather_report_keeps_pressure_and_precipitation_in_flow(monkeypatch):
|
||||
"""走完整条链路时报告里也要有气压和降水量。"""
|
||||
monkeypatch.setattr(
|
||||
weather_module,
|
||||
"fetch_city_info",
|
||||
_async_value({"id": "1", "name": "杭州"}),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
weather_module,
|
||||
"fetch_weather_data",
|
||||
_async_value(
|
||||
({"now": {"text": "多云", "pressure": "1003", "precip": "0.0"}}, {"daily": []})
|
||||
),
|
||||
)
|
||||
|
||||
response = await weather_module.get_weather(
|
||||
FakeConn({"get_weather": {"api_key": "k", "api_host": "h"}}), location="气压测试市"
|
||||
)
|
||||
|
||||
assert "气压: 1003百帕" in response.result
|
||||
assert "降水量: 0.0毫米" in response.result
|
||||
|
||||
|
||||
async def test_missing_fxlink_does_not_call_web_fallback(monkeypatch):
|
||||
"""城市信息里没有 fxLink 时不该去抓网页,直接报失败。"""
|
||||
called = []
|
||||
|
||||
async def fake_city_info(*args, **kwargs):
|
||||
return {"id": "1", "name": "杭州"}
|
||||
|
||||
async def fake_weather_data(*args, **kwargs):
|
||||
return None
|
||||
|
||||
async def fake_page(url):
|
||||
called.append(url)
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(weather_module, "fetch_city_info", fake_city_info)
|
||||
monkeypatch.setattr(weather_module, "fetch_weather_data", fake_weather_data)
|
||||
monkeypatch.setattr(weather_module, "fetch_weather_page", fake_page)
|
||||
|
||||
response = await weather_module.get_weather(
|
||||
FakeConn({"get_weather": {"api_key": "k", "api_host": "h"}}), location="无链接市"
|
||||
)
|
||||
|
||||
assert called == []
|
||||
assert response.response == "请求天气数据失败"
|
||||
|
||||
|
||||
async def test_city_not_found_message_does_not_blame_the_place_name(monkeypatch):
|
||||
"""key 失效和城市找不到都落到这句话,不能让用户去核对地名。"""
|
||||
|
||||
async def fake_city_info(*args, **kwargs):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(weather_module, "fetch_city_info", fake_city_info)
|
||||
|
||||
response = await weather_module.get_weather(
|
||||
FakeConn({"get_weather": {"api_key": "k", "api_host": "h"}}), location="随便什么市"
|
||||
)
|
||||
|
||||
assert "请确认地点是否正确" not in response.result
|
||||
assert "天气服务不可用" in response.result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 新闻接口
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"configured_url, expected_url, expected_params",
|
||||
[
|
||||
(None, "https://newsnow.busiyi.world/api/s", {"id": "thepaper"}),
|
||||
(
|
||||
"https://newsnow.busiyi.world/api/s?id=thepaper",
|
||||
"https://newsnow.busiyi.world/api/s",
|
||||
{"id": "thepaper"},
|
||||
),
|
||||
(
|
||||
"https://newsnow.busiyi.world/api/s?id={source}",
|
||||
"https://newsnow.busiyi.world/api/s?id=thepaper",
|
||||
None,
|
||||
),
|
||||
(
|
||||
"https://newsnow.busiyi.world/api/s",
|
||||
"https://newsnow.busiyi.world/api/s",
|
||||
{"id": "thepaper"},
|
||||
),
|
||||
(
|
||||
"https://newsnow.busiyi.world/api/s?id=",
|
||||
"https://newsnow.busiyi.world/api/s",
|
||||
{"id": "thepaper"},
|
||||
),
|
||||
(
|
||||
"https://newsnow.busiyi.world/api/s?id=thepaper&limit=5",
|
||||
"https://newsnow.busiyi.world/api/s",
|
||||
{"id": "thepaper", "limit": "5"},
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_news_api_url_forms(configured_url, expected_url, expected_params):
|
||||
url, params = news_module._build_news_api_url("thepaper", configured_url)
|
||||
|
||||
assert url == expected_url
|
||||
assert params == expected_params
|
||||
|
||||
|
||||
async def test_news_api_url_forms_are_actually_requested(fake_http):
|
||||
"""配置里写了 id 也不能拼出脏串。"""
|
||||
fake_http.responder = lambda url, params: FakeResponse(
|
||||
payload={"items": [{"id": "1", "title": "标题"}]}
|
||||
)
|
||||
conn = FakeConn({"get_news_from_newsnow": {"url": "https://newsnow.busiyi.world/api/s?id=thepaper"}})
|
||||
|
||||
await news_module.fetch_news_from_api(conn, "thepaper")
|
||||
|
||||
assert fake_http.requests[0]["url"] == "https://newsnow.busiyi.world/api/s"
|
||||
assert fake_http.requests[0]["params"] == {"id": "thepaper"}
|
||||
|
||||
|
||||
async def test_news_items_must_be_a_list(fake_http):
|
||||
fake_http.responder = lambda url, params: FakeResponse(payload={"items": "oops"})
|
||||
|
||||
assert await news_module.fetch_news_from_api(FakeConn(), "thepaper") == []
|
||||
|
||||
|
||||
async def test_news_items_without_title_are_filtered_out(fake_http):
|
||||
fake_http.responder = lambda url, params: FakeResponse(
|
||||
payload={"items": [{"id": "1"}, {"id": "2", "title": ""}]}
|
||||
)
|
||||
|
||||
assert await news_module.fetch_news_from_api(FakeConn(), "thepaper") == []
|
||||
|
||||
|
||||
async def test_news_items_keep_valid_entries(fake_http):
|
||||
fake_http.responder = lambda url, params: FakeResponse(
|
||||
payload={"items": [{"id": "1", "title": ""}, {"id": "2", "title": "有效标题"}]}
|
||||
)
|
||||
|
||||
items = await news_module.fetch_news_from_api(FakeConn(), "thepaper")
|
||||
|
||||
assert items == [{"id": "2", "title": "有效标题"}]
|
||||
|
||||
|
||||
def _async_value(value):
|
||||
async def _inner(*args, **kwargs):
|
||||
return value
|
||||
|
||||
return _inner
|
||||
Loading…
Reference in a new issue