Refactor event logic to support Google Calendar updates
This commit is contained in:
parent
b401b0335e
commit
ee909815b6
@ -145,17 +145,15 @@ def save_local_sync(file_path: str, events: EventsDict) -> None:
|
|||||||
logger.error(f"Problematic field: {key} = {value} (type: {type(value)})")
|
logger.error(f"Problematic field: {key} = {value} (type: {type(value)})")
|
||||||
|
|
||||||
|
|
||||||
def add_event_to_google(service: Resource, event: EventDict, calendar_id: str) -> None:
|
def _create_google_event_body(event: EventDict) -> Dict[str, Any]:
|
||||||
"""Add a single event to Google Calendar.
|
"""Create the event body for Google Calendar API.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
service: Authenticated Google Calendar API service object.
|
|
||||||
event: Dictionary containing event details.
|
event: Dictionary containing event details.
|
||||||
calendar_id: ID of the target Google Calendar.
|
|
||||||
"""
|
|
||||||
logger.info(f"Processing event: {event['summary']} (UID: {event['uid']})")
|
|
||||||
|
|
||||||
try:
|
Returns:
|
||||||
|
Dict[str, Any]: Formatted event body for Google Calendar API.
|
||||||
|
"""
|
||||||
google_event = {
|
google_event = {
|
||||||
"summary": event["summary"],
|
"summary": event["summary"],
|
||||||
"description": event.get("description", ""),
|
"description": event.get("description", ""),
|
||||||
@ -179,6 +177,36 @@ def add_event_to_google(service: Resource, event: EventDict, calendar_id: str) -
|
|||||||
exdates = [f"EXDATE;TZID=UTC:{date}" for date in event["exdate"]]
|
exdates = [f"EXDATE;TZID=UTC:{date}" for date in event["exdate"]]
|
||||||
google_event["recurrence"].extend(exdates)
|
google_event["recurrence"].extend(exdates)
|
||||||
|
|
||||||
|
return google_event
|
||||||
|
|
||||||
|
|
||||||
|
def add_event_to_google(service: Resource, event: EventDict, calendar_id: str) -> None:
|
||||||
|
"""Add or update a single event in Google Calendar.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
service: Authenticated Google Calendar API service object.
|
||||||
|
event: Dictionary containing event details.
|
||||||
|
calendar_id: ID of the target Google Calendar.
|
||||||
|
"""
|
||||||
|
logger.info(f"Processing event: {event['summary']} (UID: {event['uid']})")
|
||||||
|
|
||||||
|
try:
|
||||||
|
google_event = _create_google_event_body(event)
|
||||||
|
|
||||||
|
if event.get("google_event_id"):
|
||||||
|
logger.info(f"Updating existing event in Google Calendar: {event['summary']} (Google ID: {event['google_event_id']})")
|
||||||
|
created_event = (
|
||||||
|
service.events()
|
||||||
|
.update(
|
||||||
|
calendarId=calendar_id,
|
||||||
|
eventId=event["google_event_id"],
|
||||||
|
body=google_event,
|
||||||
|
)
|
||||||
|
.execute()
|
||||||
|
)
|
||||||
|
logger.info(f"Successfully updated event: {event['summary']} (Google ID: {created_event['id']})")
|
||||||
|
else:
|
||||||
|
logger.info(f"Creating new event in Google Calendar: {event['summary']}")
|
||||||
created_event = (
|
created_event = (
|
||||||
service.events()
|
service.events()
|
||||||
.insert(
|
.insert(
|
||||||
@ -187,14 +215,13 @@ def add_event_to_google(service: Resource, event: EventDict, calendar_id: str) -
|
|||||||
)
|
)
|
||||||
.execute()
|
.execute()
|
||||||
)
|
)
|
||||||
|
|
||||||
event["google_event_id"] = created_event["id"]
|
event["google_event_id"] = created_event["id"]
|
||||||
logger.info(f"Successfully created event: {event['summary']} (Google ID: {created_event['id']})")
|
logger.info(f"Successfully created event: {event['summary']} (Google ID: {created_event['id']})")
|
||||||
|
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to add event {event['summary']} (UID: {event['uid']})")
|
logger.error(f"Failed to add/update event {event['summary']} (UID: {event['uid']})")
|
||||||
logger.error(f"Error: {str(e)}")
|
logger.error(f"Error: {str(e)}")
|
||||||
error_events.append(event)
|
error_events.append(event)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user