Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -995,91 +995,37 @@ async def shutdown_event():
|
|
| 995 |
@app.post("/notifications")
|
| 996 |
async def notifications(request: Request, background_tasks: BackgroundTasks):
|
| 997 |
try:
|
| 998 |
-
# Extract relevant headers
|
| 999 |
channel_id = request.headers.get('X-Goog-Channel-ID')
|
| 1000 |
resource_id = request.headers.get('X-Goog-Resource-ID')
|
| 1001 |
resource_state = request.headers.get('X-Goog-Resource-State')
|
| 1002 |
resource_uri = request.headers.get('X-Goog-Resource-URI')
|
| 1003 |
changed = request.headers.get('X-Goog-Changed')
|
| 1004 |
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
logging.info(f"Channel ID: {channel_id}")
|
| 1008 |
-
logging.info(f"Resource ID: {resource_id}")
|
| 1009 |
-
logging.info(f"Resource State: {resource_state}")
|
| 1010 |
-
logging.info(f"Resource URI: {resource_uri}")
|
| 1011 |
-
logging.info(f"Changed: {changed}")
|
| 1012 |
-
|
| 1013 |
-
# Determine the user associated with this notification
|
| 1014 |
-
user_id = None
|
| 1015 |
-
folder_id = None
|
| 1016 |
if channel_id in app.state.drive_watcher.channels:
|
| 1017 |
channel_info = app.state.drive_watcher.channels[channel_id]
|
| 1018 |
user_id = channel_info['user_id']
|
| 1019 |
folder_id = channel_info['folder_id']
|
| 1020 |
|
| 1021 |
-
if user_id and folder_id:
|
| 1022 |
logging.info(f"Notification is for user: {user_id}, folder: {folder_id}")
|
| 1023 |
|
| 1024 |
-
if resource_state in ['
|
| 1025 |
-
logging.info(f"Resource state: {resource_state}")
|
| 1026 |
-
|
| 1027 |
-
# Get the file metadata
|
| 1028 |
try:
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
transaction = transaction_ref.get()
|
| 1042 |
-
|
| 1043 |
-
if not transaction.exists:
|
| 1044 |
-
transaction_ref.set({
|
| 1045 |
-
"no_sms": 0,
|
| 1046 |
-
"no_contacts": 0,
|
| 1047 |
-
"no_receipts": 0,
|
| 1048 |
-
"no_business_cards": 0,
|
| 1049 |
-
"purchased_credit": {
|
| 1050 |
-
"image_detection": 100,
|
| 1051 |
-
"sync_data": 500,
|
| 1052 |
-
}
|
| 1053 |
-
})
|
| 1054 |
-
transaction = transaction_ref.get()
|
| 1055 |
-
|
| 1056 |
-
transaction_data = transaction.to_dict()
|
| 1057 |
-
|
| 1058 |
-
# Check credit limit
|
| 1059 |
-
if transaction_data['no_receipts'] + transaction_data['no_business_cards'] >= transaction_data['purchased_credit']['image_detection']:
|
| 1060 |
-
logging.warning(f"User {user_id} has reached the credit limit for image processing")
|
| 1061 |
-
return {"message": "Credit limit reached for image processing"}
|
| 1062 |
-
|
| 1063 |
-
# Get user data
|
| 1064 |
-
user_ref = users_collection_ref.document(user_id)
|
| 1065 |
-
user = user_ref.get()
|
| 1066 |
-
if not user.exists:
|
| 1067 |
-
logging.error(f"User {user_id} not found")
|
| 1068 |
-
return {"message": "User not found"}
|
| 1069 |
-
user_data = user.to_dict()
|
| 1070 |
-
|
| 1071 |
-
# Validate user data
|
| 1072 |
-
if 'gDrive_metadata' not in user_data:
|
| 1073 |
-
logging.error(f"User {user_id} Google Drive metadata not found")
|
| 1074 |
-
return {"message": "User Google Drive metadata not found"}
|
| 1075 |
-
|
| 1076 |
-
# Add task to background
|
| 1077 |
-
background_tasks.add_task(update_user_sheets_and_folders, user_data, transaction_ref, transaction_data, resource_id)
|
| 1078 |
-
logging.info(f"Image processing started for user {user_id}, image {resource_id}")
|
| 1079 |
-
except Exception as e:
|
| 1080 |
-
logging.error(f"Failed to process image {resource_id} for user {user_id}: {str(e)}")
|
| 1081 |
except Exception as e:
|
| 1082 |
-
logging.error(f"
|
| 1083 |
|
| 1084 |
else:
|
| 1085 |
logging.warning(f"Received notification for unknown channel: {channel_id}")
|
|
|
|
| 995 |
@app.post("/notifications")
|
| 996 |
async def notifications(request: Request, background_tasks: BackgroundTasks):
|
| 997 |
try:
|
|
|
|
| 998 |
channel_id = request.headers.get('X-Goog-Channel-ID')
|
| 999 |
resource_id = request.headers.get('X-Goog-Resource-ID')
|
| 1000 |
resource_state = request.headers.get('X-Goog-Resource-State')
|
| 1001 |
resource_uri = request.headers.get('X-Goog-Resource-URI')
|
| 1002 |
changed = request.headers.get('X-Goog-Changed')
|
| 1003 |
|
| 1004 |
+
logging.info(f"Notification received: Channel ID: {channel_id}, Resource ID: {resource_id}, State: {resource_state}, URI: {resource_uri}, Changed: {changed}")
|
| 1005 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1006 |
if channel_id in app.state.drive_watcher.channels:
|
| 1007 |
channel_info = app.state.drive_watcher.channels[channel_id]
|
| 1008 |
user_id = channel_info['user_id']
|
| 1009 |
folder_id = channel_info['folder_id']
|
| 1010 |
|
|
|
|
| 1011 |
logging.info(f"Notification is for user: {user_id}, folder: {folder_id}")
|
| 1012 |
|
| 1013 |
+
if resource_state in ['changes']:
|
|
|
|
|
|
|
|
|
|
| 1014 |
try:
|
| 1015 |
+
# List files in the folder that have been recently modified
|
| 1016 |
+
results = app.state.drive_service.files().list(
|
| 1017 |
+
q=f"'{folder_id}' in parents and mimeType contains 'image/' and modifiedTime > '-1d'",
|
| 1018 |
+
fields="files(id, mimeType)"
|
| 1019 |
+
).execute()
|
| 1020 |
+
files = results.get('files', [])
|
| 1021 |
+
|
| 1022 |
+
for file in files:
|
| 1023 |
+
if file['mimeType'].startswith('image/'):
|
| 1024 |
+
logging.info(f"Processing new image: {file['id']}")
|
| 1025 |
+
await process_image(user_id, file['id'], background_tasks)
|
| 1026 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1027 |
except Exception as e:
|
| 1028 |
+
logging.error(f"Error processing folder changes: {str(e)}")
|
| 1029 |
|
| 1030 |
else:
|
| 1031 |
logging.warning(f"Received notification for unknown channel: {channel_id}")
|