This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
googleapi.py
127 lines (104 loc) · 4.32 KB
/
googleapi.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
from __future__ import print_function
import datetime
import pytz
from googleapiclient.discovery import build
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']
def formatTime(start_timezone, start_time, end_time, recur_end_date=None):
"""
Formats the timing based on the API requirements.
:param start_time: datetime object (Y, m, D, H, M)
:param start_timezone: string local timezone of user (format Continent/City)
:param end_time: datetime object (Y, m, D, H, M)
:param recur_end_date: datetime object (Y, m, D, H, M)
:return:
"""
if(start_time.tzinfo is None):
timezone = datetime.timedelta(hours=float(start_timezone), minutes=0)
start_time = start_time + timezone
start_time = start_time.astimezone(pytz.timezone('UTC'))
start_time = datetime.datetime.strftime(start_time, "%Y-%m-%dT%H:%M:%SZ")
if(end_time.tzinfo is None):
timezone = datetime.timedelta(hours=float(start_timezone), minutes=0)
end_time = end_time + timezone
end_time = end_time.astimezone(pytz.timezone('UTC'))
end_time = datetime.datetime.strftime(end_time, "%Y-%m-%dT%H:%M:%SZ")
if recur_end_date is not None:
timezone = datetime.timedelta(hours=float(start_timezone), minutes=0)
recur_end_date = recur_end_date + timezone
recur_end_date = recur_end_date.astimezone(pytz.timezone('UTC'))
recur_end_date = datetime.datetime.strftime(recur_end_date, "%Y%m%dT%H%M%SZ")
return start_time, end_time, recur_end_date
def createEvent(event_title, timezone, start_time, end_time, recur_end_date=None, recur_freq=None):
"""
Create a google event dictionary. Sets a default reminder
All params are string type.
:param event_title: Name of the event
:param start_time: start time of event (format '2021-05-28T09:00:00-07:00')
:param timezone: timezone of start time (Country_Name/City)
:param end_time: end time of event (format '2021-05-28T17:00:00-07:00')
:param recur_freq: how frequently will the event recur (DAILY, WEEKLY)
:param recur_end_date: when will the event recurrence stop (format 20110617T065959Z)
:return: event dictionary
"""
if recur_freq is not None:
event = {
'summary': event_title,
'start': {
'dateTime': start_time,
'timeZone': timezone,
},
'end': {
'dateTime': end_time,
'timeZone': timezone,
},
'recurrence': [
'RRULE:FREQ={};UNTIL={}'.format(recur_freq, recur_end_date)
],
'reminders': {
'useDefault': True
},
}
else:
event = {
'summary': event_title,
'start': {
'dateTime': start_time,
'timeZone': timezone,
},
'end': {
'dateTime': end_time,
'timeZone': timezone,
},
'reminders': {
'useDefault': True
},
}
return event
def master(event_title, curr_timezone, req_timezone, start_date, end_time, end_date, freq, http_auth):
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
service = build('calendar', 'v3', http_auth)
start_time, end_time, recur_end_date = formatTime(
curr_timezone,
start_date,
end_time,
end_date)
timezone = 'GMT'
if '-' not in req_timezone:
req_timezone = '+' + req_timezone
else:
req_timezone = timezone + req_timezone
event = createEvent(event_title=event_title,
timezone=req_timezone,
start_time=start_time,
end_time=end_time,
recur_end_date=recur_end_date,
recur_freq=freq)
event = service.events().insert(calendarId='primary', body=event).execute()
# print('Event created: %s' % (event.get('htmlLink')))
return event.get('htmlLink')
if __name__ == '__main__':
master('Google', 'America/Toronto', '-2', datetime.datetime(2021, 1, 17, 9, 30),
datetime.datetime(2021, 1, 17, 10, 30), datetime.datetime(2021, 4, 18, 10, 30), 'WEEKLY')