Update main.py
Browse files
main.py
CHANGED
@@ -2,66 +2,105 @@ import enkacard
|
|
2 |
import enkacard.encbanner
|
3 |
import enkanetwork
|
4 |
import asyncio
|
5 |
-
from flask import Flask, jsonify, request
|
6 |
import requests
|
7 |
from io import BytesIO
|
8 |
from enkanetwork import EnkaNetworkAPI
|
9 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
enka_update = EnkaNetworkAPI()
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
async def card(id, designtype):
|
16 |
async with enkacard.encbanner.ENC(uid = str(id)) as encard:
|
17 |
return await encard.creat(template = (2 if str(designtype) == "2" else 1))
|
18 |
|
19 |
-
@
|
20 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
try:
|
22 |
characters = []
|
23 |
-
result =
|
|
|
24 |
for dt in result.card:
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
characters.append({
|
31 |
-
"name": dt.name,
|
32 |
-
"url": image_url
|
33 |
-
})
|
34 |
-
return jsonify({'response': characters})
|
35 |
-
except enkanetwork.exception.VaildateUIDError:
|
36 |
-
# return jsonify({'error': 'Invalid UID. Please check your UID.'}), 400
|
37 |
|
38 |
-
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
return jsonify({'error': 'Enable display of the showcase in the game or add characters there.'})
|
43 |
-
except Exception as e:
|
44 |
|
45 |
-
|
|
|
|
|
|
|
|
|
46 |
|
|
|
|
|
47 |
|
48 |
-
@app.
|
49 |
def hello_world():
|
50 |
return 'AMERICA YA HALLO!!'
|
51 |
|
52 |
-
@app.route("/update_char")
|
53 |
-
def upload():
|
54 |
-
data_dir = "/tmp/data"
|
55 |
|
56 |
-
if not os.path.exists(data_dir):
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
data_dir = "/tmp/langs"
|
60 |
|
61 |
-
if not os.path.exists(data_dir):
|
62 |
-
os.makedirs(data_dir)
|
63 |
-
asyncio.run(update_genshin())
|
64 |
-
return 'Update smth ig!!'
|
65 |
|
66 |
def upload_image(data):
|
67 |
url = "https://telegra.ph/upload"
|
@@ -81,4 +120,14 @@ def upload_image(data):
|
|
81 |
raise Exception(f"Failed to parse response: {str(e)}")
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import enkacard.encbanner
|
3 |
import enkanetwork
|
4 |
import asyncio
|
|
|
5 |
import requests
|
6 |
from io import BytesIO
|
7 |
from enkanetwork import EnkaNetworkAPI
|
8 |
import os
|
9 |
+
from contextlib import asynccontextmanager
|
10 |
+
|
11 |
+
from fastapi import FastAPI
|
12 |
+
|
13 |
+
from fastapi.responses import (
|
14 |
+
JSONResponse,
|
15 |
+
)
|
16 |
+
from enkacard import enkatools
|
17 |
+
|
18 |
+
import uvicorn
|
19 |
|
20 |
enka_update = EnkaNetworkAPI()
|
21 |
|
22 |
+
|
23 |
+
async def update_genshin():
|
24 |
+
try:
|
25 |
+
async with enka_update:
|
26 |
+
await enka_update.update_assets(path="/tmp", lang=["EN"])
|
27 |
+
|
28 |
+
|
29 |
+
# lang=["EN"],
|
30 |
+
print("Finished update")
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Update failed: {e}")
|
33 |
|
34 |
async def card(id, designtype):
|
35 |
async with enkacard.encbanner.ENC(uid = str(id)) as encard:
|
36 |
return await encard.creat(template = (2 if str(designtype) == "2" else 1))
|
37 |
|
38 |
+
@asynccontextmanager
|
39 |
+
async def lifespan(app: FastAPI):
|
40 |
+
data_dir = "/tmp/data"
|
41 |
+
|
42 |
+
if not os.path.exists(data_dir):
|
43 |
+
os.makedirs(data_dir)
|
44 |
+
|
45 |
+
data_dir = "/tmp/langs"
|
46 |
+
|
47 |
+
if not os.path.exists(data_dir):
|
48 |
+
os.makedirs(data_dir)
|
49 |
+
await update_genshin()
|
50 |
+
yield
|
51 |
+
print("Goodbye")
|
52 |
+
|
53 |
+
app = FastAPI()
|
54 |
+
# app = FastAPI(lifespan=lifespan)
|
55 |
+
|
56 |
+
@app.get("/{id}") # Correct route definition without prefix
|
57 |
+
async def characters(id: int, design: str = "1"): # Use async and await
|
58 |
try:
|
59 |
characters = []
|
60 |
+
result = await card(id, design) # Use await instead of asyncio.run()
|
61 |
+
|
62 |
for dt in result.card:
|
63 |
+
with BytesIO() as byte_io:
|
64 |
+
dt.card.save(byte_io, "PNG")
|
65 |
+
byte_io.seek(0)
|
66 |
+
image_url = upload_image(byte_io)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
characters.append({
|
69 |
+
"name": dt.name,
|
70 |
+
"url": image_url
|
71 |
+
})
|
72 |
|
73 |
+
# Return valid JSON response using FastAPI's JSONResponse
|
74 |
+
return JSONResponse(content={'response': characters})
|
|
|
|
|
75 |
|
76 |
+
except enkanetwork.exception.VaildateUIDError:
|
77 |
+
return JSONResponse(content={'error': 'Invalid UID. Please check your UID.'}, status_code=400)
|
78 |
+
|
79 |
+
except enkacard.enc_error.ENCardError:
|
80 |
+
return JSONResponse(content={'error': 'Enable display of the showcase in the game or add characters there.'}, status_code=400)
|
81 |
|
82 |
+
except Exception as e:
|
83 |
+
return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
|
84 |
|
85 |
+
@app.get("/")
|
86 |
def hello_world():
|
87 |
return 'AMERICA YA HALLO!!'
|
88 |
|
89 |
+
# @app.route("/update_char")
|
90 |
+
# def upload():
|
91 |
+
# data_dir = "/tmp/data"
|
92 |
|
93 |
+
# if not os.path.exists(data_dir):
|
94 |
+
# os.makedirs(data_dir)
|
95 |
+
|
96 |
+
# data_dir = "/tmp/langs"
|
97 |
+
|
98 |
+
# if not os.path.exists(data_dir):
|
99 |
+
# os.makedirs(data_dir)
|
100 |
+
# asyncio.run(update_genshin())
|
101 |
+
# return 'Update smth ig!!'
|
102 |
|
|
|
103 |
|
|
|
|
|
|
|
|
|
104 |
|
105 |
def upload_image(data):
|
106 |
url = "https://telegra.ph/upload"
|
|
|
120 |
raise Exception(f"Failed to parse response: {str(e)}")
|
121 |
|
122 |
if __name__ == "__main__":
|
123 |
+
data_dir = "/tmp/data"
|
124 |
+
|
125 |
+
if not os.path.exists(data_dir):
|
126 |
+
os.makedirs(data_dir)
|
127 |
+
|
128 |
+
data_dir = "/tmp/langs"
|
129 |
+
|
130 |
+
if not os.path.exists(data_dir):
|
131 |
+
os.makedirs(data_dir)
|
132 |
+
asyncio.run(update_genshin())
|
133 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8, timeout_keep_alive=600)
|