Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- app.py +97 -0
- credentials.json +1 -0
- requirements.txt +13 -0
- token.json +1 -0
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from google.oauth2.credentials import Credentials
|
| 5 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 6 |
+
from googleapiclient.discovery import build
|
| 7 |
+
from google.auth.transport.requests import Request as GoogleRequest
|
| 8 |
+
import os
|
| 9 |
+
import logging
|
| 10 |
+
|
| 11 |
+
# ---------- FastAPI Setup ----------
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
origins = [
|
| 15 |
+
"https://e04c-49-206-114-222.ngrok-free.app",
|
| 16 |
+
"http://localhost:8501"
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
app.add_middleware(
|
| 20 |
+
CORSMiddleware,
|
| 21 |
+
allow_origins=origins,
|
| 22 |
+
allow_credentials=True,
|
| 23 |
+
allow_methods=["*"],
|
| 24 |
+
allow_headers=["*"],
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# ---------- Pydantic Schema ----------
|
| 28 |
+
class EventDetails(BaseModel):
|
| 29 |
+
summary: str
|
| 30 |
+
location: str | None = ''
|
| 31 |
+
description: str | None = ''
|
| 32 |
+
attendees: list[dict] | None = []
|
| 33 |
+
start: str
|
| 34 |
+
end: str
|
| 35 |
+
timeZone: str | None = 'Asia/Kolkata'
|
| 36 |
+
|
| 37 |
+
# ---------- Schedule Endpoint ----------
|
| 38 |
+
@app.post("/schedule")
|
| 39 |
+
async def schedule_event(event_details: EventDetails):
|
| 40 |
+
SCOPES = ['https://www.googleapis.com/auth/calendar']
|
| 41 |
+
creds = None
|
| 42 |
+
|
| 43 |
+
if os.path.exists('token.json'):
|
| 44 |
+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
|
| 45 |
+
|
| 46 |
+
if not creds or not creds.valid:
|
| 47 |
+
if creds and creds.expired and creds.refresh_token:
|
| 48 |
+
creds.refresh(GoogleRequest())
|
| 49 |
+
else:
|
| 50 |
+
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
|
| 51 |
+
flow.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
|
| 52 |
+
auth_url, _ = flow.authorization_url(prompt='consent')
|
| 53 |
+
print(f"Go to this URL: {auth_url}")
|
| 54 |
+
code = input("Enter the authorization code: ")
|
| 55 |
+
flow.fetch_token(code=code)
|
| 56 |
+
creds = flow.credentials
|
| 57 |
+
with open('token.json', 'w') as token:
|
| 58 |
+
token.write(creds.to_json())
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
service = build('calendar', 'v3', credentials=creds)
|
| 62 |
+
logging.info(f"Service type: {type(service)}")
|
| 63 |
+
|
| 64 |
+
event = {
|
| 65 |
+
'summary': event_details.summary,
|
| 66 |
+
'location': event_details.location,
|
| 67 |
+
'description': event_details.description,
|
| 68 |
+
'attendees': event_details.attendees,
|
| 69 |
+
'start': {
|
| 70 |
+
'dateTime': event_details.start,
|
| 71 |
+
'timeZone': event_details.timeZone,
|
| 72 |
+
},
|
| 73 |
+
'end': {
|
| 74 |
+
'dateTime': event_details.end,
|
| 75 |
+
'timeZone': event_details.timeZone,
|
| 76 |
+
},
|
| 77 |
+
'reminders': {
|
| 78 |
+
'useDefault': False,
|
| 79 |
+
'overrides': [
|
| 80 |
+
{'method': 'email', 'minutes': 24 * 60},
|
| 81 |
+
{'method': 'popup', 'minutes': 10},
|
| 82 |
+
],
|
| 83 |
+
},
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
created_event = service.events().insert(calendarId='primary', body=event, sendUpdates='all').execute()
|
| 87 |
+
return {
|
| 88 |
+
"status": "success",
|
| 89 |
+
"event_link": created_event.get('htmlLink'),
|
| 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 |
+
}
|
credentials.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"installed":{"client_id":"115374007511-ev1gccd64gib0csrrh3agnc1tejemr4d.apps.googleusercontent.com","project_id":"taskgamifier","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}}
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
google-adk
|
| 2 |
+
Flask
|
| 3 |
+
google-api-python-client
|
| 4 |
+
google-auth-httplib2
|
| 5 |
+
google-auth-oauthlib
|
| 6 |
+
google-cloud-storage
|
| 7 |
+
google-genai
|
| 8 |
+
requests
|
| 9 |
+
streamlit
|
| 10 |
+
python-dotenv
|
| 11 |
+
fastapi
|
| 12 |
+
uvicorn
|
| 13 |
+
pydantic
|
token.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"token": "ya29.a0AW4Xtxh_5d7edUKdNtyhHkGzZ5rEMeMbnpshJX_QMFGZAZv4FGqxu_9rUNG0H2qhICaYMapzn-geunaVj1YnK26IgGL-4j6H9ZCccifQqQ1Ta8-etRJJHI6yxSgcteuJqEvjINTvxwi_-Z2EOVbzhLAXiP8THkBq9PW2i_YtMQaCgYKAb0SARISFQHGX2MiqHVyybQE9UuHsyYw0SsOPQ0177", "refresh_token": "1//0gDSeP_yGnAcxCgYIARAAGBASNwF-L9Iro-bAKEvPpobqpM_jrQtOMrzyMdDvqq-VN59ueD_n5i97BPT_7J6-eN4Z1mkEq51dYFo", "token_uri": "https://oauth2.googleapis.com/token", "client_id": "115374007511-ev1gccd64gib0csrrh3agnc1tejemr4d.apps.googleusercontent.com", "client_secret": "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", "scopes": ["https://www.googleapis.com/auth/calendar"], "universe_domain": "googleapis.com", "account": "", "expiry": "2025-06-18T21:51:11.321792Z"}
|