Last active
October 10, 2018 06:05
-
-
Save thehesiod/6aa3813136aacbff278a6ed477bd9e66 to your computer and use it in GitHub Desktop.
Google Calendar bug with recurring events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from googleapiclient import discovery | |
import httplib2 | |
from oauth2client.service_account import ServiceAccountCredentials | |
calendar_id = 'user@host.com' | |
resource_calendar_id = 'resourceUser@resource.calendar.google.com' | |
def get_delegated_endpoint(base_creds: ServiceAccountCredentials, user_email: str): | |
creds = base_creds.create_delegated(user_email) | |
http = httplib2.Http(timeout=15) | |
http = creds.authorize(http) | |
endpoint: discovery.Resource = discovery.build('calendar', 'v3', http=http) | |
return endpoint | |
def create(base_creds: ServiceAccountCredentials): | |
now = utc_now_with_tz() | |
endpoint: discovery.Resource = get_delegated_endpoint(base_creds, calendar_id) | |
start = now.astimezone(_tzlocal).replace(hour=13, minute=0, second=0, microsecond=0) - timedelta(days=1) | |
start = start.astimezone(timezone.utc) | |
body = dict( | |
summary="scripted meeting", | |
start={'dateTime': _get_iso_dt_format(start), "timeZone": "America/Los_Angeles"}, | |
end={'dateTime': _get_iso_dt_format(start + timedelta(minutes=10)), "timeZone": "America/Los_Angeles"}, | |
recurrence=["RRULE:FREQ=DAILY"], | |
attendees=[ | |
{"email": resource_calendar_id, "responseStatus": "needsAction"}, | |
{"email": calendar_id, "responseStatus": "accepted"}, | |
], | |
visibility="private" | |
) | |
event_obj = endpoint.events().insert( | |
calendarId=calendar_id, | |
sendNotifications=True, | |
body=body | |
).execute() | |
print("sleeping for 3s") | |
time.sleep(3) | |
response = endpoint.events().instances(calendarId=calendar_id, eventId=event_obj["id"]).execute() | |
recurrance_evt_obj = response["items"][1] | |
assert "_" in recurrance_evt_obj["id"] | |
# XXX - modify here to set instance start time | |
start = iso8601.parse_date(recurrance_evt_obj["start"]["dateTime"]) | |
start_tz = start.tzinfo | |
assert start.date() == now.astimezone(start_tz).date() # we want to change today's instance | |
print(f"instance start: {start}") | |
now_plus_delta = (now - timedelta(hours=2)).astimezone(start_tz) | |
start = start.replace(hour=now_plus_delta.hour, minute=now_plus_delta.minute, second=now_plus_delta.second) | |
body = dict( | |
start={'dateTime': start.isoformat(), "timeZone": "America/Los_Angeles"}, | |
end={'dateTime': (start + timedelta(minutes=1)).isoformat(), "timeZone": "America/Los_Angeles"}, | |
) | |
print(f"updating {pformat(recurrance_evt_obj)} with body: {pformat(body)}") | |
endpoint.events().patch(calendarId=calendar_id, eventId=recurrance_evt_obj["id"], sendNotifications=True, body=body).execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment