Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,89 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
from google.oauth2.credentials import Credentials
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
if
|
| 44 |
-
creds
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
'
|
| 69 |
-
|
| 70 |
-
'
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
"
|
| 89 |
-
|
| 90 |
-
"message": f"Event created: {created_event.get('summary')}"
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
except Exception as e:
|
| 94 |
-
return {
|
| 95 |
-
"status": "error",
|
| 96 |
-
"error_message": str(e)
|
| 97 |
-
}
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from google.oauth2.credentials import Credentials
|
| 5 |
+
from googleapiclient.discovery import build
|
| 6 |
+
from google.auth.transport.requests import Request as GoogleRequest
|
| 7 |
+
import os
|
| 8 |
+
import logging
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# CORS settings (update with your frontends if needed)
|
| 13 |
+
origins = [
|
| 14 |
+
"*",
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
app.add_middleware(
|
| 18 |
+
CORSMiddleware,
|
| 19 |
+
allow_origins=origins,
|
| 20 |
+
allow_credentials=True,
|
| 21 |
+
allow_methods=["*"],
|
| 22 |
+
allow_headers=["*"],
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Schema for incoming request
|
| 26 |
+
class EventDetails(BaseModel):
|
| 27 |
+
summary: str
|
| 28 |
+
location: str | None = ''
|
| 29 |
+
description: str | None = ''
|
| 30 |
+
attendees: list[dict] | None = []
|
| 31 |
+
start: str # ISO format datetime
|
| 32 |
+
end: str # ISO format datetime
|
| 33 |
+
timeZone: str | None = 'Asia/Kolkata'
|
| 34 |
+
|
| 35 |
+
@app.post("/schedule")
|
| 36 |
+
async def schedule_event(event_details: EventDetails):
|
| 37 |
+
SCOPES = ['https://www.googleapis.com/auth/calendar']
|
| 38 |
+
creds = None
|
| 39 |
+
|
| 40 |
+
if os.path.exists('token.json'):
|
| 41 |
+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
|
| 42 |
+
|
| 43 |
+
if not creds or not creds.valid:
|
| 44 |
+
if creds and creds.expired and creds.refresh_token:
|
| 45 |
+
creds.refresh(GoogleRequest())
|
| 46 |
+
else:
|
| 47 |
+
return {
|
| 48 |
+
"status": "error",
|
| 49 |
+
"message": "Missing or invalid token. Please run locally to generate token.json."
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
service = build('calendar', 'v3', credentials=creds)
|
| 54 |
+
|
| 55 |
+
event = {
|
| 56 |
+
'summary': event_details.summary,
|
| 57 |
+
'location': event_details.location,
|
| 58 |
+
'description': event_details.description,
|
| 59 |
+
'attendees': event_details.attendees,
|
| 60 |
+
'start': {
|
| 61 |
+
'dateTime': event_details.start,
|
| 62 |
+
'timeZone': event_details.timeZone,
|
| 63 |
+
},
|
| 64 |
+
'end': {
|
| 65 |
+
'dateTime': event_details.end,
|
| 66 |
+
'timeZone': event_details.timeZone,
|
| 67 |
+
},
|
| 68 |
+
'reminders': {
|
| 69 |
+
'useDefault': False,
|
| 70 |
+
'overrides': [
|
| 71 |
+
{'method': 'email', 'minutes': 24 * 60},
|
| 72 |
+
{'method': 'popup', 'minutes': 10},
|
| 73 |
+
],
|
| 74 |
+
},
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
created_event = service.events().insert(calendarId='primary', body=event, sendUpdates='all').execute()
|
| 78 |
+
|
| 79 |
+
return {
|
| 80 |
+
"status": "success",
|
| 81 |
+
"event_link": created_event.get('htmlLink'),
|
| 82 |
+
"message": f"Event created: {created_event.get('summary')}"
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return {
|
| 87 |
+
"status": "error",
|
| 88 |
+
"message": str(e)
|
| 89 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|