Update main.py
Browse files
main.py
CHANGED
|
@@ -50,6 +50,10 @@ API_KEY = os.getenv("API_KEY")
|
|
| 50 |
START_TIME = time.time()
|
| 51 |
order_cache = TTLCache(maxsize=100, ttl=86400)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
class ProductItem(BaseModel):
|
| 54 |
name: str
|
| 55 |
price: str
|
|
@@ -138,6 +142,45 @@ def system_status():
|
|
| 138 |
"total_ram_mb": round(memory.total / 1024 / 1024, 2)
|
| 139 |
}
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
@app.get("/api/v1/invites/discord")
|
| 142 |
async def get_new_invite():
|
| 143 |
res = await invite_app.get_invite()
|
|
|
|
| 50 |
START_TIME = time.time()
|
| 51 |
order_cache = TTLCache(maxsize=100, ttl=86400)
|
| 52 |
|
| 53 |
+
class NoiTuRequest(BaseModel):
|
| 54 |
+
tu_truoc: str
|
| 55 |
+
used: Optional[List[str]] = None
|
| 56 |
+
|
| 57 |
class ProductItem(BaseModel):
|
| 58 |
name: str
|
| 59 |
price: str
|
|
|
|
| 142 |
"total_ram_mb": round(memory.total / 1024 / 1024, 2)
|
| 143 |
}
|
| 144 |
|
| 145 |
+
@app.post("/api/v1/noitu/2-word")
|
| 146 |
+
async def noi_tu_2_am_tiet(request: NoiTuRequest):
|
| 147 |
+
tudien = await load_noi_tu_words()
|
| 148 |
+
if not tudien:
|
| 149 |
+
raise HTTPException(status_code=503, detail="Không load được từ điển, thử lại sau")
|
| 150 |
+
|
| 151 |
+
tu_truoc = request.tu_truoc.strip().lower()
|
| 152 |
+
if len(tu_truoc.split()) != 2:
|
| 153 |
+
raise HTTPException(status_code=400, detail="Từ trước phải đúng 2 âm tiết (cách nhau bằng space)")
|
| 154 |
+
|
| 155 |
+
# Lấy âm tiết cuối
|
| 156 |
+
am_cuoi = tu_truoc.split()[-1]
|
| 157 |
+
|
| 158 |
+
used_set = set(t.lower() for t in (request.used or []))
|
| 159 |
+
|
| 160 |
+
candidates = []
|
| 161 |
+
for tu in tudien:
|
| 162 |
+
if tu not in used_set:
|
| 163 |
+
am_dau = tu.split()[0]
|
| 164 |
+
if am_dau == am_cuoi:
|
| 165 |
+
candidates.append(tu)
|
| 166 |
+
|
| 167 |
+
if not candidates:
|
| 168 |
+
return {
|
| 169 |
+
"ok": False,
|
| 170 |
+
"message": "Hết từ hợp lệ rồi! Bạn thắng hoặc... hết vốn từ :("
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
# Chọn random 1 từ (hoặc trả hết nếu muốn client random)
|
| 174 |
+
import random
|
| 175 |
+
tu_moi = random.choice(candidates)
|
| 176 |
+
|
| 177 |
+
return {
|
| 178 |
+
"ok": True,
|
| 179 |
+
"tu_moi": tu_moi,
|
| 180 |
+
"am_tiet_noi": am_cuoi,
|
| 181 |
+
"so_tu_con_lai_uoc_tinh": len(candidates) # bonus info
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
@app.get("/api/v1/invites/discord")
|
| 185 |
async def get_new_invite():
|
| 186 |
res = await invite_app.get_invite()
|