Skip to content

Instantly share code, notes, and snippets.

@phdd
Last active April 30, 2018 07:50
Show Gist options
  • Save phdd/6ae8f48bd1c38c070dbb5531a0fa82f9 to your computer and use it in GitHub Desktop.
Save phdd/6ae8f48bd1c38c070dbb5531a0fa82f9 to your computer and use it in GitHub Desktop.
Google Calendar recurring Event hack

Google Calendar recurring events hack

There seems to be a synchronization issue on some Android devices. Any calendar app renders recurring events a couple hundred times. Pretty annoying, cause it gets slow as hell and my smartwatch constantly chokes on all the notifications. This little Python script transforms all recurring events within a two-year timespan to single event instances. Thus, it's hacky but it solves the issue.

Install

Go through the tutorial at https://developers.google.com/calendar/quickstart/python. As a last step call python transform.py.

Done!

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/calendar'
store = file.Storage('credentials.json')
try:
creds = store.get()
except KeyError:
pass
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
calendar = build('calendar', 'v3', http=creds.authorize(Http()))
from google import calendar
from copy import deepcopy
import datetime
def transform(recurringEvent):
simpleEvent = deepcopy(recurringEvent)
del simpleEvent['kind']
del simpleEvent['etag']
del simpleEvent['id']
del simpleEvent['status']
del simpleEvent['htmlLink']
del simpleEvent['created']
del simpleEvent['updated']
del simpleEvent['creator']
del simpleEvent['organizer']
del simpleEvent['recurringEventId']
del simpleEvent['originalStartTime']
del simpleEvent['iCalUID']
del simpleEvent['sequence']
simpleEvent = calendar.events().insert(calendarId='primary', body=simpleEvent).execute()
if simpleEvent:
calendar.events().delete(calendarId='primary', eventId=recurringEvent['id']).execute()
start = simpleEvent['start'].get('dateTime', simpleEvent['start'].get('date'))
print(' ✅', start, simpleEvent['summary'])
else:
print(' ❗', start, simpleEvent['summary'])
pageToken = None
todayMinusOneYear = datetime.datetime.now() - datetime.timedelta(days=365)
todayPlusOneYear = datetime.datetime.now() + datetime.timedelta(days=365)
timeMin = todayMinusOneYear.isoformat() + 'Z' # 'Z' indicates UTC time
timeMax = todayPlusOneYear.isoformat() + 'Z' # 'Z' indicates UTC time
while True:
data = calendar.events().list(
calendarId='primary', singleEvents=True, pageToken=pageToken,
maxResults=100, timeMin=timeMin, timeMax=timeMax
).execute()
events = data.get('items', [])
pageToken = data.get('nextPageToken')
if events:
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(' ', start, event['summary'], ' ' * 120, '\r', end='')
if 'recurringEventId' in event:
transform(event)
if not pageToken:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment