Last active
September 19, 2022 04:40
-
-
Save justengel/0f828447fe08296b8b8bd462ec949dc3 to your computer and use it in GitHub Desktop.
Quick google calendar utilities
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
""" | |
https://developers.google.com/calendar/v3/reference/events/insert | |
""" | |
import os | |
import pickle | |
import datetime | |
from googleapiclient.discovery import build | |
from google_auth_oauthlib.flow import InstalledAppFlow | |
from google.auth.transport.requests import Request | |
__all__ = ['DEFAULTS', 'get_service', 'create_event'] | |
DEFAULTS = { | |
'scopes': ['https://www.googleapis.com/auth/calendar.events'], | |
'event_time': datetime.time(hour=6, minute=30, second=0), | |
} | |
def get_service(credentials_filename='credentials.json', token_filename='token.pickle', scopes=None): | |
if scopes is None: | |
scopes = DEFAULTS['scopes'] | |
creds = None | |
# The file token.pickle stores the user's access and refresh tokens, and is | |
# created automatically when the authorization flow completes for the first | |
# time. | |
if os.path.exists(token_filename): | |
with open(token_filename, 'rb') as token: | |
creds = pickle.load(token) | |
# If there are no (valid) credentials available, let the user log in. | |
if not creds or not creds.valid: | |
if creds and creds.expired and creds.refresh_token: | |
creds.refresh(Request()) | |
else: | |
flow = InstalledAppFlow.from_client_secrets_file(credentials_filename, scopes) | |
creds = flow.run_local_server(port=0) | |
# Save the credentials for the next run | |
with open(token_filename, 'wb') as token: | |
pickle.dump(creds, token) | |
service = build('calendar', 'v3', credentials=creds) | |
return service | |
def create_event(summary, start_on=None, end_on=None, attendees=None, **kwargs): | |
event = kwargs.copy() | |
event['summary'] = summary | |
if start_on is None: | |
dt = DEFAULTS['event_time'] | |
start_on = datetime.datetime.today().replace(hour=dt.hour, minute=dt.minute, second=dt.second) | |
if end_on is None: | |
end_on = start_on + datetime.timedelta(hours=1) | |
event['start'] = { | |
'dateTime': str(start_on.isoformat()), | |
'timeZone': 'America/New_York', | |
} | |
event['end'] = { | |
'dateTime': str(end_on.isoformat()), | |
'timeZone': 'America/New_York', | |
} | |
if attendees: | |
if not isinstance(attendees, list): | |
attendees = [attendees] | |
event['attendees'] = attendees | |
# event['recurrence'] = [ | |
# 'RRULE:FREQ=DAILY;COUNT=3' # INTERVAL=3, COUNT is total | |
# ] | |
# | |
# 'reminders': { | |
# 'useDefault': False, | |
# 'overrides': [ | |
# {'method': 'email', 'minutes': 24 * 60}, | |
# {'method': 'popup', 'minutes': 10}, | |
# ], | |
# }, | |
return event | |
def add_event(summary, start_on=None, end_on=None, attendees=None, service=None, calendarId='primary', **kwargs): | |
if service is None: | |
service = get_service() | |
event = create_event(summary, start_on=start_on, end_on=end_on, attendees=attendees, **kwargs) | |
return service.events().insert(calendarId=calendarId, body=event).execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment