-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_email.py
97 lines (81 loc) · 3.78 KB
/
send_email.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
import datetime
import numpy as np
from googleapiclient.discovery import build
from google.oauth2 import service_account
from dateutil.relativedelta import relativedelta, MO
import utils
import os
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
SCOPES = ["https://www.googleapis.com/auth/calendar"]
def send_invites(email, np_array, start_date, calendar_name, calendar_location, calendar_description):
# Ensure start_date is a Monday
if start_date.weekday() != 0:
start_date = start_date + relativedelta(weekday=MO)
# creds = service_account.Credentials.from_service_account_file(
# "credentials.json", scopes=SCOPES
# )
# service = build('calendar', 'v3', credentials=creds)
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'local_oauth_secret_SPA.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('calendar', 'v3', credentials=creds)
for i in range(np_array.shape[0]):
j = 0
while j < np_array.shape[1]:
if np_array[i, j] == 1:
# Calculate date and time for the event
event_date = start_date + datetime.timedelta(days=i)
start_time = datetime.time(9+j, 0)
while j < np_array.shape[1] and np_array[i, j] == 1:
j += 1
end_time = datetime.time(9+j, 0)
# Create the event
event = {
'summary': calendar_name,
'location': calendar_location,
'description': calendar_description,
'start': {
'dateTime': datetime.datetime.combine(event_date, start_time).isoformat(),
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': datetime.datetime.combine(event_date, end_time).isoformat(),
'timeZone': 'America/Los_Angeles',
},
'attendees': [
{'email': email},
],
'reminders': {
'useDefault': True,
}
}
# Call the Calendar API
# event = service.events().insert(body=event, calendarId='primary', sendUpdates='all').execute()
event = service.events().insert(body=event, calendarId='c_9b67633e6949a3032947c6bb8988c4ed56af00c18d8db1ced57ade44b9cea252@group.calendar.google.com', sendUpdates='all').execute()
print(f"Email sent for {email}")
j += 1
if __name__ == "__main__":
timeslots = np.zeros((5, 12)) # Random example array
timeslots[1][1] = 1
timeslots[1][2] = 1
monday_date = utils.nearest_future_monday('2024-01-21')
print(timeslots)
send_invites('kevin.x.han@berkeley.edu', timeslots, monday_date, "OH time", "warren", "damn I love this OH")