m8chaa commited on
Commit
f9763b4
·
verified ·
1 Parent(s): cbd4533

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -918,7 +918,7 @@ class DriveWatcher:
918
  'type': 'web_hook',
919
  'address': 'https://m8chaa-re-sheet-develop.hf.space/notifications',
920
  }
921
- response = await app.state.drive_service.files().watch(fileId=folder_id, body=body).execute()
922
  return response
923
 
924
  async def close_channel(self, channel_id: str, resource_id: str):
@@ -930,9 +930,9 @@ class DriveWatcher:
930
  async def update_watch_list(self):
931
  for channel_id, channel_info in self.channels.items():
932
  await self.close_channel(channel_id, channel_info['resourceId'])
933
-
934
  self.channels.clear()
935
-
936
  for user_id, folder_id in self.watch_list.items():
937
  try:
938
  response = await self.create_channel(folder_id)
@@ -945,32 +945,33 @@ class DriveWatcher:
945
  except Exception as e:
946
  logging.error(f"Error creating channel for user {user_id}: {str(e)}")
947
 
948
- async def on_user_change(self, change):
949
- user_id = change.document.id
950
- user_data = change.document.to_dict()
951
-
952
- if change.type.name in ['ADDED', 'MODIFIED']:
953
- if 'gDrive_metadata' in user_data and 'uploaded' in user_data['gDrive_metadata']:
954
- folder_id = user_data['gDrive_metadata']['uploaded']
955
- self.watch_list[user_id] = folder_id
956
- try:
957
- response = await self.create_channel(folder_id)
958
- self.channels[response['id']] = {
959
- 'resourceId': response['resourceId'],
960
- 'user_id': user_id,
961
- 'folder_id': folder_id
962
- }
963
- except Exception as e:
964
- logging.error(f"Error creating channel for user {user_id}: {str(e)}")
965
-
966
- elif change.type.name == 'REMOVED':
967
- if user_id in self.watch_list:
968
- folder_id = self.watch_list.pop(user_id)
969
- for channel_id, channel_info in list(self.channels.items()):
970
- if channel_info['user_id'] == user_id:
971
- await self.close_channel(channel_id, channel_info['resourceId'])
972
- del self.channels[channel_id]
973
- break
 
974
  @app.on_event("startup")
975
  async def startup_event():
976
  app.state.drive_watcher = DriveWatcher()
 
918
  'type': 'web_hook',
919
  'address': 'https://m8chaa-re-sheet-develop.hf.space/notifications',
920
  }
921
+ response = await app.state.drive_service.files().watch(fileId=folder_id, body=body).__await__()
922
  return response
923
 
924
  async def close_channel(self, channel_id: str, resource_id: str):
 
930
  async def update_watch_list(self):
931
  for channel_id, channel_info in self.channels.items():
932
  await self.close_channel(channel_id, channel_info['resourceId'])
933
+
934
  self.channels.clear()
935
+
936
  for user_id, folder_id in self.watch_list.items():
937
  try:
938
  response = await self.create_channel(folder_id)
 
945
  except Exception as e:
946
  logging.error(f"Error creating channel for user {user_id}: {str(e)}")
947
 
948
+ async def async def on_user_change(self, snapshot, changes, read_time):
949
+ for change in changes:
950
+ user_id = change.document.id
951
+ user_data = change.document.to_dict()
952
+
953
+ if change.type.name in ['ADDED', 'MODIFIED']:
954
+ if 'gDrive_metadata' in user_data and 'uploaded' in user_data['gDrive_metadata']:
955
+ folder_id = user_data['gDrive_metadata']['uploaded']
956
+ self.watch_list[user_id] = folder_id
957
+ try:
958
+ response = await self.create_channel(folder_id)
959
+ self.channels[response['id']] = {
960
+ 'resourceId': response['resourceId'],
961
+ 'user_id': user_id,
962
+ 'folder_id': folder_id
963
+ }
964
+ except Exception as e:
965
+ logging.error(f"Error creating channel for user {user_id}: {str(e)}")
966
+
967
+ elif change.type.name == 'REMOVED':
968
+ if user_id in self.watch_list:
969
+ folder_id = self.watch_list.pop(user_id)
970
+ for channel_id, channel_info in list(self.channels.items()):
971
+ if channel_info['user_id'] == user_id:
972
+ await self.close_channel(channel_id, channel_info['resourceId'])
973
+ del self.channels[channel_id]
974
+ break
975
  @app.on_event("startup")
976
  async def startup_event():
977
  app.state.drive_watcher = DriveWatcher()