[feat] solution/backend: 미니 블로그 글을 배정 날짜에 맞는 소재로 만든다

예전에는 소재 목록을 순서대로 뽑아 날짜에 차례로 붙여서, 9월 날짜에
'한겨울' 글이나 끝난 축제 글이 붙을 수 있었다. 이제 날짜를 먼저 정하고
그 날짜에 맞는 소재를 고른다.

- 축제: 시작 14일 전 ~ 종료일 사이만 / 계절: 게시일 절기 하나
  / 날씨: 그 달에 있을 법한 것만(눈 12~2월, 소나기 6~8월) / 주변: 늘 후보
- 프롬프트에 게시일을 넣고, 날씨를 단정하지 않게 문구를 고쳤다
- 계절·날씨·축제 주제 키에 연(월)을 넣어 해마다 다시 쓸 수 있게 했다
- 자동·구간·개별 세 경로가 _compose_for_dates 하나로 만든다

같이 고친 버그: materials 가 스냅샷에 없는 local.festivals·attractions 를
읽어 축제·주변 소재가 늘 비어 있었다. 원문 행(local.contents)을 읽는다.
This commit is contained in:
skkim committed 2026-09-23 09:41:23 +09:00
1 parent be3e43162a
commit 36dbe26c91
4 files changed
+240 -137

No files matched your search

