Skip to content

Instantly share code, notes, and snippets.

@cadesalaberry
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cadesalaberry/69399c55014a4c0e79f3 to your computer and use it in GitHub Desktop.
Save cadesalaberry/69399c55014a4c0e79f3 to your computer and use it in GitHub Desktop.
Move Event to Google Calendar
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import httplib2
import os
import json
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Quickstart'
flags = None
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'calendar-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatability with Python 2.6
credentials = tools.run(flow, store)
print 'Storing credentials to ' + credential_path
return credentials
def print_calendars():
page_token = None
while True:
calendar_list = service.calendarList().list(pageToken=page_token).execute()
for calendar_list_entry in calendar_list['items']:
print calendar_list_entry['id'], (calendar_list_entry['summary']).encode('utf8')
page_token = calendar_list.get('nextPageToken')
if not page_token:
break
def move_event_from_primary_to_NOMAD(event):
move_event_from_primary_to_calendar(event, '9de3s4e2p4s7els9lesuc4ql5s@group.calendar.google.com')
def move_event_from_primary_to_calendar(event, to_cal):
move_event_from_to_calendar(event, 'primary', to_cal)
def move_event_from_to_calendar(event, from_cal, to_cal):
# First retrieve the event from the API.
updated_event = service.events().move(
calendarId=from_cal,
eventId=event['id'],
destination=to_cal).execute()
# print updated_event.__dict__
# Print the updated date.
print 'Updated:', updated_event['summary']
print 'Updated:', updated_event['updated']
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
# print service.__dict__.keys()
# print_calendars()
eventsResult = service.events().list(
calendarId='primary',
timeMin='2015-02-01T10:00:00Z',
# timeMin='2015-02-01T10:00:00-07:00',
maxResults=100,
singleEvents=True,
q='[NOMAD]',
orderBy='startTime'
).execute()
events = eventsResult.get('items', [])
# print events
for event in events:
print event['id'], event['summary']
move_event_from_primary_to_NOMAD(event)
print 'Total events', len(events)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment