refactor(realestate): collect_listings MOLIT 전용화(네이버는 워커 ingest로 이전)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-10 00:36:53 +09:00
parent 54654af815
commit 04081bef6a
2 changed files with 17 additions and 21 deletions

View File

@@ -168,20 +168,14 @@ def _fetch_naver_all() -> tuple:
def collect_listings() -> dict:
naver_ok = True
new_count = total = 0
molit_diag = naver_diag = None
"""MOLIT 실거래(합법 baseline)만 수집. 네이버 호가는 naver-fetch 워커가
/api/internal/realestate/listings-ingest로 push (spec 2026-07-09-naver-listing-worker)."""
molit_diag = None
try:
_, molit_diag = _fetch_molit_all() # 실거래(합법)는 항상 우선 — 안전마진 baseline
_, molit_diag = _fetch_molit_all()
except Exception as e:
molit_diag = f"exc={type(e).__name__}:{str(e)[:80]}"
logger.error("MOLIT 수집 오류(안전마진 baseline): %s", e)
try:
new_count, total = _fetch_naver_all()
except requests.RequestException as e:
naver_ok = False
naver_diag = f"{type(e).__name__}:{str(e)[:80]}"
logger.warning("네이버 매물수집 skip(차단/오류) — 안전마진·실거래는 유지: %s", e)
diag = f"molit[{molit_diag}] naver[{naver_diag or 'ok'}]"
save_listing_collect_log(new_count, total, naver_ok, error=diag)
return {"new_count": new_count, "total_count": total, "naver_ok": naver_ok, "diag": diag}
diag = f"molit[{molit_diag}] naver[worker]"
save_listing_collect_log(0, 0, None, error=diag)
return {"new_count": 0, "total_count": 0, "naver_ok": None, "diag": diag}

View File

@@ -17,16 +17,18 @@ def test_parse_naver_article_fields():
assert d["article_no"] == "A123" and d["deal_type"] == "전세" and d["deposit"] == 29000
assert d["source"] == "naver"
def test_collect_naver_block_sets_naver_ok_false(monkeypatch):
"""네이버 403 → 매물수집 skip + naver_ok=False, 실거래는 계속."""
def test_collect_is_molit_only(monkeypatch):
"""collect_listings는 MOLIT만 수행(naver fetch 미호출). naver는 워커 담당."""
from app import listing_collector as lc
monkeypatch.setattr(lc, "_fetch_molit_all", lambda: (2, "calls=1 saved=2"))
def boom(*a, **k):
import requests
raise requests.RequestException("403")
monkeypatch.setattr(lc, "_fetch_naver_all", boom)
monkeypatch.setattr(lc, "_fetch_molit_all", lambda: (5, "calls=1 saved=5"))
called = {"naver": False}
def naver_spy():
called["naver"] = True
return (0, 0)
monkeypatch.setattr(lc, "_fetch_naver_all", naver_spy)
r = lc.collect_listings()
assert r["naver_ok"] is False # 안전마진(실거래)은 유지
assert called["naver"] is False # naver 미호출
assert "molit" in r["diag"]
def test_molit_call_returns_diag_on_http_error(monkeypatch):
"""MOLIT 401 등 HTTP 오류 → ([], diag) 반환, diag에 상태코드 포함(관측)."""