m8chaa commited on
Commit
96e55ac
·
verified ·
1 Parent(s): 4c8c653

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -37
app.py CHANGED
@@ -891,48 +891,43 @@ async def sync_data(data_json: str = Form(...), user_id: str = Form(...)):
891
 
892
 
893
 
894
- observers = {}
895
-
896
- class WatchdogHandler(FileSystemEventHandler):
897
- def __init__(self, folder_id):
898
- self.folder_id = folder_id
899
-
900
- def on_modified(self, event):
901
- logging.info(f"Modification detected: {event.src_path}")
902
-
903
- def on_created(self, event):
904
- logging.info(f"Creation detected: {event.src_path}")
905
 
906
- def on_deleted(self, event):
907
- logging.info(f"Deletion detected: {event.src_path}")
908
 
909
- def on_moved(self, event):
910
- logging.info(f"Move detected: from {event.src_path} to {event.dest_path}")
911
 
912
- def watch_folder(folder_path, folder_id):
913
- event_handler = WatchdogHandler(folder_id)
914
- observer = Observer()
915
- observer.schedule(event_handler, path=folder_path, recursive=True)
916
- observer.start()
917
- observers[folder_id] = observer
 
 
 
918
 
919
  @app.post("/watch/{folder_id}")
920
  async def watch_folder_endpoint(folder_id: str):
921
- # Construct a folder path from the folder_id
922
- folder_path = f"./{folder_id}"
923
-
924
- # Check if the folder exists
925
- if not os.path.exists(folder_path):
926
- raise HTTPException(status_code=404, detail="Folder not found")
927
-
928
- # Start watching the folder in a separate thread
929
- thread = Thread(target=watch_folder, args=(folder_path, folder_id))
930
- thread.start()
931
 
932
- return {"message": f"Started watching folder: {folder_id}"}
 
 
 
 
 
933
 
934
- @app.on_event("shutdown")
935
- def shutdown_event():
936
- for observer in observers.values():
937
- observer.stop()
938
- observer.join()
 
 
 
 
 
891
 
892
 
893
 
894
+ from fastapi import FastAPI, HTTPException, Request
895
+ from google.oauth2.credentials import Credentials
896
+ from google_auth_oauthlib.flow import InstalledAppFlow
897
+ from google.auth.transport.requests import Request as GoogleRequest
898
+ from googleapiclient.discovery import build
899
+ import os
900
+ import json
 
 
 
 
901
 
 
 
902
 
 
 
903
 
904
+ # Function to watch a Google Drive folder
905
+ def create_channel(service, folder_id):
906
+ body = {
907
+ 'id': 'unique-channel-id',
908
+ 'type': 'web_hook',
909
+ 'address': 'https://m8chaa-re-sheet-develop.hf.space/notifications', # Replace with your webhook URL
910
+ }
911
+ response = service.files().watch(fileId=folder_id, body=body).execute()
912
+ return response
913
 
914
  @app.post("/watch/{folder_id}")
915
  async def watch_folder_endpoint(folder_id: str):
916
+ try:
 
 
 
 
 
 
 
 
 
917
 
918
+ # Create a channel to watch the folder
919
+ response = create_channel(drive_service, folder_id)
920
+
921
+ return {"message": "Started watching folder", "channel": response}
922
+ except Exception as e:
923
+ raise HTTPException(status_code=500, detail=str(e))
924
 
925
+ @app.post("/notifications")
926
+ async def notifications(request: Request):
927
+ # This endpoint will receive notifications from Google Drive
928
+ try:
929
+ body = await request.body()
930
+ print("Notification received:", body)
931
+ return {"message": "Notification received"}
932
+ except Exception as e:
933
+ raise HTTPException(status_code=500, detail=str(e))