feat: 워치 스킨 가운데를 날씨 따라 비구름·천둥 구름·눈구름으로 — 초단기예보 낙뢰(LGT)로 2시간 안 천둥을 워치에 알림
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a74e3b49d0
commit
c0e0363759
10 files changed
+236
-37
No files matched your search
Vendored
+2
-2
@@ -79,13 +79,13 @@ def day_start_base(day: date) -> datetime:
|
||||
|
||||
|
||||
def parse_ultra(items: list[dict]) -> list[dict]:
|
||||
"""초단기예보 6시간: 시각별 비 형태·1시간 강수량·기온."""
|
||||
"""초단기예보 6시간: 시각별 비 형태·1시간 강수량·기온·낙뢰(LGT, 0 이면 없음)."""
|
||||
by_time: dict[datetime, dict[str, str]] = {}
|
||||
for it in items:
|
||||
at = datetime.strptime(it["fcstDate"] + it["fcstTime"], "%Y%m%d%H%M").replace(tzinfo=KST)
|
||||
by_time.setdefault(at, {})[it["category"]] = it["fcstValue"]
|
||||
return [
|
||||
{"at": at, "rain_type": _PTY[v["PTY"]], "rain_mm": parse_amount(v.get("RN1", "")), "temp_c": float(v["T1H"])}
|
||||
{"at": at, "rain_type": _PTY[v["PTY"]], "rain_mm": parse_amount(v.get("RN1", "")), "temp_c": float(v["T1H"]), "lightning": parse_amount(v.get("LGT", "")) > 0}
|
||||
for at, v in sorted(by_time.items())
|
||||
if "PTY" in v and "T1H" in v
|
||||
]
|
||||
|
||||
@@ -33,6 +33,7 @@ class RainHour:
|
||||
at: datetime
|
||||
rain_type: RainType
|
||||
rain_mm: float
|
||||
lightning: bool = False
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -56,6 +57,7 @@ class NowWeather:
|
||||
air: Optional[AirQuality]
|
||||
air_text: Optional[str]
|
||||
warnings: Optional[list[WarningItem]] = field(default=None)
|
||||
thunder_2h: bool = False
|
||||
|
||||
|
||||
def _weather_word(rain: RainType, sky: Sky, rain_mm: float) -> tuple[str, str]:
|
||||
@@ -139,6 +141,7 @@ class NowService:
|
||||
return ErrorType.SUCCESS, NowWeather(
|
||||
region=region, will_rain_2h=will_rain, rain_2h_text=rain_text, rain_hours=hours,
|
||||
tomorrow_text=tomorrow_text, tomorrow_rain_from=rain_from, commute=commute, air=air, air_text=air_text, warnings=warnings,
|
||||
thunder_2h=any(h.lightning for h in hours),
|
||||
)
|
||||
|
||||
async def _ultra(self, region: Region, now: datetime) -> Optional[list[RainHour]]:
|
||||
@@ -155,7 +158,10 @@ async def _cached_ultra(key: str, base: datetime) -> Optional[list[RainHour]]:
|
||||
data = orjson.loads(cached)
|
||||
if data["base"] != base.isoformat():
|
||||
return None
|
||||
return [RainHour(at=datetime.fromisoformat(h["at"]), rain_type=RainType(h["rain_type"]), rain_mm=h["rain_mm"]) for h in data["hours"]]
|
||||
return [
|
||||
RainHour(at=datetime.fromisoformat(h["at"]), rain_type=RainType(h["rain_type"]), rain_mm=h["rain_mm"], lightning=h.get("lightning", False))
|
||||
for h in data["hours"]
|
||||
]
|
||||
|
||||
|
||||
async def ultra_hours(region: Region, now: datetime, wait: bool = False) -> Optional[list[RainHour]]:
|
||||
@@ -173,9 +179,9 @@ async def ultra_hours(region: Region, now: datetime, wait: bool = False) -> Opti
|
||||
continue
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
parsed = parse_ultra(await fetch_ultra(client, region.nx, region.ny, base))
|
||||
hours = [RainHour(at=p["at"], rain_type=p["rain_type"], rain_mm=p["rain_mm"]) for p in parsed]
|
||||
hours = [RainHour(at=p["at"], rain_type=p["rain_type"], rain_mm=p["rain_mm"], lightning=p["lightning"]) for p in parsed]
|
||||
ttl = int((base + timedelta(hours=1, minutes=15) - now).total_seconds())
|
||||
payload = {"base": base.isoformat(), "hours": [{"at": h.at.isoformat(), "rain_type": h.rain_type.value, "rain_mm": h.rain_mm} for h in hours]}
|
||||
payload = {"base": base.isoformat(), "hours": [{"at": h.at.isoformat(), "rain_type": h.rain_type.value, "rain_mm": h.rain_mm, "lightning": h.lightning} for h in hours]}
|
||||
await REDIS.conn.set(key, orjson.dumps(payload), ex=max(ttl, 60))
|
||||
return hours
|
||||
return None
|
||||
|
||||
Reference in new issue
Block a user