|
import enkacard |
|
import enkacard.encbanner |
|
import enkanetwork |
|
import asyncio |
|
import requests |
|
from io import BytesIO |
|
from enkanetwork import EnkaNetworkAPI |
|
import os |
|
from contextlib import asynccontextmanager |
|
|
|
from fastapi import FastAPI |
|
|
|
from fastapi.responses import ( |
|
JSONResponse, |
|
) |
|
from enkacard import enkatools |
|
|
|
import uvicorn |
|
|
|
enka_update = EnkaNetworkAPI() |
|
|
|
|
|
async def update_genshin(): |
|
try: |
|
async with enka_update: |
|
await enka_update.update_assets(path="/tmp", lang=["EN"]) |
|
|
|
|
|
|
|
print("Finished update") |
|
except Exception as e: |
|
print(f"Update failed: {e}") |
|
|
|
async def card(id, designtype): |
|
async with enkacard.encbanner.ENC(uid = str(id)) as encard: |
|
return await encard.creat(template = (2 if str(designtype) == "2" else 1)) |
|
|
|
@asynccontextmanager |
|
async def lifespan(app: FastAPI): |
|
data_dir = "/tmp/data" |
|
|
|
if not os.path.exists(data_dir): |
|
os.makedirs(data_dir) |
|
|
|
data_dir = "/tmp/langs" |
|
|
|
if not os.path.exists(data_dir): |
|
os.makedirs(data_dir) |
|
await update_genshin() |
|
yield |
|
print("Goodbye") |
|
|
|
app = FastAPI() |
|
|
|
|
|
@app.get("/{id}") |
|
async def characters(id: int, design: str = "1"): |
|
try: |
|
characters = [] |
|
result = await card(id, design) |
|
|
|
for dt in result.card: |
|
with BytesIO() as byte_io: |
|
dt.card.save(byte_io, "PNG") |
|
byte_io.seek(0) |
|
image_url = upload_image(byte_io) |
|
|
|
characters.append({ |
|
"name": dt.name, |
|
"url": image_url |
|
}) |
|
|
|
|
|
return JSONResponse(content={'response': characters}) |
|
|
|
except enkanetwork.exception.VaildateUIDError: |
|
return JSONResponse(content={'error': 'Invalid UID. Please check your UID.'}, status_code=400) |
|
|
|
except enkacard.enc_error.ENCardError: |
|
return JSONResponse(content={'error': 'Enable display of the showcase in the game or add characters there.'}, status_code=400) |
|
|
|
except Exception as e: |
|
return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500) |
|
|
|
@app.get("/") |
|
def hello_world(): |
|
return 'AMERICA YA HALLO!!' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upload_image(data): |
|
url = "https://telegra.ph/upload" |
|
files = {'file': ('file', data, "image/png")} |
|
response = requests.post(url, files=files) |
|
|
|
if response.status_code != 200: |
|
raise Exception(f"HTTP Error: {response.status_code}") |
|
|
|
try: |
|
body = response.json() |
|
if isinstance(body, list) and 'src' in body[0]: |
|
return "https://telegra.ph" + body[0]['src'] |
|
else: |
|
raise Exception(f"Telegraph error: {body.get('error', 'Unknown error')}") |
|
except (ValueError, KeyError, IndexError) as e: |
|
raise Exception(f"Failed to parse response: {str(e)}") |
|
|
|
if __name__ == "__main__": |
|
data_dir = "/tmp/data" |
|
|
|
if not os.path.exists(data_dir): |
|
os.makedirs(data_dir) |
|
|
|
data_dir = "/tmp/langs" |
|
|
|
if not os.path.exists(data_dir): |
|
os.makedirs(data_dir) |
|
asyncio.run(update_genshin()) |
|
uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8, timeout_keep_alive=600) |