Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,27 +1,28 @@
|
|
1 |
-
import os
|
2 |
import asyncio
|
|
|
|
|
3 |
import concurrent.futures
|
4 |
import requests
|
5 |
from fastapi import FastAPI
|
6 |
from io import BytesIO
|
7 |
-
from enkacard import encbanner
|
8 |
from fastapi.responses import JSONResponse
|
9 |
import enkacard
|
10 |
-
import starrailcard
|
11 |
import enkanetwork
|
12 |
import uvicorn
|
13 |
-
|
|
|
|
|
14 |
|
15 |
app = FastAPI()
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
app.mount("/cards", StaticFiles(directory=IMAGE_SAVE_PATH), name="cards")
|
25 |
|
26 |
# Genshin Impact card creation
|
27 |
async def genshin_card(id, designtype):
|
@@ -30,60 +31,14 @@ async def genshin_card(id, designtype):
|
|
30 |
|
31 |
# Star Rail card creation
|
32 |
async def starrail_card(id, designtype):
|
33 |
-
async with starrailcard.Card(seeleland=True, remove_logo=True
|
34 |
return await card.create(id, style=(2 if str(designtype) == "2" else 1))
|
35 |
|
36 |
# Star Rail profile creation
|
37 |
async def starrail_profile(id):
|
38 |
-
async with starrailcard.Card(remove_logo=True, seeleland=True
|
39 |
return await card.create_profile(id, style=2)
|
40 |
|
41 |
-
# Helper function to save the image locally
|
42 |
-
def save_image(data, file_name):
|
43 |
-
file_path = os.path.join(IMAGE_SAVE_PATH, file_name)
|
44 |
-
with open(file_path, "wb") as file:
|
45 |
-
file.write(data)
|
46 |
-
return f"/cards/{file_name}"
|
47 |
-
|
48 |
-
# Process individual image card
|
49 |
-
def process_image(dt, user_id):
|
50 |
-
file_name = f"{dt.id}_{user_id}.png"
|
51 |
-
with BytesIO() as byte_io:
|
52 |
-
dt.card.save(byte_io, "PNG")
|
53 |
-
byte_io.seek(0)
|
54 |
-
# Save the image locally
|
55 |
-
image_url = save_image(byte_io.read(), file_name)
|
56 |
-
return {
|
57 |
-
"name": dt.name,
|
58 |
-
"url": image_url
|
59 |
-
}
|
60 |
-
|
61 |
-
# Process the profile image
|
62 |
-
def process_profile(profile_card, user_id):
|
63 |
-
file_name = f"profile_{user_id}.png"
|
64 |
-
with BytesIO() as byte_io:
|
65 |
-
profile_card.card.save(byte_io, "PNG")
|
66 |
-
byte_io.seek(0)
|
67 |
-
# Save the profile image locally
|
68 |
-
image_url = save_image(byte_io.read(), file_name)
|
69 |
-
return {
|
70 |
-
"name": profile_card.character_name,
|
71 |
-
"url": image_url,
|
72 |
-
"character_ids": profile_card.character_id
|
73 |
-
}
|
74 |
-
|
75 |
-
# Process all the images returned
|
76 |
-
def process_images(result, user_id):
|
77 |
-
characters = []
|
78 |
-
with concurrent.futures.ThreadPoolExecutor() as executor:
|
79 |
-
futures = [executor.submit(process_image, dt, user_id) for dt in result.card]
|
80 |
-
for future in concurrent.futures.as_completed(futures):
|
81 |
-
try:
|
82 |
-
characters.append(future.result())
|
83 |
-
except Exception as e:
|
84 |
-
print(f"Error processing image: {e}")
|
85 |
-
return characters
|
86 |
-
|
87 |
# Route for Genshin Impact
|
88 |
@app.get("/genshin/{id}")
|
89 |
async def genshin_characters(id: int, design: str = "1"):
|
@@ -123,6 +78,62 @@ async def starrail_profile_route(id: int):
|
|
123 |
except Exception as e:
|
124 |
return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
|
125 |
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
if __name__ == "__main__":
|
128 |
uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8, timeout_keep_alive=60000)
|
|
|
|
|
|
1 |
import asyncio
|
2 |
+
import os
|
3 |
+
from enkacard import encbanner
|
4 |
import concurrent.futures
|
5 |
import requests
|
6 |
from fastapi import FastAPI
|
7 |
from io import BytesIO
|
|
|
8 |
from fastapi.responses import JSONResponse
|
9 |
import enkacard
|
10 |
+
import starrailcard
|
11 |
import enkanetwork
|
12 |
import uvicorn
|
13 |
+
import cloudinary
|
14 |
+
import cloudinary.uploader
|
15 |
+
from cloudinary.utils import cloudinary_url
|
16 |
|
17 |
app = FastAPI()
|
18 |
|
19 |
+
# Cloudinary configuration
|
20 |
+
cloudinary.config(
|
21 |
+
cloud_name="dob6buqxd",
|
22 |
+
api_key="397945971979515",
|
23 |
+
api_secret="nRMIOSr10xiLo443LLMNDxJBB-Q",
|
24 |
+
secure=True
|
25 |
+
)
|
|
|
26 |
|
27 |
# Genshin Impact card creation
|
28 |
async def genshin_card(id, designtype):
|
|
|
31 |
|
32 |
# Star Rail card creation
|
33 |
async def starrail_card(id, designtype):
|
34 |
+
async with starrailcard.Card(seeleland=True, remove_logo=True) as card:
|
35 |
return await card.create(id, style=(2 if str(designtype) == "2" else 1))
|
36 |
|
37 |
# Star Rail profile creation
|
38 |
async def starrail_profile(id):
|
39 |
+
async with starrailcard.Card(remove_logo=True, seeleland=True) as card:
|
40 |
return await card.create_profile(id, style=2)
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Route for Genshin Impact
|
43 |
@app.get("/genshin/{id}")
|
44 |
async def genshin_characters(id: int, design: str = "1"):
|
|
|
78 |
except Exception as e:
|
79 |
return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
|
80 |
|
81 |
+
@app.get("/")
|
82 |
+
def root():
|
83 |
+
return "Welcome to the API!"
|
84 |
+
|
85 |
+
# Helper function to upload the image to Cloudinary
|
86 |
+
def upload_image(data, character_id, player_id):
|
87 |
+
try:
|
88 |
+
# Set the public_id to include the character ID and the given player ID
|
89 |
+
public_id = f"{character_id}_{player_id}"
|
90 |
+
|
91 |
+
# Upload image to Cloudinary with the specified public_id
|
92 |
+
upload_result = cloudinary.uploader.upload(data, folder="card_images", public_id=public_id,invalidate = True)
|
93 |
+
|
94 |
+
# Get the secure URL of the uploaded image
|
95 |
+
return upload_result["secure_url"]
|
96 |
+
except Exception as e:
|
97 |
+
raise Exception(f"Cloudinary upload error: {str(e)}")
|
98 |
+
|
99 |
+
# Process individual image card
|
100 |
+
def process_image(dt, player_id):
|
101 |
+
with BytesIO() as byte_io:
|
102 |
+
dt.card.save(byte_io, "PNG")
|
103 |
+
byte_io.seek(0)
|
104 |
+
# Upload the image using the character's ID and player ID
|
105 |
+
image_url = upload_image(byte_io, dt.id, player_id)
|
106 |
+
return {
|
107 |
+
"name": dt.name,
|
108 |
+
"id": dt.id,
|
109 |
+
"url": image_url
|
110 |
+
}
|
111 |
+
|
112 |
+
# Process the profile image
|
113 |
+
def process_profile(profile_card, player_id):
|
114 |
+
with BytesIO() as byte_io:
|
115 |
+
profile_card.card.save(byte_io, "PNG")
|
116 |
+
byte_io.seek(0)
|
117 |
+
# Use the character ID and player ID for the profile card
|
118 |
+
image_url = upload_image(byte_io, profile_card.character_id, player_id)
|
119 |
+
return {
|
120 |
+
"name": profile_card.character_name,
|
121 |
+
"url": image_url,
|
122 |
+
"character_ids": profile_card.character_id
|
123 |
+
}
|
124 |
+
|
125 |
+
# Process all the images returned
|
126 |
+
def process_images(result, player_id):
|
127 |
+
characters = []
|
128 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
129 |
+
futures = [executor.submit(process_image, dt, player_id) for dt in result.card]
|
130 |
+
for future in concurrent.futures.as_completed(futures):
|
131 |
+
try:
|
132 |
+
characters.append(future.result())
|
133 |
+
except Exception as e:
|
134 |
+
print(f"Error processing image: {e}")
|
135 |
+
return characters
|
136 |
+
|
137 |
if __name__ == "__main__":
|
138 |
uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8, timeout_keep_alive=60000)
|
139 |
+
|