Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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).
|
| 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,
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
|
| 953 |
-
if
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
|
| 958 |
-
|
| 959 |
-
|
| 960 |
-
|
| 961 |
-
|
| 962 |
-
|
| 963 |
-
|
| 964 |
-
|
| 965 |
-
|
| 966 |
-
|
| 967 |
-
|
| 968 |
-
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
| 972 |
-
|
| 973 |
-
|
|
|
|
| 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()
|