-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalapi.py
57 lines (46 loc) · 1.99 KB
/
calapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pprint
import datefinder
from datetime import datetime, timedelta
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
import pickle
scopes = ['https://www.googleapis.com/auth/calendar']
flow = InstalledAppFlow.from_client_secrets_file("client_secret.json", scopes=scopes)
#Runs user authentication flow giving the html link to begin authorisation
credentials = flow.run_console()
#Stores the user credentials here
pickle.dump(credentials, open("token.pkl", "wb"))
credentials = pickle.load(open("token.pkl", "rb"))
#Uses the credentials to begin making python event
service = build("calendar", "v3", credentials=credentials)
result = service.calendarList().list().execute()
calendar_id = result['items'][0]['id']
pp = pprint.PrettyPrinter(indent=4) #use pprint for nicer view
timezone = 'Singapore' #enter your timezone
#Summary is the same as the title for the event
def create_event(start_time_str, summary, duration=1,attendees=None, description=None, location=None):
matches = list(datefinder.find_dates(start_time_str))
if len(matches):
start_time = matches[0]
end_time = start_time + timedelta(hours=duration)
event = {
'summary': summary,
'location': location,
'description': description,
'start': {
'dateTime': start_time.strftime("%Y-%m-%dT%H:%M:%S"),
'timeZone': timezone,
},
'end': {
'dateTime': end_time.strftime("%Y-%m-%dT%H:%M:%S"),
'timeZone': timezone,
},
}
pp.pprint('''*** %r event added:
With: %s
Start: %s
End: %s''' % (summary.encode('utf-8'),
attendees,start_time, end_time))
return service.events().insert(calendarId='primary', body=event,sendNotifications=True).execute()
create_event(start_time_str= '23 Jan 12.30pm', summary="Test Meeting using CreateFunction Method",
description="Test Description",location="Mentone, VIC, Australia") #callfunction