import requests from typing import Dict, Any from .db import get_draw, upsert_draw def _normalize_item(item: dict) -> dict: # smok95 all.json / latest.json 구조 # - draw_no: int # - numbers: [n1..n6] # - bonus_no: int # - date: "YYYY-MM-DD ..." numbers = item["numbers"] return { "drw_no": int(item["draw_no"]), "drw_date": (item.get("date") or "")[:10], "n1": int(numbers[0]), "n2": int(numbers[1]), "n3": int(numbers[2]), "n4": int(numbers[3]), "n5": int(numbers[4]), "n6": int(numbers[5]), "bonus": int(item["bonus_no"]), } def sync_all_from_json(all_url: str) -> Dict[str, Any]: r = requests.get(all_url, timeout=60) r.raise_for_status() data = r.json() # list[dict] inserted = 0 skipped = 0 for item in data: row = _normalize_item(item) if get_draw(row["drw_no"]): skipped += 1 continue upsert_draw(row) inserted += 1 return {"mode": "all_json", "url": all_url, "inserted": inserted, "skipped": skipped, "total": len(data)} def sync_latest(latest_url: str) -> Dict[str, Any]: r = requests.get(latest_url, timeout=30) r.raise_for_status() item = r.json() row = _normalize_item(item) before = get_draw(row["drw_no"]) upsert_draw(row) return {"mode": "latest_json", "url": latest_url, "was_new": (before is None), "drawNo": row["drw_no"]}