+89 -27
View File
@@ -8,9 +8,9 @@
import hashlib
import re
import secrets
from datetime import datetime, timedelta, timezone
from datetime import date, datetime, timedelta, timezone
from common.enums import PlaceCategory, PostStatus, PostTopicKind
from common.enums import LocalContentType, PlaceCategory, PostStatus, PostTopicKind
from common.logger import LOG
# 본문 길이 — 회의 확정값(140~150자)에 여유를 둔다. 벗어나면 버린다.
@@ -66,12 +66,13 @@ def issue_token() -> tuple[str, str, object]:
# 숙소(LODGING) 기본 갈래 규칙 — 업종별 규칙이 없을 때의 폴백이기도 하다.
TOPIC_RULES: dict[int, str] = {
# ★ 게시일의 실제 날씨는 모른다(글은 며칠·몇 주 앞서 만든다) — "오늘은 비가 옵니다"라고 단정하게 두지 않는다.
PostTopicKind.WEATHER.value:
"오늘의 날씨와 그 날씨에 이 숙소에서 하기 좋은 일을 한 장면으로 적는다.",
"소재로 주어진 날씨인 날, 이 숙소에서 하기 좋은 일을 한 장면으로 적는다. 게시일의 날씨를 단정하지 않는다.",
PostTopicKind.FESTIVAL.value:
"주어진 축제 하나를 언급하고, 숙소에서 그곳까지 어떻게 가는지를 걸음 단위로 적는다.",
"주어진 축제 하나를 게시일 기준으로(곧 열리는지, 열리는 중인지) 언급하고, 숙소에서 그곳까지 어떻게 가는지를 걸음 단위로 적는다.",
PostTopicKind.SEASON.value:
"지금 절기에 이 지역과 숙소가 어떻게 달라지는지를 적는다.",
"게시일 무렵 절기에 이 지역과 숙소가 어떻게 달라지는지를 적는다.",
PostTopicKind.NEARBY.value:
"주어진 주변 장소 하나를 손님 시선에서 적는다. 영업시간과 가격은 쓰지 않는다.",
PostTopicKind.GUIDE.value:
@@ -102,17 +103,36 @@ _RULES = (
"- '최고' '유일' 같은 최상급을 쓰지 않는다.\n"
"- 손님에게 말하듯 존댓말로 적는다.\n"
"- 아래 '이미 쓴 주제'와 겹치는 소재를 고르지 않는다.\n"
"- 게시일과 맞지 않는 계절·날씨·행사 이야기를 쓰지 않는다.\n"
)
_WEEKDAYS = "월화수목금토일"
def season_term(on: date) -> str:
"""게시일 → 절기 이름(materials 의 계절 소재와 같은 말). 달로만 가른다."""
return {
3: "봄", 4: "봄", 5: "봄",
6: "초여름", 7: "한여름", 8: "한여름",
9: "초가을", 10: "늦가을", 11: "늦가을",
12: "초겨울", 1: "한겨울", 2: "한겨울",
}[on.month]
def _date_line(on: date) -> str:
return f"게시일: {on.year}년 {on.month}월 {on.day}일({_WEEKDAYS[on.weekday()]}) · {season_term(on)}\n"
def build_prompt(*, place_name: str, region: str, topic_kind: int, material: str, used_topics: list[str],
place_category: int = PlaceCategory.LODGING.value) -> str:
"""갈래 하나에 대한 프롬프트 한 벌. 프롬프트를 두 곳에 적지 않으려고 여기서만 만든다."""
place_category: int = PlaceCategory.LODGING.value, post_date: date | None = None) -> str:
"""갈래 하나에 대한 프롬프트 한 벌. 프롬프트를 두 곳에 적지 않으려고 여기서만 만든다.
post_date 가 있으면 게시일을 알려 준다 — 글이 그 날짜의 계절·행사와 맞게 쓰이도록(2026-09-23)."""
used = ", ".join(used_topics[:40]) or "없음"
noun = _business_noun(place_category)
rules = _topic_rules(place_category)
return (
f"{region}에 있는 {noun} '{place_name}'의 짧은 홍보 글을 쓴다.\n"
f"{_date_line(post_date) if post_date else ''}"
f"갈래: {rules.get(topic_kind, '')}\n"
f"소재: {material}\n"
f"이미 쓴 주제: {used}\n\n"
@@ -146,7 +166,7 @@ def filter_drafts(rows: list[dict]) -> tuple[list[dict], list[tuple[str, str]]]:
async def generate_one(*, place_name: str, region: str, topic_kind: int, material: str,
used_topics: list[str], place_category: int = PlaceCategory.LODGING.value,
client=None) -> tuple[str, str] | None:
post_date: date | None = None, client=None) -> tuple[str, str] | None:
"""(문구, 모델명) 한 쌍. LLM 이 없거나 실패하면 None — 생성 실패가 잡을 죽이지 않는다.
모델명은 생성 이력 화면이 "어느 모델썼는지" 보여주는 데 쓴다(2026-09-17, 사장님 지시).
@@ -166,7 +186,8 @@ async def generate_one(*, place_name: str, region: str, topic_kind: int, materia
return None
prompt = build_prompt(place_name=place_name, region=region, topic_kind=topic_kind,
material=material, used_topics=used_topics, place_category=place_category)
material=material, used_topics=used_topics, place_category=place_category,
post_date=post_date)
owns = client is None
if owns:
import httpx
@@ -183,27 +204,68 @@ async def generate_one(*, place_name: str, region: str, topic_kind: int, materia
await client.aclose()
def materials(snapshot: dict) -> list[tuple[int, str, str]]:
"""(갈래, topic_key, 소재). 소재가 없는 갈래는 아예 만들지 않는다 — 지어내지 않는다."""
local = snapshot.get("local") or {}
# 축제 글을 시작일 며칠 전부터 낼 수 있나. 끝난 축제는 내지 않는다.
FESTIVAL_LEAD_DAYS = 14
# 그 달에 말이 되는 날씨만 소재로 쓴다 — 여름에 "눈인 날" 글이 나가지 않게.
_SKIES = ("맑음", "흐림", "비", "안개")
_SKIES_BY_MONTH = {12: ("눈",), 1: ("눈",), 2: ("눈",), 6: ("소나기",), 7: ("소나기",), 8: ("소나기",)}
def _ymd(value) -> date | None:
digits = "".join(ch for ch in str(value or "") if ch.isdigit())
if len(digits) != 8:
return None
try:
return date(int(digits[:4]), int(digits[4:6]), int(digits[6:]))
except ValueError:
return None
def materials(snapshot: dict, on: date) -> list[tuple[int, str, str]]:
"""게시일 on 에 맞는 (갈래, topic_key, 소재). 앞에 있을수록 먼저 고른다 — 축제 → 계절 → 주변 → 날씨.
소재가 없는 갈래는 아예 만들지 않는다 — 지어내지 않는다.
★ 날짜에 맞춘다(2026-09-23). 예전에는 날짜와 무관한 한 줄 목록이라, 9월 날짜에 '한겨울' 글이나
이미 끝난 축제 글이 붙을 수 있었다.
- 축제: 시작 FESTIVAL_LEAD_DAYS 일 전 ~ 끝나는 날 사이에만. 기간을 모르는 축제는 쓰지 않는다.
- 계절: 게시일의 절기 하나. 키에 연도를 넣어 해마다 한 번씩 다시 쓸 수 있다.
- 날씨: 그 달에 있을 법한 것만. 키에 연·월을 넣어 달마다 다시 쓸 수 있다.
- 주변 장소: 날짜와 무관해 늘 후보다.
★ 스냅샷의 지역 정보는 원문 행 목록(snapshot["local"]["contents"])이다. 예전 코드는
site_payload 모양(local.festivals·attractions)을 읽어 축제·주변 소재가 늘 비어 있었다."""
contents = (snapshot.get("local") or {}).get("contents") or []
by_type: dict[int, list[dict]] = {}
for row in contents:
if isinstance(row, dict):
by_type.setdefault(row.get("content_type"), []).append(row)
out: list[tuple[int, str, str]] = []
for festival in (local.get("festivals") or [])[:12]:
name = (festival.get("name") or "").strip()
for row in by_type.get(LocalContentType.FESTIVAL.value, []):
body = row.get("body") or {}
name = str(body.get("name") or row.get("title") or "").strip()
start = _ymd(body.get("eventstartdate"))
end = _ymd(body.get("eventenddate")) or start
if not name or start is None or not (start - timedelta(days=FESTIVAL_LEAD_DAYS) <= on <= end):
continue
period = f"{start:%Y.%m.%d}" + (f" ~ {end:%Y.%m.%d}" if end != start else "")
detail = f"{body.get('location') or ''} {str(body.get('overview') or '')[:300]}".strip()
out.append((PostTopicKind.FESTIVAL.value, f"festival:{start.year}:{name}"[:120],
f"{name} (기간 {period}) — {detail}".strip(" —")))
term = season_term(on)
out.append((PostTopicKind.SEASON.value, f"season:{on.year}:{term}", term))
spots = (by_type.get(LocalContentType.ATTRACTION.value, [])
+ by_type.get(LocalContentType.RESTAURANT.value, []))
for row in spots[:20]:
body = row.get("body") or {}
name = str(body.get("name") or row.get("title") or "").strip()
if name:
out.append((PostTopicKind.FESTIVAL.value, f"festival:{name}",
f"{name} — {festival.get('location') or ''} {festival.get('description') or ''}".strip()))
detail = body.get("description") or body.get("overview") or body.get("location") or ""
out.append((PostTopicKind.NEARBY.value, f"nearby:{name}"[:120], f"{name} — {detail}".strip(" —")))
for spot in ((local.get("attractions") or []) + (local.get("restaurants") or []))[:20]:
name = (spot.get("name") or "").strip()
if name:
out.append((PostTopicKind.NEARBY.value, f"nearby:{name}",
f"{name} — {spot.get('description') or spot.get('address') or ''}".strip()))
for sky in ("맑음", "흐림", "비", "눈", "안개", "소나기"):
out.append((PostTopicKind.WEATHER.value, f"weather:{sky}", f"{sky}인 날"))
for term in ("봄", "초여름", "한여름", "초가을", "늦가을", "초겨울", "한겨울"):
out.append((PostTopicKind.SEASON.value, f"season:{term}", term))
for sky in _SKIES + _SKIES_BY_MONTH.get(on.month, ()):
out.append((PostTopicKind.WEATHER.value, f"weather:{on:%Y-%m}:{sky}", f"{sky}인 날"))
return out