forked from kossiitkgp/mailing-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar-invite.py
213 lines (176 loc) · 7.63 KB
/
calendar-invite.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
import csv
import getpass
import sys
import os
import base64
import re
from email.utils import formataddr
from email.header import Header
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from templates.variable_mappings import variable_column_mapping
# Help and number of argument passed checker
if len(sys.argv) < 5:
#DONT USE <LOBBY LINK VARIABLE>
print("USAGE: python3 calendar-invite.py <template_file> <csv_file> <include_meet> <slot_time> (OPTIONAL)<variables_with_same_value_for_all_mails>")
print('python3 calendar-invite.py selections/onboarding onboarding.csv YES slot_time="Tuesday, 3 June 2023, 10:00 PM - 11:00 PM"')
print("python3 calendar-invite.py selections/onboarding onboarding.csv NO slot_time='Tuesday, 2 February 2030, 8:00 AM - 1:00 PM' number_of_applicants='250+'")
sys.exit(1)
# If modifying SCOPES, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/gmail.send", "https://www.googleapis.com/auth/calendar.events"]
# Various files being used
template_file = "./templates/" + sys.argv[1]
csv_file = "./csv/" + sys.argv[2]
signature_file = "./templates/signature"
hasMeet = (sys.argv[3].lower() == 'yes')
# Getting subject and mail body
lines = []
with open(template_file, "r") as file:
subject_template = file.readline().strip()
lines = file.readlines()[1:] # Slice the list starting from index 2 (line number 3)
email_body_template = "".join(lines)
# Getting signature
with open(signature_file) as file:
signature = file.read()
def convert_to_RFC3339(input):
lookUp={'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6, 'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12}
chunks = re.findall(r"[\w']+",input)
chunks[2] = lookUp[chunks[2]]
if chunks[6]=='PM':
chunks[4]=int(chunks[4])+12
if chunks[9]=='PM':
chunks[7]=int(chunks[7])+12
chunks[4]=str(chunks[4]).zfill(2)
chunks[7]=str(chunks[7]).zfill(2)
chunks[1]=str(chunks[1]).zfill(2)
chunks[2]=str(chunks[2]).zfill(2)
return [f"{chunks[3]}-{chunks[2]}-{chunks[1]}T{chunks[4]}:{chunks[5]}:00",f"{chunks[3]}-{chunks[2]}-{chunks[1]}T{chunks[7]}:{chunks[8]}:00"]
def create_event(sender, email_list, subject, message, timeRange, has_meet):
formatted_sender = formataddr((str(Header('KOSS IIT Kharagpur', 'utf-8')), sender))
attendee_data = []
for address in email_list:
attendee_data.append({'email': address})
if(has_meet):
event = {
'summary': f'{subject}',
'description': f'{message}',
'start': {
'dateTime': f'{timeRange[0]}+05:30',
'timeZone': 'Asia/Kolkata'
},
'end': {
'dateTime': f'{timeRange[1]}+05:30',
'timeZone': 'Asia/Kolkata'
},
'attendees': attendee_data,
'reminders': {
'useDefault': False,
'overrides': [
#email reminder 1 day before event
{'method': 'email', 'minutes': 24 * 60},
#pop-up reminder 10 minutes before event
{'method': 'popup', 'minutes': 10},
],
},
'guestsCanSeeOtherGuests': False,
'guestsCanInviteOthers': False,
"conferenceData": {"createRequest": {"requestId": "sample123", "conferenceSolutionKey": {"type": "hangoutsMeet"}}}
}
return event
event = {
'summary': f'{subject}',
'description': f'{message}',
'start': {
'dateTime': f'{timeRange[0]}+05:30',
'timeZone': 'Asia/Kolkata'
},
'end': {
'dateTime': f'{timeRange[1]}+05:30',
'timeZone': 'Asia/Kolkata'
},
'attendees': attendee_data,
'reminders': {
'useDefault': False,
'overrides': [
#email reminder 1 day before event
{'method': 'email', 'minutes': 24 * 60},
#pop-up reminder 10 minutes before event
{'method': 'popup', 'minutes': 10},
],
},
'guestsCanSeeOtherGuests': False,
'guestsCanInviteOthers': False,
}
return event
def send_event(service, event):
event = service.events().insert(calendarId='primary', body=event, sendNotifications=True,sendUpdates='all',conferenceDataVersion=1).execute()
print ('Event created: %s' % (event.get('htmlLink')))
print ('Meet link: %s' % (event.get('hangoutLink')))
return event
def fill_variables(content, variables):
for variable, value in variables.items():
placeholder = "{" + variable + "}"
content = content.replace(placeholder, value)
return content
def validate_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if re.match(pattern, email):
return True
else:
return False
def main(subject_template, email_body_template, signature):
creds = None
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
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(
"credentials.json", SCOPES
)
creds = flow.run_local_server(port=0)
with open("token.json", "w") as token:
token.write(creds.to_json())
service = build("gmail", "v1", credentials=creds)
serviceCal = build("calendar", "v3", credentials=creds)
timeSlot=[]
# Getting extra static variables(those which are same for all mails) if required by the template - from arguments
if len(sys.argv) > 4:
variables = {}
for arg in sys.argv[4:]:
variable, value = arg.split("=")
variables[variable] = value.strip()
if 'slot_time' in variables:
timeSlot=convert_to_RFC3339(variables['slot_time'])
email_body_template = fill_variables(email_body_template, variables)
subject_template = fill_variables(subject_template, variables)
sender = "[email protected]"
with open(csv_file, newline="") as file:
reader = csv.DictReader(file)
emailList=[]
for row in reader:
# Getting unique variables values - from the CSV file
required_columns = set([column for variables in variable_column_mapping.values() for column in variables if column in row])
variables = {}
for variable, columns in variable_column_mapping.items():
for column in columns:
if column in required_columns:
variables[variable] = row.get(column, "").strip()
break
email = variables['email'].strip()
if not validate_email(email):
print(f'Invalid mail provided: {email}')
continue
emailList.append(email)
# calendar invites cannot have personalised details
email_body = email_body_template
subject = subject_template
email_content = email_body + signature
event_obj = create_event(sender,emailList,subject,email_content,timeSlot,hasMeet)
send_event(serviceCal,event_obj)
print("Script execution completed.")
if __name__ == "__main__":
main(subject_template, email_body_template, signature)