nex_buddy/server/app/services/weather.py

329 lines
13 KiB
Python
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""和风天气聚合:服务端持有 Key/Host,设备只拿精简结果。
配置只读平台设置(库),不读 .env;配置直读库不缓存,只有查询结果进 Redis。
缓存键 ``weather:{city}``,TTL 30 分钟(01-总体设计.md 3.4)。
"""
from __future__ import annotations
import asyncio
import os
import socket
import ssl
import sys
import time
from typing import Any
from urllib.parse import urlsplit
import httpx
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.config import settings
from app.models import PlatformSetting
from app.services import redis_store
class WeatherNotConfigured(RuntimeError):
"""缺 API Host:和风新账号必须用专属域名,否则一律 403 Invalid Host。"""
class WeatherUpstreamError(RuntimeError):
pass
def _upstream_msg(data: Any) -> str:
"""取上游响应里的消息字段,不做二次拼接。"""
if not isinstance(data, dict):
return ""
err = data.get("error")
if isinstance(err, dict):
for key in ("detail", "title", "msg", "message"):
value = str(err.get(key) or "").strip()
if value:
return value
for key in ("msg", "message", "code"):
value = str(data.get(key) or "").strip()
if value:
return value
return ""
async def _platform_value(db: AsyncSession, key: str) -> str | None:
"""平台配置直读库,不走 Redis、不回退 .env。
配置是运维级的低频写入,读一次主键查询成本可忽略;一旦进缓存,一条写错的
值(如文档示例 Host ``abc1234xyz.re.qweatherapi.com``)会压住库和 .env,
只能手动清 Redis 才能恢复——这类故障不值得为省一次查询去承担。
"""
row = await db.get(PlatformSetting, key)
return str(row.value) if row and row.value else None
async def _conf(db: AsyncSession) -> tuple[str, str]:
key = await _platform_value(db, "qweather.key")
host = await _platform_value(db, "qweather.host")
if not key or not host:
raise WeatherNotConfigured(
"和风天气未配置:请在管理端「平台设置」填写 API Key 和 API Host 后保存"
)
h = host.strip().rstrip("/")
if not h.startswith(("http://", "https://")):
h = "https://" + h
return key, h
PROXY_ENV_KEYS = ("http_proxy", "https_proxy", "all_proxy", "no_proxy")
def proxy_env() -> dict[str, str]:
"""httpx 默认 trust_env=True,会读这几个环境变量。
容器里常见的是继承了一个早已失效的代理地址(``ALL_PROXY=http://127.0.0.1:7890``),
表现和"服务器连不出去"一模一样——都是 TCP 建连超时。诊断时必须把它列出来,
否则会一直在服务器防火墙/安全组上找原因。
"""
return {k: v for k, v in os.environ.items() if k.lower() in PROXY_ENV_KEYS}
def effective_proxy() -> str:
"""httpx 实际会用的代理:显式配置 QWEATHER_PROXY 优先,其次环境变量。
不用 httpx 的内部函数(私有 API 换版本就没了),按大小写各读一遍即可。
"""
if settings.qweather_proxy.strip():
return settings.qweather_proxy.strip()
for k in ("https_proxy", "all_proxy", "http_proxy"):
v = os.environ.get(k) or os.environ.get(k.upper()) or ""
if v.strip():
return v.strip()
return ""
def explain_error(exc: BaseException, host: str) -> str:
"""把 httpx 的异常翻成"下一步该查什么",别只丢个 ConnectTimeout。"""
name = exc.__class__.__name__
if isinstance(exc, httpx.ConnectTimeout):
px = effective_proxy()
via = f",当前走代理 {px}" if px else ",未走代理(直连)"
return (
f"连不上和风 {host}:TCP 建连超时{via}。"
"这不是 Key/Host 配错(那会是 401/403),而是服务端到目标的网络不通:"
"查服务器/容器出网策略(安全组出站、iptables FORWARD、代理是否失效),"
"或用「测试连通」看卡在 DNS 还是 TCP。"
)
if isinstance(exc, httpx.ConnectError):
return f"连不上和风 {host}:{exc}(DNS 解析失败或端口被拒,先看域名能不能解析)"
if isinstance(exc, httpx.ReadTimeout):
return f"和风 {host} 响应超时:连接通了但迟迟不回数据(链路慢或被中间设备拦截)"
if isinstance(exc, httpx.HTTPError):
return f"和风 {host} 请求失败:{name} {exc}"
return f"和风 {host} 请求失败:{name}"
async def _get(http: httpx.AsyncClient, url: str, params: dict[str, Any], key: str = "") -> dict[str, Any]:
# 和风新版:API Key 走 X-QW-Api-Key 头;JWT 走 Authorization: Bearer。
# 早期文档里的 ?key= 查询参数对新建项目已不再生效(会返回 401),这里不再用。
headers: dict[str, str] = {}
if key:
if key.count(".") == 2 and len(key) > 80: # JWT:xxx.yyy.zzz
headers["Authorization"] = f"Bearer {key}"
else:
headers["X-QW-Api-Key"] = key
resp = await http.get(url, params=params, headers=headers)
if resp.status_code >= 400:
try:
data = resp.json()
except ValueError:
data = None
raise WeatherUpstreamError(_upstream_msg(data) or f"HTTP {resp.status_code}")
data = resp.json()
# 新版错误体是 {"error": {"status": 403, "title": ...}}
if isinstance(data, dict) and "error" in data:
raise WeatherUpstreamError(_upstream_msg(data) or "weather upstream error")
return data
async def _location_id(http: httpx.AsyncClient, host: str, key: str, city: str) -> tuple[str, str]:
"""城市名 → LocationID。返回 (id, 规范名)。"""
data = await _get(http, f"{host}/geo/v2/city/lookup", {"location": city, "number": 1, "lang": "zh"}, key)
items = data.get("location") or []
if not items:
raise WeatherUpstreamError(f"找不到城市:{city}")
return items[0]["id"], items[0]["name"]
def _slim(city: str, loc_id: str, now: dict[str, Any], hourly: list[dict[str, Any]]) -> dict[str, Any]:
"""设备端要的字段越少越好:360×360 屏只放得下这些。"""
return {
"ok": True,
"city": city,
"location_id": loc_id,
"temp": int(now.get("temp", 0)),
"feels": int(now.get("feelsLike", 0)),
"text": now.get("text", ""),
"code": now.get("icon", ""), # 和风图标码,固件据此画天气图标
"humidity": int(now.get("humidity", 0)),
"wind_dir": now.get("windDir", ""),
"wind_scale": now.get("windScale", ""),
"obs_time": now.get("obsTime", ""),
"hourly": [
{"time": h.get("fxTime", "")[11:16], "temp": int(h.get("temp", 0)),
"text": h.get("text", ""), "code": h.get("icon", ""), "pop": int(h.get("pop", 0) or 0)}
for h in hourly[:3]
],
"source": "qweather",
}
async def is_configured(db: AsyncSession) -> bool:
"""管理端数据库配置是否齐备,供 /healthz 使用。"""
try:
await _conf(db)
return True
except WeatherNotConfigured:
return False
async def now(db: AsyncSession, city: str) -> dict[str, Any]:
cached = await redis_store.get_weather(city)
if cached:
cached["cached"] = True
return cached
key, host = await _conf(db)
timeout = httpx.Timeout(8.0, connect=6.0)
try:
async with httpx.AsyncClient(timeout=timeout, headers={"Accept-Encoding": "gzip"}) as http:
loc_id, name = await _location_id(http, host, key, city)
current = await _get(http, f"{host}/v7/weather/now", {"location": loc_id, "lang": "zh"}, key)
try:
f24 = await _get(http, f"{host}/v7/weather/24h", {"location": loc_id, "lang": "zh"}, key)
hourly = f24.get("hourly") or []
except WeatherUpstreamError:
hourly = [] # 24h 可能是付费套餐,缺了不影响当前天气
except httpx.HTTPError as exc:
# DNS/连接/超时都归到上游不可用,让设备拿到 502 而不是 500。
# 上一次成功的结果(24h)还在就发它——设备上显示一份旧实况,
# 比显示"暂无数据"有用得多,`stale` 让客户端知道它不是实时的。
last = await redis_store.get_weather_last(city)
if last:
last["cached"] = True
last["stale"] = True
return last
raise WeatherUpstreamError(explain_error(exc, host)) from exc
payload = _slim(name or city, loc_id, current.get("now") or {}, hourly)
await redis_store.set_weather(city, payload)
return payload
# ---------------------------------------------------------------- 连通性自检
# 设备上只能看到"取不到天气",真要定位就得登服务器敲命令。这个函数把排查搬到
# 网页上:DNS → TCP → TLS → HTTP 各跑一遍并计时,断在哪一步一目了然。
# 只读、无副作用(不会写天气缓存),后台点多少次都行。
async def _tcp(ip: str, port: int, timeout: float = 6.0) -> float:
"""返回建连耗时(秒);失败抛 OSError。用线程池跑,别占事件循环。"""
def _do() -> float:
t0 = time.perf_counter()
with socket.create_connection((ip, port), timeout=timeout):
return time.perf_counter() - t0
return await asyncio.to_thread(_do)
async def probe(db: AsyncSession, city: str | None = None) -> dict[str, Any]:
city = (city or "").strip() or settings.default_city
out: dict[str, Any] = {
"ok": False,
"city": city,
"msg": "",
"steps": [],
"proxy": effective_proxy(),
"proxy_env": proxy_env(),
"python": sys.version.split()[0],
"httpx": httpx.__version__,
}
def step(name: str, ok: bool, ms: float | None, detail: str) -> None:
out["steps"].append({"name": name, "ok": ok, "ms": None if ms is None else round(ms * 1000), "detail": detail})
try:
key, base = await _conf(db)
except WeatherNotConfigured as exc:
step("config", False, None, str(exc))
out["msg"] = str(exc)
return out
parts = urlsplit(base)
host = parts.hostname or ""
port = parts.port or (443 if parts.scheme == "https" else 80)
kind = "JWT" if key.count(".") == 2 and len(key) > 80 else "API Key"
out["host"], out["port"], out["key_kind"] = host, port, kind
step("config", True, 0, f"{parts.scheme}://{host}:{port} {kind} {key[:6]}…{key[-4:]}")
# 1) DNS:解析失败一般是容器没 DNS / 域名写错,与出网无关
t0 = time.perf_counter()
try:
infos = await asyncio.to_thread(socket.getaddrinfo, host, port, type=socket.SOCK_STREAM)
ips = sorted({i[4][0] for i in infos})
out["resolved"] = ips
step("dns", True, time.perf_counter() - t0, " ".join(ips))
except OSError as exc:
step("dns", False, time.perf_counter() - t0, f"{exc.__class__.__name__}: {exc}")
out["msg"] = f"{exc.__class__.__name__}: {exc}"
return out
# 2) TCP:这一步通过说明网络通,后面出问题都跟 Key/证书有关
ip = ips[0]
try:
ms = await _tcp(ip, port)
step("tcp", True, ms, f"{ip}:{port} 建连 {ms * 1000:.0f}ms")
except OSError as exc:
step("tcp", False, None, f"{ip}:{port} {exc.__class__.__name__}: {exc}")
out["msg"] = f"{exc.__class__.__name__}: {exc}"
return out
# 3) TLS:能把 TCP 建起来但握手失败,多半是中间设备劫持或证书链缺失
if parts.scheme == "https":
t0 = time.perf_counter()
try:
reader, writer = await asyncio.wait_for(
asyncio.open_connection(ip, port, ssl=ssl.create_default_context(), server_hostname=host), 8
)
ssock = writer.get_extra_info("ssl_object")
detail = f"{ssock.version()} {ssock.cipher()[0] if ssock else ''}"
writer.close()
step("tls", True, time.perf_counter() - t0, detail.strip())
except Exception as exc: # noqa: BLE001 - 自检要兜住一切
step("tls", False, time.perf_counter() - t0, f"{exc.__class__.__name__}: {exc}")
out["msg"] = f"{exc.__class__.__name__}: {exc}"
return out
# 4) 真正打一次和风接口:401/403 才是 Key/Host 的问题
t0 = time.perf_counter()
try:
async with httpx.AsyncClient(timeout=httpx.Timeout(8.0, connect=6.0)) as http:
data = await _get(http, f"{base}/geo/v2/city/lookup",
{"location": city, "number": 1, "lang": "zh"}, key)
except (WeatherUpstreamError, httpx.HTTPError) as exc:
msg = str(exc) if isinstance(exc, WeatherUpstreamError) else explain_error(exc, base)
step("http", False, time.perf_counter() - t0, msg)
out["msg"] = msg
return out
items = data.get("location") or []
step("http", True, time.perf_counter() - t0,
f"HTTP 200,{city} → {items[0].get('name') if items else '(无结果)'} / {items[0].get('id') if items else '-'}")
if not items:
out["msg"] = _upstream_msg(data)
return out
out["ok"] = True
out["msg"] = _upstream_msg(data)
return out