Update main.py
Browse files
main.py
CHANGED
|
@@ -1,74 +1,60 @@
|
|
| 1 |
-
import
|
| 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 |
-
|
| 10 |
|
|
|
|
| 11 |
from fastapi import FastAPI
|
| 12 |
-
|
|
|
|
| 13 |
from fastapi.responses import (
|
| 14 |
JSONResponse,
|
| 15 |
)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
import uvicorn
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
|
|
|
|
| 22 |
|
| 23 |
-
async def
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
print("Finished update")
|
| 31 |
-
except Exception as e:
|
| 32 |
-
print(f"Update failed: {e}")
|
| 33 |
|
| 34 |
-
|
| 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 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 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 |
-
|
| 63 |
-
|
| 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})
|
|
@@ -103,7 +89,8 @@ def hello_world():
|
|
| 103 |
|
| 104 |
|
| 105 |
def upload_image(data):
|
| 106 |
-
|
|
|
|
| 107 |
files = {'file': ('file', data, "image/png")}
|
| 108 |
response = requests.post(url, files=files)
|
| 109 |
|
|
@@ -112,22 +99,178 @@ def upload_image(data):
|
|
| 112 |
|
| 113 |
try:
|
| 114 |
body = response.json()
|
| 115 |
-
if
|
| 116 |
-
return "
|
| 117 |
else:
|
| 118 |
raise Exception(f"Telegraph error: {body.get('error', 'Unknown error')}")
|
| 119 |
except (ValueError, KeyError, IndexError) as e:
|
| 120 |
raise Exception(f"Failed to parse response: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
|
|
|
| 122 |
if __name__ == "__main__":
|
| 123 |
-
data_dir = "/tmp/data"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 1 |
+
from enkacard import encbanner
|
|
|
|
|
|
|
| 2 |
import asyncio
|
|
|
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
+
import concurrent.futures
|
| 5 |
|
| 6 |
+
# from enkanetwork import EnkaNetworkAPI
|
| 7 |
from fastapi import FastAPI
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
import enkanetwork
|
| 10 |
from fastapi.responses import (
|
| 11 |
JSONResponse,
|
| 12 |
)
|
| 13 |
+
import enkacard
|
|
|
|
| 14 |
import uvicorn
|
| 15 |
+
import requests
|
| 16 |
|
| 17 |
+
# client = EnkaNetworkAPI()
|
| 18 |
|
| 19 |
+
# import asyncio
|
| 20 |
|
| 21 |
+
# async def card():
|
| 22 |
+
# print("cret")
|
| 23 |
+
# async with encbanner.ENC(uid = "811455610") as encard:
|
| 24 |
+
# print("creting")
|
| 25 |
+
# return await encard.creat()
|
| 26 |
|
| 27 |
+
# result = asyncio.run(card())
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# data_dir = "/tmp/data"
|
| 32 |
+
# if not os.path.exists(data_dir):
|
| 33 |
+
# os.makedirs(data_dir)
|
| 34 |
+
# data_dir = "/tmp/langs"
|
| 35 |
+
# if not os.path.exists(data_dir):
|
| 36 |
+
# os.makedirs(data_dir)
|
| 37 |
|
| 38 |
+
# async def main():
|
| 39 |
+
# async with client:
|
| 40 |
+
# await client.update_assets(lang = ["EN", "CHT"], path="/tmp")
|
| 41 |
|
| 42 |
+
# asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
app = FastAPI()
|
| 45 |
# app = FastAPI(lifespan=lifespan)
|
| 46 |
+
async def card(id, designtype):
|
| 47 |
+
async with encbanner.ENC(uid = str(id)) as encard:
|
| 48 |
+
return await encard.creat(template = (2 if str(designtype) == "2" else 1))
|
| 49 |
+
|
| 50 |
@app.get("/{id}") # Correct route definition without prefix
|
| 51 |
async def characters(id: int, design: str = "1"): # Use async and await
|
| 52 |
try:
|
| 53 |
+
# characters = []
|
| 54 |
result = await card(id, design) # Use await instead of asyncio.run()
|
| 55 |
+
print(result)
|
| 56 |
+
characters = process_images(result)
|
| 57 |
+
print(characters)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Return valid JSON response using FastAPI's JSONResponse
|
| 60 |
return JSONResponse(content={'response': characters})
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
def upload_image(data):
|
| 92 |
+
print(data)
|
| 93 |
+
url = "https://fighter-programmer-uploader.hf.space/upload"
|
| 94 |
files = {'file': ('file', data, "image/png")}
|
| 95 |
response = requests.post(url, files=files)
|
| 96 |
|
|
|
|
| 99 |
|
| 100 |
try:
|
| 101 |
body = response.json()
|
| 102 |
+
if body["url"]:
|
| 103 |
+
return body["url"]
|
| 104 |
else:
|
| 105 |
raise Exception(f"Telegraph error: {body.get('error', 'Unknown error')}")
|
| 106 |
except (ValueError, KeyError, IndexError) as e:
|
| 107 |
raise Exception(f"Failed to parse response: {str(e)}")
|
| 108 |
+
def process_image(dt):
|
| 109 |
+
with BytesIO() as byte_io:
|
| 110 |
+
dt.card.save(byte_io, "PNG")
|
| 111 |
+
byte_io.seek(0)
|
| 112 |
+
image_url = upload_image(byte_io)
|
| 113 |
+
|
| 114 |
+
return {
|
| 115 |
+
"name": dt.name,
|
| 116 |
+
"url": image_url
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
def process_images(result):
|
| 120 |
+
characters = []
|
| 121 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 122 |
+
# Execute image uploads in parallel
|
| 123 |
+
futures = [executor.submit(process_image, dt) for dt in result.card]
|
| 124 |
+
|
| 125 |
+
for future in concurrent.futures.as_completed(futures):
|
| 126 |
+
try:
|
| 127 |
+
characters.append(future.result())
|
| 128 |
+
except Exception as e:
|
| 129 |
+
print(f"Error processing image: {e}")
|
| 130 |
|
| 131 |
+
return characters
|
| 132 |
if __name__ == "__main__":
|
| 133 |
+
# data_dir = "/tmp/data"
|
| 134 |
+
|
| 135 |
+
# if not os.path.exists(data_dir):
|
| 136 |
+
# os.makedirs(data_dir)
|
| 137 |
+
|
| 138 |
+
# data_dir = "/tmp/langs"
|
| 139 |
+
|
| 140 |
+
# if not os.path.exists(data_dir):
|
| 141 |
+
# os.makedirs(data_dir)
|
| 142 |
+
# asyncio.run(update_genshin())
|
| 143 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8, timeout_keep_alive=60000)
|
| 144 |
+
# import enkacard
|
| 145 |
+
# import enkacard.encbanner
|
| 146 |
+
# import enkanetwork
|
| 147 |
+
# import asyncio
|
| 148 |
+
# import requests
|
| 149 |
+
# from io import BytesIO
|
| 150 |
+
# from enkanetwork import EnkaNetworkAPI
|
| 151 |
+
# import os
|
| 152 |
+
# from contextlib import asynccontextmanager
|
| 153 |
+
|
| 154 |
+
# from fastapi import FastAPI
|
| 155 |
+
|
| 156 |
+
# from fastapi.responses import (
|
| 157 |
+
# JSONResponse,
|
| 158 |
+
# )
|
| 159 |
+
# from enkacard import enkatools
|
| 160 |
+
|
| 161 |
+
# import uvicorn
|
| 162 |
+
|
| 163 |
+
# enka_update = EnkaNetworkAPI()
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
# async def update_genshin():
|
| 167 |
+
# try:
|
| 168 |
+
# async with enka_update:
|
| 169 |
+
# await enka_update.update_assets(path="/tmp", lang=["EN"])
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
# # lang=["EN"],
|
| 173 |
+
# print("Finished update")
|
| 174 |
+
# except Exception as e:
|
| 175 |
+
# print(f"Update failed: {e}")
|
| 176 |
+
|
| 177 |
+
# async def card(id, designtype):
|
| 178 |
+
# async with enkacard.encbanner.ENC(uid = str(id)) as encard:
|
| 179 |
+
# return await encard.creat(template = (2 if str(designtype) == "2" else 1))
|
| 180 |
+
|
| 181 |
+
# @asynccontextmanager
|
| 182 |
+
# async def lifespan(app: FastAPI):
|
| 183 |
+
# data_dir = "/tmp/data"
|
| 184 |
+
|
| 185 |
+
# if not os.path.exists(data_dir):
|
| 186 |
+
# os.makedirs(data_dir)
|
| 187 |
+
|
| 188 |
+
# data_dir = "/tmp/langs"
|
| 189 |
+
|
| 190 |
+
# if not os.path.exists(data_dir):
|
| 191 |
+
# os.makedirs(data_dir)
|
| 192 |
+
# await update_genshin()
|
| 193 |
+
# yield
|
| 194 |
+
# print("Goodbye")
|
| 195 |
+
|
| 196 |
+
# app = FastAPI()
|
| 197 |
+
# # app = FastAPI(lifespan=lifespan)
|
| 198 |
+
|
| 199 |
+
# @app.get("/{id}") # Correct route definition without prefix
|
| 200 |
+
# async def characters(id: int, design: str = "1"): # Use async and await
|
| 201 |
+
# try:
|
| 202 |
+
# characters = []
|
| 203 |
+
# result = await card(id, design) # Use await instead of asyncio.run()
|
| 204 |
+
|
| 205 |
+
# for dt in result.card:
|
| 206 |
+
# with BytesIO() as byte_io:
|
| 207 |
+
# dt.card.save(byte_io, "PNG")
|
| 208 |
+
# byte_io.seek(0)
|
| 209 |
+
# image_url = upload_image(byte_io)
|
| 210 |
+
|
| 211 |
+
# characters.append({
|
| 212 |
+
# "name": dt.name,
|
| 213 |
+
# "url": image_url
|
| 214 |
+
# })
|
| 215 |
+
|
| 216 |
+
# # Return valid JSON response using FastAPI's JSONResponse
|
| 217 |
+
# return JSONResponse(content={'response': characters})
|
| 218 |
+
|
| 219 |
+
# except enkanetwork.exception.VaildateUIDError:
|
| 220 |
+
# return JSONResponse(content={'error': 'Invalid UID. Please check your UID.'}, status_code=400)
|
| 221 |
+
|
| 222 |
+
# except enkacard.enc_error.ENCardError:
|
| 223 |
+
# return JSONResponse(content={'error': 'Enable display of the showcase in the game or add characters there.'}, status_code=400)
|
| 224 |
+
|
| 225 |
+
# except Exception as e:
|
| 226 |
+
# return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
|
| 227 |
+
|
| 228 |
+
# @app.get("/")
|
| 229 |
+
# def hello_world():
|
| 230 |
+
# return 'AMERICA YA HALLO!!'
|
| 231 |
+
|
| 232 |
+
# # @app.route("/update_char")
|
| 233 |
+
# # def upload():
|
| 234 |
+
# # data_dir = "/tmp/data"
|
| 235 |
+
|
| 236 |
+
# # if not os.path.exists(data_dir):
|
| 237 |
+
# # os.makedirs(data_dir)
|
| 238 |
+
|
| 239 |
+
# # data_dir = "/tmp/langs"
|
| 240 |
+
|
| 241 |
+
# # if not os.path.exists(data_dir):
|
| 242 |
+
# # os.makedirs(data_dir)
|
| 243 |
+
# # asyncio.run(update_genshin())
|
| 244 |
+
# # return 'Update smth ig!!'
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
# def upload_image(data):
|
| 249 |
+
# url = "https://telegra.ph/upload"
|
| 250 |
+
# files = {'file': ('file', data, "image/png")}
|
| 251 |
+
# response = requests.post(url, files=files)
|
| 252 |
+
|
| 253 |
+
# if response.status_code != 200:
|
| 254 |
+
# raise Exception(f"HTTP Error: {response.status_code}")
|
| 255 |
+
|
| 256 |
+
# try:
|
| 257 |
+
# body = response.json()
|
| 258 |
+
# if isinstance(body, list) and 'src' in body[0]:
|
| 259 |
+
# return "https://telegra.ph" + body[0]['src']
|
| 260 |
+
# else:
|
| 261 |
+
# raise Exception(f"Telegraph error: {body.get('error', 'Unknown error')}")
|
| 262 |
+
# except (ValueError, KeyError, IndexError) as e:
|
| 263 |
+
# raise Exception(f"Failed to parse response: {str(e)}")
|
| 264 |
+
|
| 265 |
+
# if __name__ == "__main__":
|
| 266 |
+
# data_dir = "/tmp/data"
|
| 267 |
|
| 268 |
+
# if not os.path.exists(data_dir):
|
| 269 |
+
# os.makedirs(data_dir)
|
| 270 |
|
| 271 |
+
# data_dir = "/tmp/langs"
|
| 272 |
|
| 273 |
+
# if not os.path.exists(data_dir):
|
| 274 |
+
# os.makedirs(data_dir)
|
| 275 |
+
# asyncio.run(update_genshin())
|
| 276 |
+
# uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8, timeout_keep_alive=600)
|