-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
312 lines (259 loc) · 10.8 KB
/
main.py
File metadata and controls
312 lines (259 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import json
from fastapi import FastAPI, Request, Depends, HTTPException, Query
from fastapi.responses import FileResponse
from sqlalchemy import inspect, text
from sqlalchemy.orm import Session
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
from app import database, models
from app.services.lunar_service import get_lunar_date
from app.services.saju_engine import calculate_saju, get_fortune_by_period, get_dominant_element, ARCHETYPES, RHYTHM_KEYWORDS, RELATIONS, get_element
from app.services.prompt_builder import build_saju_prompt, translate_pillar
from app.services.llm_service import get_ai_analysis
from app.services.geo_service import geo_service
from pydantic import BaseModel
from typing import Optional
import redis
import os
app = FastAPI()
@app.on_event("startup")
def startup_event():
db_url = str(database.engine.url).split('@')[-1]
print(f"[DB] Connecting to: {db_url}")
try:
models.Base.metadata.create_all(bind=database.engine)
inspector = inspect(database.engine)
user_columns = {col["name"] for col in inspector.get_columns("users")} if inspector.has_table("users") else set()
if "nickname" not in user_columns:
with database.engine.begin() as conn:
conn.execute(text("ALTER TABLE users ADD COLUMN nickname VARCHAR"))
print("[DB] Tables ready.")
except Exception as e:
print(f"[DB] Table creation failed: {e}")
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
REDIS_URL = os.getenv("REDIS_URL")
r = redis.from_url(REDIS_URL) if REDIS_URL else None
class SajuRequest(BaseModel):
birth_date: str
birth_time: str
timezone: str = "Asia/Seoul"
longitude: float = 127.0
latitude: float = 37.5
include_analysis: bool = False
theme: str = "general"
user_id: Optional[str] = None
birth_place: Optional[str] = None
class UserLogin(BaseModel):
uid: str
email: str
provider: str = "google"
nickname: Optional[str] = None
class FeedPostRequest(BaseModel):
record_id: int
caption: Optional[str] = None
# ---------------------------------------------------------------------------
# Auth
# ---------------------------------------------------------------------------
@app.post("/login")
def login_user(payload: UserLogin, db: Session = Depends(database.get_db)):
user = db.query(models.User).filter(models.User.uid == payload.uid).first()
if not user:
user = models.User(
uid=payload.uid,
email=payload.email,
provider=payload.provider,
nickname=payload.nickname,
)
db.add(user)
else:
user.email = payload.email
user.provider = payload.provider
if payload.nickname is not None:
user.nickname = payload.nickname
db.commit()
db.refresh(user)
return {"status": "ok", "uid": user.uid}
# ---------------------------------------------------------------------------
# Core: Saju calculate
# ---------------------------------------------------------------------------
@app.post("/saju")
@limiter.limit("60/minute")
def saju_calculate(request: Request, payload: SajuRequest, db: Session = Depends(database.get_db)):
if payload.birth_place:
geo_info = geo_service.get_location_info(payload.birth_place)
if geo_info:
payload.latitude = geo_info['latitude']
payload.longitude = geo_info['longitude']
payload.timezone = geo_info['timezone']
lat_r = round(payload.latitude, 2)
lon_r = round(payload.longitude, 2)
cache_key = f"saju:{payload.birth_date}:{payload.birth_time}:{payload.timezone}:{lat_r}:{lon_r}:{payload.theme}"
cached_payload = None
if r:
cached = r.get(cache_key)
if cached:
cached_payload = json.loads(cached)
try:
if cached_payload:
res_data = cached_payload
else:
y, m, d = map(int, payload.birth_date.split("-"))
h, mn = map(int, payload.birth_time.split(":"))
lunar = get_lunar_date(y, m, d, h, mn, payload.timezone, payload.longitude)
saju_res = calculate_saju(
lunar["solar"]["year"],
lunar["solar"]["month"],
lunar["solar"]["day"],
lunar["solar"]["hour"],
lunar["solar"]["minute"]
)
dominant = get_dominant_element(saju_res["elements"])
archetype = ARCHETYPES.get(dominant, "The Explorer")
res_data = {
"lunar": lunar["lunar"],
"saju": saju_res["pillars"],
"elements": saju_res["elements"],
"dominant": dominant,
"archetype": archetype,
}
if r:
r.setex(cache_key, 3600, json.dumps(res_data))
analysis_result = None
if payload.include_analysis:
prompt = build_saju_prompt(
{"saju": {"pillars": res_data["saju"], "elements": res_data["elements"]}},
theme=payload.theme,
)
if payload.birth_place:
prompt += f"\n(Location context: {payload.birth_place}, {payload.timezone})"
analysis_result = get_ai_analysis(prompt)
response_data = dict(res_data)
response_data["analysis"] = analysis_result
new_record = models.SajuRecord(
user_id=payload.user_id,
birth_date=payload.birth_date,
birth_time=payload.birth_time,
timezone=payload.timezone,
longitude=payload.longitude,
day_master=translate_pillar(res_data["saju"]["day"]),
result_json=response_data,
)
db.add(new_record)
db.commit()
db.refresh(new_record)
response_data["record_id"] = new_record.id
return response_data
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ---------------------------------------------------------------------------
# History
# ---------------------------------------------------------------------------
@app.get("/history/{user_id}")
def get_history(user_id: str, db: Session = Depends(database.get_db)):
records = (
db.query(models.SajuRecord)
.filter(models.SajuRecord.user_id == user_id)
.order_by(models.SajuRecord.created_at.desc())
.limit(10)
.all()
)
return [{"record_id": rec.id, "created_at": rec.created_at.isoformat(), **rec.result_json} for rec in records]
# ---------------------------------------------------------------------------
# Fortune (period-based)
# ---------------------------------------------------------------------------
@app.get("/fortune/{period}/{record_id}")
def get_period_fortune(period: str, record_id: int, db: Session = Depends(database.get_db)):
if period not in ("daily", "weekly", "monthly", "yearly", "life"):
raise HTTPException(status_code=400, detail="period must be: daily, weekly, monthly, yearly, life")
rec = db.query(models.SajuRecord).filter(models.SajuRecord.id == record_id).first()
if not rec:
raise HTTPException(status_code=404, detail="Record not found")
user_gan = rec.result_json["saju"]["day"][0]
return get_fortune_by_period(user_gan, period)
# ---------------------------------------------------------------------------
# Feed
# ---------------------------------------------------------------------------
def _build_feed_item(post: models.FeedPost, rec: models.SajuRecord, db: Session) -> dict:
rj = rec.result_json
elements = rj.get("elements", {})
dominant = rj.get("dominant") or get_dominant_element(elements)
archetype = rj.get("archetype") or ARCHETYPES.get(dominant, "The Explorer")
user_gan = rj["saju"]["day"][0]
user_elm = get_element(user_gan)
from datetime import datetime
now = datetime.now()
yearly_solar = __import__("lunar_python").Solar.fromYmd(now.year, 2, 4)
yearly_eight = yearly_solar.getLunar().getEightChar()
yearly_elm = get_element(yearly_eight.getYearGan())
year_relation = RELATIONS.get((user_elm, yearly_elm), "Friend")
rhythm = RHYTHM_KEYWORDS.get(year_relation, "Steady")
display_name = archetype
if rec.user_id:
user = db.query(models.User).filter(models.User.uid == rec.user_id).first()
if user and user.nickname:
display_name = user.nickname
return {
"post_id": post.id,
"record_id": post.record_id,
"display_name": display_name,
"archetype": archetype,
"dominant": dominant,
"rhythm": rhythm,
"year_theme": f"{now.year} Pattern",
"elements": elements,
"caption": post.caption,
"created_at": post.created_at.isoformat(),
}
@app.get("/feed")
def list_feed(
limit: int = Query(20, ge=1, le=100),
offset: int = Query(0, ge=0),
db: Session = Depends(database.get_db),
):
posts = (
db.query(models.FeedPost)
.order_by(models.FeedPost.created_at.desc())
.offset(offset)
.limit(limit)
.all()
)
result = []
for post in posts:
rec = db.query(models.SajuRecord).filter(models.SajuRecord.id == post.record_id).first()
if rec:
result.append(_build_feed_item(post, rec, db))
return result
@app.post("/feed")
def create_feed_post(payload: FeedPostRequest, db: Session = Depends(database.get_db)):
rec = db.query(models.SajuRecord).filter(models.SajuRecord.id == payload.record_id).first()
if not rec:
raise HTTPException(status_code=404, detail="Record not found")
existing = db.query(models.FeedPost).filter(models.FeedPost.record_id == payload.record_id).first()
if existing:
existing.caption = payload.caption
db.commit()
db.refresh(existing)
return {"status": "ok", "post_id": existing.id}
post = models.FeedPost(record_id=payload.record_id, caption=payload.caption)
db.add(post)
db.commit()
db.refresh(post)
return {"status": "ok", "post_id": post.id}
@app.get("/feed/{post_id}")
def get_feed_post(post_id: int, db: Session = Depends(database.get_db)):
post = db.query(models.FeedPost).filter(models.FeedPost.id == post_id).first()
if not post:
raise HTTPException(status_code=404, detail="Post not found")
rec = db.query(models.SajuRecord).filter(models.SajuRecord.id == post.record_id).first()
if not rec:
raise HTTPException(status_code=404, detail="Record not found")
return _build_feed_item(post, rec, db)
# ---------------------------------------------------------------------------
# PWA
# ---------------------------------------------------------------------------
@app.get("/manifest.json")
def get_manifest():
return FileResponse("manifest.json")