-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
421 lines (350 loc) · 15.3 KB
/
main.py
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import os
import json
import hashlib
import redis
import uvicorn
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException, BackgroundTasks, Request, Header
from fastapi.responses import RedirectResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
from sqlalchemy import or_
from typing import Optional, Dict
from fetcher import fetch_genshin_impact_update, fetch_zzz_update, fetch_starrail_update
from api_config import game_name_id_map, DOCS_URL, ACCEPTED_LANGUAGES, LANGUAGE_PAIRS, TOKEN, API_VERSION
from base_logger import logger
from db.mysql_db import SessionLocal
from db.schemas import TranslateRequest, TranslateResponse
from db import crud
from db import models
# ------------------------------------------------------------------------
# UTILITY FUNCTIONS
# ------------------------------------------------------------------------
def get_game_id_by_name(this_game_name: str) -> Optional[int]:
return game_name_id_map.get(this_game_name, None)
# ------------------------------------------------------------------------
# MD5 CACHE
# ------------------------------------------------------------------------
md5_dict_cache: Dict[str, Dict[str, str]] = {}
# ------------------------------------------------------------------------
# FASTAPI APP SETUP
# ------------------------------------------------------------------------
@asynccontextmanager
async def lifespan(fastapi_app: FastAPI):
try:
logger.info("Starting FastAPI app")
redis_host = os.getenv("REDIS_HOST", "redis")
redis_pool = redis.ConnectionPool.from_url(f"redis://{redis_host}", db=0)
fastapi_app.state.redis = redis_pool
logger.info("Connected to Redis")
app.state.mysql = SessionLocal()
logger.info("Connected to MySQL")
yield
logger.info("Shutting down FastAPI app")
finally:
await fastapi_app.shutdown()
app = FastAPI(
title="UIGF API",
summary="Supporting localization API for UIGF-Org",
description="This API provides localization support for various games, for more information, please refer to the [UIGF-Org](https://github.com/UIGF-org)",
version=API_VERSION,
docs_url=DOCS_URL,
redoc_url=None,
lifespan=lifespan
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ------------------------------------------------------------------------
# ROUTES
# ------------------------------------------------------------------------
@app.get("/", response_class=RedirectResponse, status_code=302)
async def root():
return "api/v1/docs"
@app.post("/translate", response_model=TranslateResponse, tags=["translate"])
async def translate(request_data: TranslateRequest, request: Request):
"""
Translate an item name to an item ID, or vice versa.
- **type**: "normal" or "reverse"
- **lang**: Language code (e.g. "en", "zh-cn")
- **game**: Game name (e.g. "genshin", "starrail")
- **item_name**: The item name to translate
- **item_id**: The item ID to translate
If **`normal`**, text -> item_id
If **`reverse`**, item_id -> text
"""
db = request.app.state.mysql
# Normalize language
lang = request_data.lang.lower()
if lang not in ACCEPTED_LANGUAGES:
# Attempt to convert 5-letter code if possible
return HTTPException(status_code=403, detail="Language not supported")
if len(lang) == 5 and lang in LANGUAGE_PAIRS:
lang = LANGUAGE_PAIRS[lang]
else:
raise HTTPException(status_code=403, detail="Language not supported")
game_id = get_game_id_by_name(request_data.game)
if game_id is None:
raise HTTPException(status_code=403, detail="Game not supported")
translate_type = request_data.type.lower()
# ------------------------------------------------------------------
# Translate "normal": from text -> item_id
# ------------------------------------------------------------------
if translate_type == "normal":
word = request_data.item_name
if not word:
raise HTTPException(status_code=400, detail="item_name must be provided")
column_attr = crud.get_lang_column(lang)
if not column_attr:
raise HTTPException(status_code=403, detail="Language not recognized")
if word.startswith("[") and word.endswith("]"):
# It's a list of words
word_list = json.loads(word)
rows = (
db.query(column_attr, getattr(column_attr.property.parent.class_, 'item_id'))
.filter_by(game_id=game_id)
.filter(column_attr.in_(word_list))
.all()
)
# build { text_value -> item_id }
text_to_id_map = {}
for text_val, item_id_val in rows:
if text_val:
text_to_id_map[text_val] = item_id_val
# For each in the original list, get item_id
result_list = [text_to_id_map.get(w, 0) for w in word_list]
return TranslateResponse(item_id=result_list)
else:
row = (
db.query(getattr(column_attr.property.parent.class_, 'item_id'))
.filter_by(game_id=game_id)
.filter(column_attr == word)
.first()
)
if not row:
raise HTTPException(status_code=404, detail="Hash ID not found")
return TranslateResponse(item_id=row[0], item_name=word)
# ------------------------------------------------------------------
# Translate "reverse": from item_id -> text
# ------------------------------------------------------------------
elif translate_type == "reverse":
item_id = request_data.item_id
if not item_id:
raise HTTPException(status_code=400, detail="item_id must be provided")
column_attr = crud.get_lang_column(lang)
if not column_attr:
raise HTTPException(status_code=403, detail="Language not recognized")
if item_id.startswith("[") and item_id.endswith("]"):
# It's a list
item_id_list = json.loads(item_id)
rows = (
db.query(getattr(column_attr.property.parent.class_, 'item_id'), column_attr)
.filter_by(game_id=game_id)
.filter(getattr(column_attr.property.parent.class_, 'item_id').in_(item_id_list))
.all()
)
id_to_text_map = {r[0]: r[1] for r in rows}
return_list = [id_to_text_map.get(iid, "") for iid in item_id_list]
return TranslateResponse(item_name=return_list)
else:
row = (
db.query(column_attr)
.filter_by(game_id=game_id, item_id=item_id)
.first()
)
if not row:
raise HTTPException(status_code=404, detail="Word at this ID not found")
return TranslateResponse(item_name=row[0], item_id=item_id)
else:
raise HTTPException(status_code=403, detail="Translate type not supported")
def build_language_filter(word: str):
or_conditions = []
for lang_code in ACCEPTED_LANGUAGES:
column_attr = crud.get_lang_column(lang_code)
or_conditions.append(column_attr == word)
return or_(*or_conditions)
@app.get("/identify/{this_game_name}/{word}", tags=["translate"])
async def identify_item_in_i18n(this_game_name: str, word: str, request: Request):
"""
Identify an item in the i18n_dict, if the string is found in any language column.
"""
db = request.app.state.mysql
game_id = get_game_id_by_name(this_game_name)
if game_id is None:
raise HTTPException(status_code=404, detail="Game not supported")
# Dynamically generate column selection
# Use get_lang_column(lang) to get the column attribute
# Logic: or_(xxx_text == word, yyy_text == word, ...)
or_clauses = []
for lang_code in ACCEPTED_LANGUAGES:
col_attr = crud.get_lang_column(lang_code)
if col_attr is not None:
or_clauses.append(col_attr == word)
if not or_clauses:
raise HTTPException(status_code=500, detail="No valid language columns found")
# Look up the word in the database
results = db.query(models.I18nDict).filter(
models.I18nDict.game_id == game_id,
or_(*or_clauses)
).all()
if not results:
raise HTTPException(status_code=404, detail="Hash ID not found")
# Match the results to the language codes
# if string matched, then the language code is determined
# Convert the language code to 5-letter code
reversed_lp = {v: k for k, v in LANGUAGE_PAIRS.items()}
matched_items = []
for row in results:
matched_langs = []
for lang_code in ACCEPTED_LANGUAGES:
col_attr = crud.get_lang_column(lang_code)
if col_attr is None:
continue
if getattr(row, f"{lang_code}_text") == word:
matched_5letter = reversed_lp.get(lang_code, lang_code)
matched_langs.append(matched_5letter)
matched_items.append({
"item_id": row.item_id,
"matched_langs": matched_langs
})
return {
"count": len(matched_items),
"matched": matched_items
}
@app.get("/dict/{this_game_name}/{lang}.json", tags=["dictionary"])
async def download_language_dict_json(this_game_name: str, lang: str, request: Request):
db = request.app.state.mysql
# Basic sanity checks
lang = lang.lower()
if lang not in ACCEPTED_LANGUAGES and lang not in ["all", "md5"]:
if len(lang) == 5 and lang in LANGUAGE_PAIRS:
lang = LANGUAGE_PAIRS[lang]
else:
raise HTTPException(status_code=403, detail="Language not supported")
file_path = f"dict/{this_game_name}/{lang}.json"
if os.path.exists(file_path):
return FileResponse(
path=file_path,
filename=f"{lang}.json",
media_type="application/json"
)
# Else try to create it
if lang in ACCEPTED_LANGUAGES:
if make_language_dict_json(lang, this_game_name, db) and os.path.exists(file_path):
return FileResponse(
path=file_path,
filename=f"{lang}.json",
media_type="application/json"
)
else:
raise HTTPException(status_code=400, detail="Failed to create dictionary.")
raise HTTPException(status_code=400, detail="Invalid request.")
def make_language_dict_json(lang: str, this_game_name: str, db):
""" Re-build the dict file for one language. """
game_id = get_game_id_by_name(this_game_name)
if not game_id:
return False
col_attr = crud.get_lang_column(lang)
if not col_attr:
return False
rows = db.query(models.I18nDict.item_id, col_attr).filter_by(game_id=game_id).all()
os.makedirs(f"dict/{this_game_name}", exist_ok=True)
lang_dict = {}
for (item_id, text_value) in rows:
if text_value:
lang_dict[text_value] = item_id
with open(f"dict/{this_game_name}/{lang}.json", "w", encoding="utf-8") as f:
json.dump(lang_dict, f, indent=4, ensure_ascii=False)
return True
@app.get("/refresh/{this_game_name}", tags=["refresh"])
async def refresh(this_game_name: str, background_tasks: BackgroundTasks, request: Request,
x_uigf_token: str = Header(None)):
if x_uigf_token != TOKEN:
raise HTTPException(status_code=403, detail="Token not accepted")
db = request.app.state.mysql
redis_client = redis.Redis.from_pool(request.app.state.redis)
logger.info(f"Received refresh request for {this_game_name}")
background_tasks.add_task(force_refresh_local_data, this_game_name, db, redis_client)
return {"status": "Background refresh task added"}
def force_refresh_local_data(this_game_name: str, db: Session, redis_client: redis.Redis):
""" Runs as a background task: fetch -> wipe -> insert -> build JSON dict -> MD5. """
try:
if this_game_name == "genshin":
localization_dict = fetch_genshin_impact_update()
game_id = 1
elif this_game_name == "starrail":
localization_dict = fetch_starrail_update()
game_id = 2
elif this_game_name == "zzz":
localization_dict = fetch_zzz_update()
game_id = 3
else:
raise HTTPException(status_code=400, detail="Game not supported.")
logger.info(f"Fetched {len(localization_dict)} items from {this_game_name}")
# Clear old data
crud.clear_game_data(db, game_id)
# Insert new data
crud.insert_localization_data(db, redis_client, game_id, localization_dict)
# Build dict files
for language in ACCEPTED_LANGUAGES:
make_language_dict_json(language, this_game_name, db)
# Build all.json
all_language_dict = {}
for language in ACCEPTED_LANGUAGES:
fp = f"dict/{this_game_name}/{language}.json"
if os.path.exists(fp):
with open(fp, "r", encoding="utf-8") as f:
all_language_dict[language] = json.load(f)
with open(f"dict/{this_game_name}/all.json", "w", encoding="utf-8") as f:
json.dump(all_language_dict, f, indent=4, ensure_ascii=False)
make_checksum(this_game_name)
finally:
db.close()
@app.get("/md5/{this_game_name}", tags=["checksum"])
async def get_checksum(this_game_name: str, background_tasks: BackgroundTasks):
if this_game_name not in game_name_id_map:
raise HTTPException(status_code=403, detail="Game name not accepted")
try:
return md5_dict_cache[this_game_name]
except KeyError:
background_tasks.add_task(make_checksum, this_game_name)
return {"status": "No checksum file at this time. Generating..."}
def make_checksum(this_game_name: str):
"""Generate an MD5 for each .json file. If no .json files, forcibly refresh data."""
if this_game_name in game_name_id_map:
work_list = [this_game_name]
elif this_game_name == "all":
work_list = list(game_name_id_map.keys())
else:
return False
for g in work_list:
dict_path = f"dict/{g}"
if not os.path.exists(dict_path):
os.makedirs(dict_path)
file_list = [
f for f in os.listdir(dict_path)
if f.endswith(".json") and "md5" not in f
]
# If none exist, force refresh
if len(file_list) == 0:
raise RuntimeError("No Json files found, forcing refresh.")
checksum_dict = {}
for json_file in file_list:
file_full_path = os.path.join(dict_path, json_file)
with open(file_full_path, "rb") as rf:
file_bytes = rf.read()
md5_val = hashlib.md5(file_bytes).hexdigest()
checksum_dict[json_file.replace(".json", "")] = md5_val
md5_dict_cache[g] = checksum_dict
with open(os.path.join(dict_path, "md5.json"), "w", encoding="utf-8") as wf:
json.dump(checksum_dict, wf, indent=2)
return True
if __name__ == "__main__":
# Ensure dict directories exist
for gname in game_name_id_map.keys():
os.makedirs(f"./dict/{gname}", exist_ok=True)
uvicorn.run(app, host="0.0.0.0", port=8900, proxy_headers=True, forwarded_allow_ips="*")