Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -891,48 +891,43 @@ async def sync_data(data_json: str = Form(...), user_id: str = Form(...)):
|
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
| 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 |
-
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
|
|
|
|
|
|
|
|
|
| 918 |
|
| 919 |
@app.post("/watch/{folder_id}")
|
| 920 |
async def watch_folder_endpoint(folder_id: str):
|
| 921 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 933 |
|
| 934 |
-
@app.
|
| 935 |
-
def
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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))
|