Insert New Calendar Before Inserting Events Google API and Delete Calendar Events (in Python)

Viewed 430

Thank you so much for taking time to help!

I am creating a Python script that I need to do the following:

  • Create a new calendar; call it "XYZ" (instead of the "Primary" calendar)
  • Insert hard coded custom events
  • Have the ability to delete the entire "XYZ" calendar and subsequent inserted events if needed

I am having trouble inserting a new calendar to an oauthed Calendar. I have looked through the Google calendar resources, but can't seem to follow it at all. My code right now is as follows:

Note: since I don't know how to insert a new calendar, the events are currently coded to enter into the primary calendar

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents 
    [tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/calendar'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store, flags) \
          if flags else tools.run(flow, store)

 CAL = build('calendar', 'v3', http=creds.authorize(Http()))

 GMT_OFF = '-04:00'          # ET/MST/GMT-4
EVENT = {
     'summary': 'Find venue',
     'start': {'dateTime': '2017-06-23T13:00:00%s' % GMT_OFF},
     'end': {'dateTime': '2017-06-23T14:00:00%s' % GMT_OFF},
}
 EVENT2 = {
     'summary': 'Visit museum',
     'start': {'dateTime': '2017-07-09T13:00:00%s' % GMT_OFF},
     'end': {'dateTime': '2017-07-09T14:00:00%s' % GMT_OFF},
}

 EVENT3 = {
     'summary': 'Buy fruits',
     'start': {'dateTime': '2017-07-23T13:00:00%s' % GMT_OFF},
     'end': {'dateTime': '2017-07-23T14:00:00%s' % GMT_OFF},
}

e = CAL.events().insert(calendarId='primary',
                    sendNotifications=True, body=EVENT).execute()

e = CAL.events().insert(calendarId='primary',
                    sendNotifications=True, body=EVENT2).execute()

e = CAL.events().insert(calendarId='primary',
                    sendNotifications=True, body=EVENT3).execute()

print('''*** %r event added:
    Start: %s
    End: %s''' % (e['summary'].encode('utf-8'),
                  e['start']['dateTime'], e['end']['dateTime']))

So, before the Calendar event inserts, I'd like to insert an entire a new calendar named 'XYZ'. Then, the following events coded, would be added to 'XYZ'. Separately, I'd like to add the ability to delete the calendar 'XYZ' from an oauthed Gmail upon user request.

Thank you again for your help! Let me know if I can provide greater clarity.

1 Answers
Related