nex_buddy/server/scripts/seed_fortunes.py

103 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""灌内置月度运势:12 星座 × 12 通用月度,中英各一份。
只灌 scope='month' 的通用模板(每年都适用);指定年月的运势由管理端按需写。
已有条目不覆盖,--force 才覆盖。用法:../.venv/bin/python scripts/seed_fortunes.py [--force]
"""
from __future__ import annotations
import asyncio
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from sqlalchemy import select # noqa: E402
from app.db.session import SessionLocal # noqa: E402
from app.models import Fortune, ZodiacSign # noqa: E402
from app.services.fortune import ELEMENT_ADVICE, MONTH_THEMES # noqa: E402
FORCE = "--force" in sys.argv
# 每个星座开个头,避免 144 条看起来像复制粘贴
SIGN_LEADS = {
"aries": ("势头很足", "Momentum is on your side"),
"taurus": ("步子稳了", "Your footing is steady"),
"gemini": ("想法多起来", "Ideas keep flowing"),
"cancer": ("心绪安定", "Your mind settles"),
"leo": ("气场全开", "Your presence is full-on"),
"virgo": ("细节盘顺", "Details fall into place"),
"libra": ("关系回暖", "Relationships warm up"),
"scorpio": ("直觉在线", "Your intuition is sharp"),
"sagittarius": ("视野打开", "Your horizon widens"),
"capricorn": ("积累见效", "Your efforts compound"),
"aquarius": ("思路清奇", "Fresh angles find you"),
"pisces": ("感受力强", "Your sensitivity peaks"),
}
# 英文主题短名(做标题用;正文的 body_en 太长,塞进标题会变双冒号长句)
THEME_EN = {
1: "Reset & plan", 2: "Nurture bonds", 3: "Start something", 4: "Go deep",
5: "Stay flexible", 6: "Widen your circle", 7: "Rest & recover", 8: "Decide boldly",
9: "Fix the gaps", 10: "Harvest", 11: "Think ahead", 12: "Close the loop",
}
def build(sign: dict) -> dict:
m_th = MONTH_THEMES # month -> (主题zh, 正文zh, 正文en)
month = int(sign["period"])
lead_zh, lead_en = SIGN_LEADS[sign["zodiac_code"]]
adv_zh, adv_en = ELEMENT_ADVICE.get(sign["element"] or "风", ELEMENT_ADVICE["风"])
theme_zh, body_zh, body_en = m_th[month]
title_zh = f"{lead_zh}:{theme_zh}"
title_en = f"{lead_en} · {THEME_EN[month]}"
return {
"title": title_zh,
"title_en": title_en,
"content": f"{lead_zh}。{body_zh}。{adv_zh}。",
"content_en": f"{lead_en}. {body_en}. {adv_en}.",
}
async def main() -> None:
async with SessionLocal() as db:
zmap = {z.code: z for z in (await db.execute(select(ZodiacSign))).scalars().all()}
existing = {
(f.zodiac_code, f.period)
for f in (await db.execute(select(Fortune).where(Fortune.scope == "month"))).scalars()
}
added = skipped = 0
for z in zmap.values():
if not z.enabled:
continue
for month in range(1, 13):
key = (z.code, f"{month:02d}")
if key in existing and not FORCE:
skipped += 1
continue
if key in existing:
row = (
await db.execute(
select(Fortune).where(
Fortune.zodiac_code == z.code,
Fortune.scope == "month",
Fortune.period == key[1],
)
)
).scalars().first()
data = build({"zodiac_code": z.code, "element": z.element, "period": key[1]})
row.title, row.title_en = data["title"], data["title_en"]
row.content, row.content_en = data["content"], data["content_en"]
continue
data = build({"zodiac_code": z.code, "element": z.element, "period": key[1]})
db.add(Fortune(zodiac_code=z.code, scope="month", period=key[1], **data))
added += 1
await db.commit()
print(f"added {added}, skipped {skipped}")
if __name__ == "__main__":
asyncio.run(main())