test/skills/get-fortune-analysis/lunar_python.py
2026-03-24 04:04:58 +00:00

91 lines
4.0 KiB
Python
Executable File

pip install lunar_python
import datetime
from lunar_python import Lunar, Solar
def get_cyber_divination_data(birth_year, birth_month, birth_day, birth_hour=0, birth_minute=0):
"""
赛博算命核心算法 v2.1
- 输出适配 HTML 前端渲染
"""
# --- 基础配置 (简化版) ---
GAN_WU_XING = {"": "", "": "", "": "", "": "", "": "", "": "", "": "", "": "", "": "", "": ""}
ZHI_WU_XING = {"": "", "": "", "": "", "": "", "": "", "": "", "": "", "": "", "": "", "": "", "": "", "": ""}
RELATIONSHIP = {
"": {"": "", "": "", "": "", "": "被克", "": "被生"},
"": {"": "被生", "": "", "": "", "": "", "": "被克"},
"": {"": "被克", "": "被生", "": "", "": "", "": ""},
"": {"": "", "": "被克", "": "被生", "": "", "": ""},
"": {"": "", "": "", "": "被克", "": "被生", "": ""}
}
TEN_GODS = {
"同_同": "比肩 (Friend)", "异_同": "劫财 (Rob)",
"同_生": "食神 (Artist)", "异_生": "伤官 (Rebel)",
"同_克": "偏财 (Windfall)", "异_克": "正财 (Salary)",
"同_被克": "七杀 (7-Killings)", "异_被克": "正官 (Officer)",
"同_被生": "偏印 (Owl)", "异_被生": "正印 (Seal)"
}
# --- 1. 排盘 ---
solar = Solar.fromYmdHms(birth_year, birth_month, birth_day, birth_hour, birth_minute, 0)
lunar = Lunar.fromSolar(solar)
ba_zi = lunar.getEightChar()
day_master = ba_zi.getDayGan()
dm_element = GAN_WU_XING[day_master]
dm_yin_yang = "" if day_master in ["", "", "", "", ""] else ""
# --- 2. 辅助函数 ---
def get_ten_god(target_gan):
if not target_gan: return ""
target_element = GAN_WU_XING.get(target_gan) or ZHI_WU_XING.get(target_gan)
rel = RELATIONSHIP[dm_element].get(target_element, "")
# 简化阴阳判定
target_yy = "" if target_gan in ["", "", "", "", "", "", "", "", ""] else ""
is_same = (dm_yin_yang == target_yy)
key = f"{'' if is_same else ''}_{rel if rel in ['','','被生','被克'] else ''}"
return TEN_GODS.get(key, "未知")
# --- 3. 旺衰硬规则 ---
score = 0
month_zhi = ba_zi.getMonthZhi()
m_ele = ZHI_WU_XING[month_zhi]
# 得令 (+40)
if RELATIONSHIP[dm_element][m_ele] in ["", "被生"]: score += 40
elif RELATIONSHIP[dm_element][m_ele] == "被克": score -= 20
# 得地 (+15/each)
for zhi in [ba_zi.getYearZhi(), ba_zi.getDayZhi(), ba_zi.getTimeZhi()]:
if ZHI_WU_XING[zhi] == dm_element: score += 15
body_strength = "身强 (Strong)" if score >= 40 else "身弱 (Weak)"
strength_cn = "身强" if score >= 40 else "身弱"
# --- 4. 流年 ---
current_year = datetime.datetime.now().year
# 简单的流年计算 (以立春为界需要更复杂逻辑,这里简化取当年农历年干支)
# 修正:直接用 Lunar 获取当年的干支
current_lunar = Lunar.fromYmd(current_year, 6, 1)
annual_gan = current_lunar.getYearGan()
annual_zhi = current_lunar.getYearZhi()
annual_god = get_ten_god(annual_gan)
return {
"meta": {
"solar_date": f"{birth_year}-{birth_month}-{birth_day}",
"lunar_date": f"{lunar.getYearInChinese()}{lunar.getMonthInChinese()}{lunar.getDayInChinese()}"
},
"bazi": {
"day_master": day_master,
"element": dm_element,
"strength": strength_cn,
"score": score
},
"fortune": {
"current_year": f"{current_year} ({annual_gan}{annual_zhi})",
"year_god": annual_god.split(" ")[0], # 只取中文名,如 "七杀"
"lucky_direction": lunar.getDayPositionCaiDesc() # 财神方位
}
}