m8chaa commited on
Commit
7656ff5
·
verified ·
1 Parent(s): 999e223

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -92
app.py CHANGED
@@ -30,6 +30,11 @@ from google.cloud.firestore import AsyncClient
30
  import asyncio
31
 
32
 
 
 
 
 
 
33
  # start application
34
  app = FastAPI()
35
 
@@ -904,127 +909,104 @@ async def sync_data(data_json: str = Form(...), user_id: str = Form(...)):
904
 
905
 
906
 
907
- # Global variables
908
- gDrive_watch_list = {
909
- "7A5bxxijved7Du4G7xXo1916hIU2": "1ZfvMPZ_R8j4g0JCVU4dWFerVOlFWG6w1"
910
- }
911
- watch_list_channels = {}
912
 
913
- def create_channel(service, folder_id):
914
- body = {
915
- 'id': f'channel-{folder_id}-{int(time.time())}',
916
- 'type': 'web_hook',
917
- 'address': 'https://m8chaa-re-sheet-develop.hf.space/notifications',
918
- }
919
- response = service.files().watch(fileId=folder_id, body=body).execute()
920
- return response
921
-
922
- def close_channel(channel_id, resource_id):
923
- request_body = {
924
- 'id': channel_id,
925
- 'resourceId': resource_id,
926
- }
927
- drive_service.channels().stop(body=request_body).execute()
928
-
929
- def update_watch_list():
930
- global gDrive_watch_list, watch_list_channels
931
-
932
- # Close all existing channels
933
- for channel_id, channel_info in watch_list_channels.items():
934
- close_channel(channel_id, channel_info['resourceId'])
935
-
936
- watch_list_channels.clear()
937
-
938
- # Create new channels for all folders in the watch list
939
- for user_id, folder_id in gDrive_watch_list.items():
940
- try:
941
- response = create_channel(drive_service, folder_id)
942
- logging.info("create channel: " + response)
943
- watch_list_channels[response['id']] = {
944
- 'resourceId': response['resourceId'],
945
- 'user_id': user_id,
946
- 'folder_id': folder_id
947
- }
948
- logging.info("watch list: " + watch_list_channels)
949
- except Exception as e:
950
- print(f"Error creating channel for user {user_id}: {str(e)}")
951
 
952
- def on_snapshot(doc_snapshot, changes, read_time):
953
- global gDrive_watch_list, watch_list_channels
 
954
 
955
- for change in changes:
 
 
 
 
 
 
 
 
 
 
 
 
 
956
  user_id = change.document.id
957
  user_data = change.document.to_dict()
958
 
959
- if change.type.name == 'ADDED' or change.type.name == 'MODIFIED':
960
  if 'gDrive_metadata' in user_data and 'uploaded' in user_data['gDrive_metadata']:
961
  folder_id = user_data['gDrive_metadata']['uploaded']
962
- gDrive_watch_list[user_id] = folder_id
963
-
964
- # Create a new channel for this folder
965
  try:
966
- response = create_channel(drive_service, folder_id)
967
- watch_list_channels[response['id']] = {
968
  'resourceId': response['resourceId'],
969
  'user_id': user_id,
970
  'folder_id': folder_id
971
  }
972
  except Exception as e:
973
- print(f"Error creating channel for user {user_id}: {str(e)}")
974
 
975
  elif change.type.name == 'REMOVED':
976
- if user_id in gDrive_watch_list:
977
- folder_id = gDrive_watch_list.pop(user_id)
978
-
979
- # Find and close the channel for this user
980
- for channel_id, channel_info in list(watch_list_channels.items()):
981
  if channel_info['user_id'] == user_id:
982
- close_channel(channel_id, channel_info['resourceId'])
983
- del watch_list_channels[channel_id]
984
  break
985
 
986
- @app.post("/notifications")
987
- async def notifications(request: Request):
988
- try:
989
- body = await request.body()
990
- print("Notification received:", body)
991
- return {"message": "Notification received"}
992
- except Exception as e:
993
- raise HTTPException(status_code=500, detail=str(e))
994
-
995
-
996
-
997
-
998
- # Set up the Firebase listener
999
- users_watch = users_collection_ref.on_snapshot(on_snapshot)
1000
-
1001
- # # Set up the scheduler to update watch list every 24 hours
1002
- # scheduler = BackgroundScheduler()
1003
- # scheduler.add_job(update_watch_list, 'interval', hours=24)
1004
- # scheduler.start()
1005
-
1006
- # Startup event to initialize the watch list
1007
  @app.on_event("startup")
1008
  async def startup_event():
1009
- global gDrive_watch_list
 
 
1010
  # Fetch all users and initialize the watch list
1011
- users = users_collection_ref.get()
 
1012
  for user in users:
1013
  user_data = user.to_dict()
1014
  if 'gDrive_metadata' in user_data and 'uploaded' in user_data['gDrive_metadata']:
1015
- gDrive_watch_list[user.id] = user_data['gDrive_metadata']['uploaded']
1016
 
1017
  # Initial update of watch list channels
1018
- update_watch_list()
 
 
 
1019
 
1020
- # Shutdown event to clean up resources
1021
  @app.on_event("shutdown")
1022
  async def shutdown_event():
1023
- global watch_list_channels
1024
  # Close all channels
1025
- for channel_id, channel_info in watch_list_channels.items():
1026
- close_channel(channel_id, channel_info['resourceId'])
1027
 
1028
- # Stop the Firebase listener and the scheduler
1029
- users_watch.unsubscribe()
1030
- scheduler.shutdown()
 
 
 
 
 
 
 
 
 
 
30
  import asyncio
31
 
32
 
33
+
34
+
35
+ from typing import Dict, Any
36
+
37
+
38
  # start application
39
  app = FastAPI()
40
 
 
909
 
910
 
911
 
912
+ class DriveWatcher:
913
+ def __init__(self):
914
+ self.watch_list: Dict[str, str] = {}
915
+ self.channels: Dict[str, Dict[str, Any]] = {}
 
916
 
917
+ async def create_channel(self, folder_id: str) -> Dict[str, Any]:
918
+ body = {
919
+ 'id': f'channel-{folder_id}-{int(time.time())}',
920
+ 'type': 'web_hook',
921
+ 'address': 'https://m8chaa-re-sheet-develop.hf.space/notifications',
922
+ }
923
+ response = await app.state.drive_service.files().watch(fileId=folder_id, body=body).execute()
924
+ return response
925
+
926
+ async def close_channel(self, channel_id: str, resource_id: str):
927
+ await app.state.drive_service.channels().stop(body={
928
+ 'id': channel_id,
929
+ 'resourceId': resource_id,
930
+ }).execute()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
931
 
932
+ async def update_watch_list(self):
933
+ for channel_id, channel_info in self.channels.items():
934
+ await self.close_channel(channel_id, channel_info['resourceId'])
935
 
936
+ self.channels.clear()
937
+
938
+ for user_id, folder_id in self.watch_list.items():
939
+ try:
940
+ response = await self.create_channel(folder_id)
941
+ self.channels[response['id']] = {
942
+ 'resourceId': response['resourceId'],
943
+ 'user_id': user_id,
944
+ 'folder_id': folder_id
945
+ }
946
+ except Exception as e:
947
+ logging.error(f"Error creating channel for user {user_id}: {str(e)}")
948
+
949
+ async def on_user_change(self, change):
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
976
  @app.on_event("startup")
977
  async def startup_event():
978
+ app.state.drive_watcher = DriveWatcher()
979
+ app.state.firestore_client = firestore.AsyncClient()
980
+
981
  # Fetch all users and initialize the watch list
982
+ users_ref = app.state.firestore_client.collection('users')
983
+ users = await users_ref.get()
984
  for user in users:
985
  user_data = user.to_dict()
986
  if 'gDrive_metadata' in user_data and 'uploaded' in user_data['gDrive_metadata']:
987
+ app.state.drive_watcher.watch_list[user.id] = user_data['gDrive_metadata']['uploaded']
988
 
989
  # Initial update of watch list channels
990
+ await app.state.drive_watcher.update_watch_list()
991
+
992
+ # Set up the Firebase listener
993
+ app.state.users_watch = users_ref.on_snapshot(app.state.drive_watcher.on_user_change)
994
 
 
995
  @app.on_event("shutdown")
996
  async def shutdown_event():
 
997
  # Close all channels
998
+ for channel_id, channel_info in app.state.drive_watcher.channels.items():
999
+ await app.state.drive_watcher.close_channel(channel_id, channel_info['resourceId'])
1000
 
1001
+ # Stop the Firebase listener
1002
+ app.state.users_watch.unsubscribe()
1003
+
1004
+ @app.post("/notifications")
1005
+ async def notifications(request: Request):
1006
+ try:
1007
+ body = await request.body()
1008
+ logging.info("Notification received: %s", body)
1009
+ return {"message": "Notification received"}
1010
+ except Exception as e:
1011
+ raise HTTPException(status_code=500, detail=str(e))
1012
+