Skip to content

Instantly share code, notes, and snippets.

@ngs
Created August 31, 2012 09:18
Show Gist options
  • Save ngs/3550670 to your computer and use it in GitHub Desktop.
Save ngs/3550670 to your computer and use it in GitHub Desktop.
Create Events on Google Calendar with Git Commit

Create Events on Google Calendar with Git Commit

Requirements

  • google-api-python-client
  • oauth2
  • rfc3339

Set up

pip install google-api-python-client oauth2 rfc3339

cd /path/to/project
curl -L http://s.liap.us/gcal-post-commit.py > .git/hooks/post-commit
chmod +x .git/hooks/post-commit

vim .git/hooks/post-commit

Create new client on the console and replace variables.

#!/usr/bin/env python
import sys, os
import gflags
import httplib2
import time
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
from rfc3339 import rfc3339
FLAGS = gflags.FLAGS
FLOW = OAuth2WebServerFlow(
client_id='YOUR_CLIENT_ID',
client_secret='YOUR_CLIENT_SECRET',
scope='https://www.googleapis.com/auth/calendar',
user_agent='YOUR_APPLICATION_NAME/YOUR_APPLICATION_VERSION')
storage = Storage('.git/calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
credentials = run(FLOW, storage)
http = httplib2.Http()
http = credentials.authorize(http)
service = build(serviceName='calendar', version='v3', http=http,
developerKey='YOUR_DEVELOPER_KEY')
description = "%s\n" % os.popen("git whatchanged -1 --pretty=format:\"commiter: %cn <%ae>%ndate : %cd%n%s%n--------------------%n%b%n\"").read()
summary = "[commit] %s" % os.popen("git log -1 --oneline").read()
startTime = time.time() - 60 * 60
endTime = time.time()
event = {
'summary': summary,
'description': description,
'start' : { 'dateTime' : rfc3339(startTime) },
'end' : { 'dateTime' : rfc3339(endTime) }
}
created_event = service.events().insert(calendarId='YOUR_CALENDAR_ID', body=event).execute()
print "Created Event: %s" % created_event['id']
@michaelkariv
Copy link

genious

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment