Akane710 commited on
Commit
0db219f
·
verified ·
1 Parent(s): 6220d49

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +32 -116
main.py CHANGED
@@ -1,128 +1,44 @@
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 # Import StarRailCard module
11
- import enkanetwork
12
- import uvicorn
13
- from fastapi.staticfiles import StaticFiles
14
 
15
  app = FastAPI()
16
 
17
- # Directory where images will be saved
18
- IMAGE_SAVE_PATH = "static/cards"
19
-
20
- # Ensure the directory exists
21
- os.makedirs(IMAGE_SAVE_PATH, exist_ok=True)
22
-
23
- # Mount the static files directory to serve saved images
24
- app.mount("/cards", StaticFiles(directory=IMAGE_SAVE_PATH), name="cards")
25
-
26
- # Genshin Impact card creation
27
- async def genshin_card(id, designtype):
28
- async with encbanner.ENC(uid=str(id)) as encard:
29
- return await encard.creat(akasha=True, template=(2 if str(designtype) == "2" else 1))
30
-
31
- # Star Rail card creation
32
- async def starrail_card(id, designtype):
33
- async with starrailcard.Card(seeleland=True, remove_logo=True, enka=True) as card:
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, enka=True) as card:
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"):
90
  try:
91
- result = await genshin_card(id, design)
92
- characters = process_images(result, id)
93
- return JSONResponse(content={'response': characters})
94
-
95
- except enkanetwork.exception.VaildateUIDError:
96
- return JSONResponse(content={'error': 'Invalid UID. Please check your UID.'}, status_code=400)
97
-
98
- except enkacard.enc_error.ENCardError:
99
- return JSONResponse(content={'error': 'Enable display of the showcase in the game or add characters there.'}, status_code=400)
 
 
100
 
101
  except Exception as e:
102
- return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
103
 
104
- # Route for Star Rail
105
- @app.get("/starrail/{id}")
106
- async def starrail_characters(id: int, design: str = "1"):
107
- try:
108
- result = await starrail_card(id, design)
109
- characters = process_images(result, id)
110
- return JSONResponse(content={'response': characters})
111
 
112
- except Exception as e:
113
- return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
114
-
115
- # Route for Star Rail profile
116
- @app.get("/starrail/profile/{id}")
117
- async def starrail_profile_route(id: int):
118
- try:
119
- result = await starrail_profile(id)
120
- profile_data = process_profile(result, id)
121
- return JSONResponse(content={'response': profile_data})
122
-
123
- except Exception as e:
124
- return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
125
 
126
- # Start the FastAPI server
127
  if __name__ == "__main__":
128
- uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8, timeout_keep_alive=60000)
 
 
1
  import os
2
+ import cloudinary
3
+ import cloudinary.uploader
4
+ from fastapi import FastAPI, Query
 
 
 
5
  from fastapi.responses import JSONResponse
6
+ from cloudinary.utils import cloudinary_url
 
 
 
 
7
 
8
  app = FastAPI()
9
 
10
+ # Cloudinary configuration
11
+ cloudinary.config(
12
+ cloud_name="dob6buqxd", # Replace with your Cloudinary cloud name
13
+ api_key="397945971979515", # Replace with your Cloudinary API key
14
+ api_secret="nRMIOSr10xiLo443LLMNDxJBB-Q", # Replace with your Cloudinary API secret
15
+ secure=True
16
+ )
17
+
18
+ # Image compression route
19
+ @app.get("/compress")
20
+ async def compress_image(image_url: str = Query(..., description="URL of the image to be compressed")):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  try:
22
+ # Fetch the compressed image from Cloudinary using the provided image URL
23
+ compressed_image_url, options = cloudinary_url(
24
+ image_url,
25
+ transformation=[
26
+ {'quality': "auto"}, # Set quality to 'auto'
27
+ {'fetch_format': "auto"} # Set format to 'auto'
28
+ ]
29
+ )
30
+
31
+ # Return the compressed image URL
32
+ return JSONResponse(content={'compressed_image_url': compressed_image_url})
33
 
34
  except Exception as e:
35
+ return JSONResponse(content={'error': 'Error compressing image: ' + str(e)}, status_code=500)
36
 
 
 
 
 
 
 
 
37
 
38
+ @app.get("/")
39
+ def root():
40
+ return "خدتك عليه"
 
 
 
 
 
 
 
 
 
 
41
 
 
42
  if __name__ == "__main__":
43
+ import uvicorn
44
+ uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8)