feat: 날씨각 백엔드 API (날씨·노을·지수·주말·계절·피드·로그인·반응)
- FastAPI + PostgreSQL + Redis, web·worker 분리, compose 에 backend·worker·postgres·redis 추가 - 기상청 단기·초단기·중기·특보, 에어코리아, Open-Meteo, TourAPI, 특일정보 연동 (첫 조회 때 받고 캐시) - 매일 03:00 worker 가 전국 공통 피드 칸을 정하고 지난 데이터를 정리 - 시드: 시군구 256곳, 지수 23개, 노을 대표 지점·명소, 데이트 명소, 읽을거리 - pytest 66개 (테스트 전용 DB·Redis 사용) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9cdc0d8fea
commit
e134295ce5
135 files changed
+9123
No files matched your search
+56
@@ -0,0 +1,56 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
|
||||
from common.utils.kma_time import KST
|
||||
from services.external.data_go_kr import get_all_items
|
||||
|
||||
REALTIME_URL = "https://apis.data.go.kr/B552584/ArpltnInforInqireSvc/getCtprvnRltmMesureDnsty"
|
||||
STATION_URL = "https://apis.data.go.kr/B552584/MsrstnInfoInqireSvc/getMsrstnList"
|
||||
|
||||
|
||||
def _int(v) -> Optional[int]:
|
||||
try:
|
||||
return int(v)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def _measured_at(text: str) -> datetime:
|
||||
""""2026-10-02 08:00" (KST). 자정은 "24:00" 으로 온다."""
|
||||
day, hm = text.split()
|
||||
if hm == "24:00":
|
||||
return datetime.strptime(day, "%Y-%m-%d").replace(tzinfo=KST) + timedelta(days=1)
|
||||
return datetime.strptime(text, "%Y-%m-%d %H:%M").replace(tzinfo=KST)
|
||||
|
||||
|
||||
async def fetch_realtime(client: httpx.AsyncClient) -> list[dict]:
|
||||
return await get_all_items(client, REALTIME_URL, {"returnType": "json", "sidoName": "전국", "ver": "1.0"})
|
||||
|
||||
|
||||
async def fetch_stations(client: httpx.AsyncClient) -> list[dict]:
|
||||
return await get_all_items(client, STATION_URL, {"returnType": "json"})
|
||||
|
||||
|
||||
def parse_realtime(items: list[dict]) -> list[dict]:
|
||||
rows = []
|
||||
for it in items:
|
||||
if not it.get("stationName") or not it.get("dataTime"):
|
||||
continue
|
||||
grades = [g for g in (_int(it.get("pm25Grade")), _int(it.get("pm10Grade"))) if g]
|
||||
rows.append({
|
||||
"station_name": it["stationName"], "pm25": _int(it.get("pm25Value")), "pm10": _int(it.get("pm10Value")),
|
||||
"grade": max(grades) if grades else None, "measured_at": _measured_at(it["dataTime"]),
|
||||
})
|
||||
return rows
|
||||
|
||||
|
||||
def parse_stations(items: list[dict]) -> list[dict]:
|
||||
rows = []
|
||||
for it in items:
|
||||
try:
|
||||
rows.append({"station_name": it["stationName"], "lat": float(it["dmX"]), "lon": float(it["dmY"])})
|
||||
except (KeyError, TypeError, ValueError):
|
||||
continue
|
||||
return rows
|
||||
Reference in new issue
Block a user