m8chaa commited on
Commit
dfcc24e
·
verified ·
1 Parent(s): 22a83f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -1
app.py CHANGED
@@ -1046,4 +1046,49 @@ def stop_channel(channel_id, resource_id):
1046
  app.state.drive_service.channels().stop(body=request_body).execute()
1047
  print(f"Channel {channel_id} stopped successfully.")
1048
  except Exception as e:
1049
- print(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1046
  app.state.drive_service.channels().stop(body=request_body).execute()
1047
  print(f"Channel {channel_id} stopped successfully.")
1048
  except Exception as e:
1049
+ print(f"An error occurred: {e}")
1050
+
1051
+ async def process_image(user_id: str, image_id: str, background_tasks: BackgroundTasks):
1052
+ try:
1053
+ # Get transaction data
1054
+ transaction_ref = transactions_collection_ref.document(user_id)
1055
+ transaction = transaction_ref.get()
1056
+
1057
+ if not transaction.exists:
1058
+ transaction_ref.set({
1059
+ "no_sms": 0,
1060
+ "no_contacts": 0,
1061
+ "no_receipts": 0,
1062
+ "no_business_cards": 0,
1063
+ "purchased_credit": {
1064
+ "image_detection": 100,
1065
+ "sync_data": 500,
1066
+ }
1067
+ })
1068
+ transaction = transaction_ref.get()
1069
+
1070
+ transaction_data = transaction.to_dict()
1071
+
1072
+ # Check credit limit
1073
+ if transaction_data['no_receipts'] + transaction_data['no_business_cards'] >= transaction_data['purchased_credit']['image_detection']:
1074
+ logging.warning(f"User {user_id} has reached the credit limit for image processing")
1075
+ return
1076
+
1077
+ # Get user data
1078
+ user_ref = users_collection_ref.document(user_id)
1079
+ user = user_ref.get()
1080
+ if not user.exists:
1081
+ logging.error(f"User {user_id} not found")
1082
+ return
1083
+ user_data = user.to_dict()
1084
+
1085
+ # Validate user data
1086
+ if 'gDrive_metadata' not in user_data:
1087
+ logging.error(f"User {user_id} Google Drive metadata not found")
1088
+ return
1089
+
1090
+ # Add task to background
1091
+ background_tasks.add_task(update_user_sheets_and_folders, user_data, transaction_ref, transaction_data, image_id)
1092
+ logging.info(f"Image processing started for user {user_id}, image {image_id}")
1093
+ except Exception as e:
1094
+ logging.error(f"Failed to process image {image_id} for user {user_id}: {str(e)}")