fix: 같은 축제가 다른 출처로 이미 있으면 넣지 않기 (이름 같고 기간 겹침)

This commit is contained in:
minaChoi committed 2026-10-08 13:29:20 +09:00
1 parent 0436971cf1
commit b889f69135
3 files changed
+45 -2

No files matched your search

+21 -1
View File
@@ -193,6 +193,26 @@ def _name_key(name: str) -> str:
return _NAME_NOISE.sub("", name).lower()
async def drop_known(rows: list[dict]) -> list[dict]:
"""다른 출처로 이미 들어 있는 행사(같은 시도, 이름이 같고 기간이 겹침)는 뺀다. 먼저 넣은 출처를 남긴다."""
if not rows:
return rows
since, until = min(r["start_date"] for r in rows), max(r["end_date"] for r in rows)
err, existing = await DB_SESSION_MNG.execute_lambda(lambda s: WeekCRUD().events(s, sorted({r["sido_code"] for r in rows}), since, until))
if err is not ErrorType.SUCCESS:
return rows
known: dict[tuple[str, str], list[Event]] = {}
for e in existing or []:
known.setdefault((e.sido_code, _name_key(e.name)), []).append(e)
def dup(r: dict) -> bool:
key = _name_key(r["name"])
return bool(key) and any(e.source_id != r["source_id"] and e.start_date <= r["end_date"] and r["start_date"] <= e.end_date
for e in known.get((r["sido_code"], key), []))
return [r for r in rows if not dup(r)]
async def _seoul(client: httpx.AsyncClient, sido: str, today: date, known: list[dict]) -> list[dict]:
"""서울문화포털 축제(동네·구청·공원 행사). TourAPI 에 이미 있는 축제는 뺀다. 실패해도 다른 출처는 그대로 넣는다."""
if sido != SEOUL:
@@ -217,7 +237,7 @@ async def collect_events(sido: str, today: date) -> tuple[ErrorType, dict[str, i
except (DataGoKrNotConfigured, DataGoKrRequestFailed) as e:
LOG.error(f"[week] events {sido}: {e}")
return ErrorType.EXTERNAL_API_ERROR, {}
rows = list({r["source_id"]: r for r in festivals + exhibitions + concerts}.values())
rows = await drop_known(list({r["source_id"]: r for r in festivals + exhibitions + concerts}.values()))
err = await DB_SESSION_MNG.execute_lambda_run([lambda s: WeekCRUD().upsert_events(s, rows)])
LOG.info(f"[week] events {sido} {len(rows)}: {err.name}")
return err, {"festival": len(festivals), "exhibition": len(exhibitions), "concert": len(concerts)}