-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSV-to-Google-Calendar-API.py
344 lines (262 loc) · 10.3 KB
/
CSV-to-Google-Calendar-API.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import openpyxl as xl
from openpyxl import Workbook, load_workbook
import pyexcel as p
from datetime import datetime
import pandas as pd
import os
from win32com.client import Dispatch
import pickle
import datetime
from collections import namedtuple
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from google.auth.transport.requests import Request
# create api service function
def create_service(client_secret_file, api_name, api_version, *scopes, prefix=''):
CLIENT_SECRET_FILE = client_secret_file
API_SERVICE_NAME = api_name
API_VERSION = api_version
SCOPES = [scope for scope in scopes[0]]
cred = None
working_dir = os.getcwd()
token_dir = 'token files'
pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}{prefix}.pickle'
# check if token dir exists first, if not, create the folder
if not os.path.exists(os.path.join(working_dir, token_dir)):
os.mkdir(os.path.join(working_dir, token_dir))
if os.path.exists(os.path.join(working_dir, token_dir, pickle_file)):
with open(os.path.join(working_dir, token_dir, pickle_file), 'rb') as token:
cred = pickle.load(token)
if not cred or not cred.valid:
if cred and cred.expired and cred.refresh_token:
cred.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES, redirect_uri="http://localhost:3000")
cred = flow.run_local_server()
# flow.run_local_server()
# credentials = flow.credentials
with open(os.path.join(working_dir, token_dir, pickle_file), 'wb') as token:
pickle.dump(cred, token)
try:
service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
print(API_SERVICE_NAME, API_VERSION, 'service created successfully')
return service
except Exception as e:
print(e)
print(f'Failed to create service instance for {API_SERVICE_NAME}')
os.remove(os.path.join(working_dir, token_dir, pickle_file))
return None
# convert datetime function
def convert_to_RFC_datetime(start_year=1900, start_month=1, start_day=1, hour=0, minute=0):
dt = datetime.datetime(start_year, start_month, start_day, hour, minute, 0).isoformat() + 'Z'
return dt
# launch api
CLIENT_SECRET_FILE = "client_secret.json" ################ do not forget to download OAuth json file and put it in current directory!!!!!!!!!!
API_NAME = 'calendar'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/calendar']
service = create_service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
# list calendars
calendar_list = service.calendarList().list(pageToken=None).execute()
# delete already existing calendar ### COMPLETELY OPTIONAL - feel free to comment!!!!
# for calendar_list_entry in calendar_list['items']:
# if 'CALENDAR_NAME' in calendar_list_entry['summary']:
# id = calendar_list_entry['id']
# service.calendars().delete(calendarId=id).execute()
# create new calendar
calendar_body = {
'summary': 'CALENDAR_NAME',
'timeZone': 'America/Sao_Paulo', ### YOU CAN CHECK TIMEZONES TABLE -> https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
}
service.calendars().insert(body=calendar_body).execute()
# list calendars
calendar_list = service.calendarList().list(pageToken=None).execute()
# get calendar id
for calendar_list_entry in calendar_list['items']:
if 'CALENDAR_NAME' in calendar_list_entry['summary']:
id = calendar_list_entry['id']
##TODO: bug that times from before 16:00 to after 16:00, the end date should be the next date because of timem conversion
## 16+8=0 (e.g. 15 to 16 would be 23 to 0)
def adjust_time(time, adjust):
adjusted = time+adjust
if(adjusted>=24):
adjusted-=24
return adjusted
# insert events to google calendar function
def insert_events(color):
# validate if it is an all-day event or not
def is_all_day_event(i):
if ("FALSE" or "False" or "false") in all_day_event[i]:
return False
else:
return True
x = 0
for i in range(0, max_rows):
if is_all_day_event(i):
all_day_event_true_start.append("{}-{}-{}".format(start_year[i], start_month[i], start_day[i]))
all_day_event_true_end.append("{}-{}-{}".format(end_year[i], end_month[i], end_day[i]))
event_request_body = {
'start':{
'date': all_day_event_true_start[x],
'timeZone': 'America/Sao_Paulo',
},
'end':{
'date': all_day_event_true_end[x],
'timeZone': 'America/Sao_Paulo',
},
'summary': subject[i],
'description': description[i],
'location': location[i],
'colorId': color,
'visibility': is_private[i]
#'attendees':[
# {
# 'email': '',
# 'optional': False,
# 'responseStatus': 'accepted',
# }
#],
#'reminders': {
# 'useDefault': False,
# 'overrides':[
# {'method': 'email', 'minutes': 30},
# ]
#}
}
service.events().insert(calendarId=id, body=event_request_body).execute()
x += 1
else:
adjust_timezone = 8 # (8 is for -8), (3 is for UTC-3) ### CHANGE TO YOUR OWN TIMEZONE, COULD BE -n, +n, or none, BASED ON UTC TIME (0)
# print("start time:", adjust_time(int(fstart_time[i]), adjust_timezone), "\n",
# "end time:", adjust_time(int(fend_time[i]), adjust_timezone), "\n",
# "end time:", int(fend_time[i]), "to", adjust_timezone
# )
print("adding: ", subject[i])
event_request_body = {
'start':{
'dateTime': convert_to_RFC_datetime(int(start_year[i]), int(start_month[i]), int(start_day[i]), adjust_time(int(fstart_time[i]), adjust_timezone) ,0), #int(fstart_time[i]) + adjust_timezone,
'timeZone': 'America/Sao_Paulo',
},
'end':{
'dateTime': convert_to_RFC_datetime(int(end_year[i]), int(end_month[i]), int(end_day[i]), adjust_time(int(fend_time[i]), adjust_timezone), 0), #int(fend_time[i]) + adjust_timezone
'timeZone': 'America/Sao_Paulo',
},
'summary': subject[i],
'description': description[i],
'location': location[i],
'colorId': color,
'visibility': is_private[i]
#'attendees':[
# {
# 'email': '',
# 'optional': False,
# 'responseStatus': 'accepted',
# }
#],
#'reminders': {
# 'useDefault': False,
# 'overrides':[
# {'method': 'email', 'minutes': 30},
# ]
#}
}
service.events().insert(calendarId=id, body=event_request_body).execute()
all_day_event_true_start = []
all_day_event_true_end = []
# convert csv file to excel
if not os.path.exists('excel_file.xlsx'):
read_file = pd.read_csv('Schedule.csv') # YOU MAY NEED TO DECLARE THE SPECIFIC ENCODING -> https://docs.python.org/3.7/library/codecs.html#standard-encodings
read_file.to_excel('excel_file.xlsx', index = None, header = True)
# load xlsx file containing the events
wb = load_workbook('excel_file.xlsx')
ws = wb.active
# delete header row
ws.delete_rows(1)
# row number variable
max_rows = ws.max_row
# copy subject
subject = []
for i in range(1, max_rows+1):
subject.append(ws.cell(row = i, column = 1).value)
## FORMATTING START DATE
# copy start date
start_date = []
for i in range(1, max_rows+1):
start_date.append(ws.cell(row = i, column = 2).value)
# append lists
start_month = []
start_day = []
start_year = []
# format start month, day & year
for i in range(0, max_rows):
start_month.append(start_date[i])
start_day.append(start_date[i])
start_year.append(start_date[i])
start_month = [x[:-6] for x in start_month]
start_day = [x[3:-3] for x in start_day]
start_year = ['20' + x[6:] for x in start_year]
## FORMATTING START TIME
# copy start time
start_time = []
for i in range(1, max_rows+1):
start_time.append(ws.cell(row = i, column = 3).value)
# format start time
fstart_time = []
for i in range(0, max_rows):
fstart_time.append(start_time[i])
fstart_time = [x[:-3] for x in fstart_time]
## FORMATTING END DATE
# copy end date
end_date = []
for i in range(1, max_rows+1):
end_date.append(ws.cell(row = i, column = 4).value)
# append lists
end_month = []
end_day = []
end_year = []
# format end month, day & year
for i in range(0, max_rows):
end_month.append(end_date[i])
end_day.append(end_date[i])
end_year.append(end_date[i])
end_month = [x[:-6] for x in end_month]
end_day = [x[3:-3] for x in end_day]
end_year = ['20' + x[6:] for x in end_year]
## FORMATTING END TIME
# copy end time
end_time = []
for i in range(1, max_rows+1):
end_time.append(ws.cell(row = i, column = 5).value)
# format end time
fend_time = []
for i in range(0, max_rows):
fend_time.append(end_time[i])
fend_time = [x[:-3] for x in fend_time]
# copy all-day event
all_day_event = []
for i in range(1, max_rows+1):
all_day_event.append(ws.cell(row = i, column = 6).value)
for i in range(0, max_rows):
all_day_event[i] = str(all_day_event[i]).upper()
# copy description
description = []
for i in range(1, max_rows+1):
description.append(ws.cell(row = i, column = 7).value)
# copy location
location = []
for i in range(1, max_rows+1):
location.append(ws.cell(row = i, column = 8).value)
# copy private
is_private = []
for i in range(1, max_rows+1):
is_private.append(ws.cell(row = i, column = 9).value)
# adapt private to fit json
for i in range(0, max_rows):
is_private[i] = str(is_private[i]).upper()
if ("TRUE" or "True" or "true") in is_private[i]:
is_private[i] = "private"
else:
is_private[i] = "default"
# insert events(color) ---> check available colors here => https://lukeboyle.com/blog/posts/google-calendar-api-color-id
insert_events(7)