another Disclaimer: This is really ugly and often fails. Try to get it running, as the apiclient has got the service (this is the hard part), everything should work.
WARNING: oauth2client is outdated and no longer maintained. I still use it but you shouldn't. Unfortunately the calendar quickstart documentation uses it, so one has to put own effort in using more modern tools.
Just one google account and you can follow the guide here. The normal use of the API is free.
In [1]:
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import datetime
SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'
import argparse
from argparse import Namespace
flags = None
In [2]:
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-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
credentials = tools.run_flow(
flow, store,
Namespace(
auth_host_name='localhost', auth_host_port=[8080, 8090],
logging_level='ERROR', noauth_local_webserver=False))
In [3]:
http = credentials.authorize(httplib2.Http())
In [4]:
service = discovery.build('calendar', 'v3', credentials=credentials)
Yes, most of this code is not good. There are no good docstrings nor good code.
This is not a bug, it's an possibility to improve ;-)
Mainly, the helper file contains some convenience helpers and two important functions:
create_pydata_calendars
(this creates the calendars if there is none with the same summary)write_event
(this creates the event if there is none with same time and summary)
In [13]:
from helpers import *
In [6]:
now = datetime.datetime.utcnow().isoformat() + 'Z'
CONFERENCE_START = '2018-07-06'
LEVEL_LIST = ['pydata_novice', 'pydata_intermediate', 'pydata_experienced']
hint: the LEVEL_LIST
can be created automatically from the dataframe of events later and then execute these functions later -> therefore you can also split by room instead of level if you want to have separate calendars for that.
In [7]:
create_pydata_calendars(service=service)
Out[7]:
In [8]:
reduce_to_summary(get_cals(service, LEVEL_LIST))
Out[8]:
In [9]:
cals = get_cals(service, LEVEL_LIST)
In [10]:
df = pd.read_json('./data.json')[
[
'description', 'title', 'day_info', 'level',
'room_inf', 'speaker', 'time_from', 'time_to'
]]
df.rename(
columns={
'title': 'summary',
'room_inf': 'location',
'speaker': 'attendees'
}, inplace=True)
df.level = 'pydata_' + df.level.str.lower()
# mapping date (dayofweek) to right format for google calendar api values
df['date'] = df.day_info.map(
{
ddd.strftime('%A'): ddd.strftime('%Y-%m-%dT')
for ddd in pd.date_range(
start=CONFERENCE_START, periods=df.day_info.nunique(), freq="D")})
df['start'] = df['date'] + df.time_from
df['end'] = df['date'] + df.time_to
In [11]:
df.head(2)
Out[11]:
In [12]:
for (_, event) in df.iterrows():
write_event(event, cals, service)