m8chaa commited on
Commit
56cfc1a
·
verified ·
1 Parent(s): b797528

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -76
app.py CHANGED
@@ -901,84 +901,84 @@ async def sync_data(data_json: str = Form(...), user_id: str = Form(...)):
901
 
902
 
903
 
904
- # # Global variables
905
- # gDrive_watch_list = {
906
- # "7A5bxxijved7Du4G7xXo1916hIU2": "1ZfvMPZ_R8j4g0JCVU4dWFerVOlFWG6w1"
907
- # }
908
- # watch_list_channels = {}
909
-
910
- # def create_channel(service, folder_id):
911
- # body = {
912
- # 'id': f'channel-{folder_id}-{int(time.time())}',
913
- # 'type': 'web_hook',
914
- # 'address': 'https://m8chaa-re-sheet-develop.hf.space/notifications',
915
- # }
916
- # response = service.files().watch(fileId=folder_id, body=body).execute()
917
- # return response
918
-
919
- # def close_channel(channel_id, resource_id):
920
- # request_body = {
921
- # 'id': channel_id,
922
- # 'resourceId': resource_id,
923
- # }
924
- # drive_service.channels().stop(body=request_body).execute()
925
-
926
- # def update_watch_list():
927
- # global gDrive_watch_list, watch_list_channels
928
-
929
- # # Close all existing channels
930
- # for channel_id, channel_info in watch_list_channels.items():
931
- # close_channel(channel_id, channel_info['resourceId'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
932
 
933
- # watch_list_channels.clear()
934
-
935
- # # Create new channels for all folders in the watch list
936
- # for user_id, folder_id in gDrive_watch_list.items():
937
- # try:
938
- # response = create_channel(drive_service, folder_id)
939
- # logging.info("create channel: " + response)
940
- # watch_list_channels[response['id']] = {
941
- # 'resourceId': response['resourceId'],
942
- # 'user_id': user_id,
943
- # 'folder_id': folder_id
944
- # }
945
- # logging.info("watch list: " + watch_list_channels)
946
- # except Exception as e:
947
- # print(f"Error creating channel for user {user_id}: {str(e)}")
948
-
949
- # def on_snapshot(doc_snapshot, changes, read_time):
950
- # global gDrive_watch_list, watch_list_channels
951
-
952
- # for change in changes:
953
- # user_id = change.document.id
954
- # user_data = change.document.to_dict()
955
-
956
- # if change.type.name == 'ADDED' or change.type.name == 'MODIFIED':
957
- # if 'gDrive_metadata' in user_data and 'uploaded' in user_data['gDrive_metadata']:
958
- # folder_id = user_data['gDrive_metadata']['uploaded']
959
- # gDrive_watch_list[user_id] = folder_id
960
-
961
- # # Create a new channel for this folder
962
- # try:
963
- # response = create_channel(drive_service, folder_id)
964
- # watch_list_channels[response['id']] = {
965
- # 'resourceId': response['resourceId'],
966
- # 'user_id': user_id,
967
- # 'folder_id': folder_id
968
- # }
969
- # except Exception as e:
970
- # print(f"Error creating channel for user {user_id}: {str(e)}")
971
-
972
- # elif change.type.name == 'REMOVED':
973
- # if user_id in gDrive_watch_list:
974
- # folder_id = gDrive_watch_list.pop(user_id)
975
 
976
- # # Find and close the channel for this user
977
- # for channel_id, channel_info in list(watch_list_channels.items()):
978
- # if channel_info['user_id'] == user_id:
979
- # close_channel(channel_id, channel_info['resourceId'])
980
- # del watch_list_channels[channel_id]
981
- # break
982
 
983
  # @app.post("/notifications")
984
  # async def notifications(request: Request):
 
901
 
902
 
903
 
904
+ # Global variables
905
+ gDrive_watch_list = {
906
+ "7A5bxxijved7Du4G7xXo1916hIU2": "1ZfvMPZ_R8j4g0JCVU4dWFerVOlFWG6w1"
907
+ }
908
+ watch_list_channels = {}
909
+
910
+ def create_channel(service, folder_id):
911
+ body = {
912
+ 'id': f'channel-{folder_id}-{int(time.time())}',
913
+ 'type': 'web_hook',
914
+ 'address': 'https://m8chaa-re-sheet-develop.hf.space/notifications',
915
+ }
916
+ response = service.files().watch(fileId=folder_id, body=body).execute()
917
+ return response
918
+
919
+ def close_channel(channel_id, resource_id):
920
+ request_body = {
921
+ 'id': channel_id,
922
+ 'resourceId': resource_id,
923
+ }
924
+ drive_service.channels().stop(body=request_body).execute()
925
+
926
+ def update_watch_list():
927
+ global gDrive_watch_list, watch_list_channels
928
+
929
+ # Close all existing channels
930
+ for channel_id, channel_info in watch_list_channels.items():
931
+ close_channel(channel_id, channel_info['resourceId'])
932
+
933
+ watch_list_channels.clear()
934
+
935
+ # Create new channels for all folders in the watch list
936
+ for user_id, folder_id in gDrive_watch_list.items():
937
+ try:
938
+ response = create_channel(drive_service, folder_id)
939
+ logging.info("create channel: " + response)
940
+ watch_list_channels[response['id']] = {
941
+ 'resourceId': response['resourceId'],
942
+ 'user_id': user_id,
943
+ 'folder_id': folder_id
944
+ }
945
+ logging.info("watch list: " + watch_list_channels)
946
+ except Exception as e:
947
+ print(f"Error creating channel for user {user_id}: {str(e)}")
948
+
949
+ def on_snapshot(doc_snapshot, changes, read_time):
950
+ global gDrive_watch_list, watch_list_channels
951
+
952
+ for change in changes:
953
+ user_id = change.document.id
954
+ user_data = change.document.to_dict()
955
+
956
+ if change.type.name == 'ADDED' or change.type.name == 'MODIFIED':
957
+ if 'gDrive_metadata' in user_data and 'uploaded' in user_data['gDrive_metadata']:
958
+ folder_id = user_data['gDrive_metadata']['uploaded']
959
+ gDrive_watch_list[user_id] = folder_id
960
+
961
+ # Create a new channel for this folder
962
+ try:
963
+ response = create_channel(drive_service, folder_id)
964
+ watch_list_channels[response['id']] = {
965
+ 'resourceId': response['resourceId'],
966
+ 'user_id': user_id,
967
+ 'folder_id': folder_id
968
+ }
969
+ except Exception as e:
970
+ print(f"Error creating channel for user {user_id}: {str(e)}")
971
 
972
+ elif change.type.name == 'REMOVED':
973
+ if user_id in gDrive_watch_list:
974
+ folder_id = gDrive_watch_list.pop(user_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
975
 
976
+ # Find and close the channel for this user
977
+ for channel_id, channel_info in list(watch_list_channels.items()):
978
+ if channel_info['user_id'] == user_id:
979
+ close_channel(channel_id, channel_info['resourceId'])
980
+ del watch_list_channels[channel_id]
981
+ break
982
 
983
  # @app.post("/notifications")
984
  # async def notifications(request: Request):