62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
"""
|
|
Google Calendar API integration.
|
|
Reads today's events and upcoming schedule.
|
|
"""
|
|
import datetime
|
|
from googleapiclient.discovery import build
|
|
from core.google_auth import get_credentials
|
|
|
|
def _service():
|
|
return build('calendar', 'v3', credentials=get_credentials())
|
|
|
|
def get_todays_events() -> list[dict]:
|
|
"""Return all events happening today."""
|
|
now = datetime.datetime.now(tz=datetime.timezone.utc)
|
|
end = now.replace(hour=23, minute=59, second=59)
|
|
result = _service().events().list(
|
|
calendarId='primary',
|
|
timeMin=now.isoformat(),
|
|
timeMax=end.isoformat(),
|
|
singleEvents=True,
|
|
orderBy='startTime'
|
|
).execute()
|
|
return [
|
|
{
|
|
'summary': e.get('summary', '(No title)'),
|
|
'start': e['start'].get('dateTime', e['start'].get('date')),
|
|
'end': e['end'].get('dateTime', e['end'].get('date')),
|
|
'location': e.get('location', ''),
|
|
}
|
|
for e in result.get('items', [])
|
|
]
|
|
|
|
def get_upcoming_events(days: int = 7) -> list[dict]:
|
|
"""Return events in the next N days."""
|
|
now = datetime.datetime.now(tz=datetime.timezone.utc)
|
|
end = now + datetime.timedelta(days=days)
|
|
result = _service().events().list(
|
|
calendarId='primary',
|
|
timeMin=now.isoformat(),
|
|
timeMax=end.isoformat(),
|
|
maxResults=20,
|
|
singleEvents=True,
|
|
orderBy='startTime'
|
|
).execute()
|
|
return [
|
|
{
|
|
'summary': e.get('summary', '(No title)'),
|
|
'start': e['start'].get('dateTime', e['start'].get('date')),
|
|
}
|
|
for e in result.get('items', [])
|
|
]
|
|
|
|
def create_event(summary: str, start_dt: str, end_dt: str, description: str = "") -> dict:
|
|
"""Create a new calendar event. Dates in ISO 8601 format."""
|
|
event = {
|
|
'summary': summary,
|
|
'description': description,
|
|
'start': {'dateTime': start_dt, 'timeZone': 'America/Chicago'},
|
|
'end': {'dateTime': end_dt, 'timeZone': 'America/Chicago'},
|
|
}
|
|
return _service().events().insert(calendarId='primary', body=event).execute()
|