Akane710 commited on
Commit
7af2f44
·
verified ·
1 Parent(s): 92764d6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +39 -12
main.py CHANGED
@@ -160,7 +160,7 @@ async def update():
160
  content={'error': f'UNKNOWN ERR: {str(e)}', 'details': error_details},
161
  status_code=500
162
  )
163
-
164
  # Helper function to upload the image to Cloudinary
165
  def upload_image(data, character_id, player_id):
166
  try:
@@ -210,6 +210,24 @@ def process_profile(profile_card):
210
  "url": image_url,
211
  }
212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  # Process all the images returned
214
  def process_images(result, player_id):
215
  characters = []
@@ -222,16 +240,26 @@ def process_images(result, player_id):
222
  print(f"Error processing image: {e}")
223
  return characters
224
 
 
 
 
 
 
 
 
 
 
 
 
225
  # ZZZ Card and Profile creation functions
226
  async def zenless_card(uid, character_id=None, character_art=None):
227
  from zenka import zenka, ZZZError, CacheConfig, Lang
228
-
229
  config = zenka.Config(
230
- save=True, # Changed to match your example
231
  asset_save=True,
232
  hide_uid=False,
233
  )
234
-
235
  try:
236
  # Simplified to match your example exactly
237
  async with zenka.Client(
@@ -252,13 +280,12 @@ async def zenless_card(uid, character_id=None, character_art=None):
252
 
253
  async def zenless_profile(uid):
254
  from zenka import zenka, ZZZError, CacheConfig, Lang
255
-
256
  config = zenka.Config(
257
- save=True, # Changed to match your example
258
  asset_save=True,
259
  hide_uid=False,
260
  )
261
-
262
  try:
263
  # Simplified to match your example exactly
264
  async with zenka.Client(
@@ -284,16 +311,16 @@ async def zenless_characters(uid: int, character_id: str = None, character_art_u
284
  char_id = None
285
  if character_id:
286
  char_id = [int(id) if id.isdigit() else id for id in character_id.split(',')]
287
-
288
  # Process character_art from URL to dict if provided
289
  char_art = None
290
  if character_id and character_art_url:
291
  # Create a dictionary mapping character IDs to art URLs
292
  char_ids = char_id if char_id else []
293
  char_art = {str(char_ids[0]): character_art_url} if char_ids else None
294
-
295
  result = await zenless_card(uid, char_id, char_art)
296
- characters = process_images(result, uid)
297
  return JSONResponse(content={'response': characters})
298
 
299
  except Exception as e:
@@ -303,11 +330,11 @@ async def zenless_characters(uid: int, character_id: str = None, character_art_u
303
  async def zenless_profile_route(uid: int):
304
  try:
305
  result = await zenless_profile(uid)
306
- profile_data = process_profile(result)
307
  return JSONResponse(content={'response': profile_data})
308
 
309
  except Exception as e:
310
  return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
311
-
312
  if __name__ == "__main__":
313
  uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8, timeout_keep_alive=60000)
 
160
  content={'error': f'UNKNOWN ERR: {str(e)}', 'details': error_details},
161
  status_code=500
162
  )
163
+
164
  # Helper function to upload the image to Cloudinary
165
  def upload_image(data, character_id, player_id):
166
  try:
 
210
  "url": image_url,
211
  }
212
 
213
+ from io import BytesIO
214
+
215
+ def process_profilee(profile_card):
216
+ # Ensure profile_card contains an image
217
+ if not profile_card.cards or not isinstance(profile_card.cards, list):
218
+ raise ValueError("Invalid profile_card: 'cards' is missing or not a list.")
219
+
220
+ profile_image = profile_card.cards[0].card # Extract the PIL image from the first card
221
+
222
+ with BytesIO() as byte_io:
223
+ profile_image.save(byte_io, "PNG") # Save the image as PNG
224
+ byte_io.seek(0)
225
+ image_url = upload_imagee(byte_io) # Upload the image
226
+ return {
227
+ "url": image_url,
228
+ }
229
+
230
+
231
  # Process all the images returned
232
  def process_images(result, player_id):
233
  characters = []
 
240
  print(f"Error processing image: {e}")
241
  return characters
242
 
243
+ def process_imagess(result, player_id):
244
+ characters = []
245
+ with concurrent.futures.ThreadPoolExecutor() as executor:
246
+ futures = [executor.submit(process_image, dt, player_id) for dt in result.cards]
247
+ for future in concurrent.futures.as_completed(futures):
248
+ try:
249
+ characters.append(future.result())
250
+ except Exception as e:
251
+ print(f"Error processing image: {e}")
252
+ return characters
253
+
254
  # ZZZ Card and Profile creation functions
255
  async def zenless_card(uid, character_id=None, character_art=None):
256
  from zenka import zenka, ZZZError, CacheConfig, Lang
257
+
258
  config = zenka.Config(
 
259
  asset_save=True,
260
  hide_uid=False,
261
  )
262
+
263
  try:
264
  # Simplified to match your example exactly
265
  async with zenka.Client(
 
280
 
281
  async def zenless_profile(uid):
282
  from zenka import zenka, ZZZError, CacheConfig, Lang
283
+
284
  config = zenka.Config(
 
285
  asset_save=True,
286
  hide_uid=False,
287
  )
288
+
289
  try:
290
  # Simplified to match your example exactly
291
  async with zenka.Client(
 
311
  char_id = None
312
  if character_id:
313
  char_id = [int(id) if id.isdigit() else id for id in character_id.split(',')]
314
+
315
  # Process character_art from URL to dict if provided
316
  char_art = None
317
  if character_id and character_art_url:
318
  # Create a dictionary mapping character IDs to art URLs
319
  char_ids = char_id if char_id else []
320
  char_art = {str(char_ids[0]): character_art_url} if char_ids else None
321
+
322
  result = await zenless_card(uid, char_id, char_art)
323
+ characters = process_imagess(result, uid)
324
  return JSONResponse(content={'response': characters})
325
 
326
  except Exception as e:
 
330
  async def zenless_profile_route(uid: int):
331
  try:
332
  result = await zenless_profile(uid)
333
+ profile_data = process_profilee(result)
334
  return JSONResponse(content={'response': profile_data})
335
 
336
  except Exception as e:
337
  return JSONResponse(content={'error': 'UNKNOWN ERR: ' + str(e)}, status_code=500)
338
+
339
  if __name__ == "__main__":
340
  uvicorn.run("main:app", host="0.0.0.0", port=7860, workers=8, timeout_keep_alive=60000)