from fastapi import APIRouter, Depends, Query from common.enums import AirGrade, ErrorType, RainType, Sky from common.models.errors import error_info from common.utils.kma_time import KST from router.v1.weather.protocol import ( AirData, CommuteData, Rain2h, RainHourData, Res_WeatherNow, Res_WeatherToday, Tomorrow, WarningData, WeatherSlot, ) from services.now_service import NowService from services.weather_service import Slot, WeatherService router = APIRouter(prefix="/v1/weather", tags=["weather"]) def _to_slot(s: Slot) -> WeatherSlot: data = WeatherSlot(at=s.at, label=f"{s.at.astimezone(KST).hour}시", icon=s.icon, tone=s.tone, note=s.note) if s.row is not None: data.temp_c, data.feels_c = float(s.row.temp_c), float(s.row.feels_c) data.sky, data.rain_type, data.rain_prob = Sky(s.row.sky), RainType(s.row.rain_type), s.row.rain_prob return data @router.get("/today", response_model=Res_WeatherToday, response_model_exclude_none=True, summary="오늘 3시간 칸 날씨") async def get_weather_today(region: str = Query(..., description="구 행정코드"), service: WeatherService = Depends()) -> Res_WeatherToday: err, today = await service.today(region) if err is not ErrorType.SUCCESS: return Res_WeatherToday(result=error_info(err)) return Res_WeatherToday( region_code=today.region.code, region_name=today.region.name, day=today.day, issued_at=today.issued_at.astimezone(KST) if today.issued_at else None, headline=today.headline, slots=[_to_slot(s) for s in today.slots], ) @router.get("/now", response_model=Res_WeatherNow, response_model_exclude_none=True, summary="지금 비 올까: 2시간 비, 내일 출퇴근, 미세먼지, 특보") async def get_weather_now(region: str = Query(..., description="구 행정코드"), service: NowService = Depends()) -> Res_WeatherNow: err, now = await service.now(region) if err is not ErrorType.SUCCESS: return Res_WeatherNow(result=error_info(err)) res = Res_WeatherNow( region_code=now.region.code, rain_2h=Rain2h( will_rain=now.will_rain_2h, text=now.rain_2h_text, hours=[RainHourData(at=h.at.astimezone(KST), rain_type=h.rain_type, rain_mm=h.rain_mm) for h in now.rain_hours], ), tomorrow=Tomorrow( text=now.tomorrow_text, rain_from=now.tomorrow_rain_from.astimezone(KST) if now.tomorrow_rain_from else None, commute=[CommuteData(label=c.label, at=c.at.astimezone(KST), icon=c.icon, temp_c=c.temp_c, text=c.text) for c in now.commute], ), ) if now.air is not None: res.air = AirData( station_name=now.air.station_name, pm25=now.air.pm25, pm10=now.air.pm10, grade=AirGrade(now.air.grade) if now.air.grade else None, measured_at=now.air.measured_at.astimezone(KST), text=now.air_text, ) if now.warnings is not None: res.warnings = [WarningData(warning_type=w.warning_type, level=w.level, text=w.text) for w in now.warnings] return res