-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanta.py
99 lines (85 loc) · 2.92 KB
/
santa.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
from random import randint
import smtplib
def initialize():
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
print("[username]@gmail.com")
username = input("Username: ")
username = username.strip() + '@gmail.com'
password = input("Password: ")
server.login(username,password)
return username, server
def santa(sheetname): #.tsv plz
sheet = open(sheetname)
give = []
receive = []
final = []
sheet.readline()
for line in sheet:
trimmed = tuple(line.strip().split('\t')[1:4])
give.append(trimmed)
receive.append(trimmed)
#print(receive)
for i in range(len(give)):
if len(give) == 1:
if give[0][1] == receive[0][1]:
print("Edgecase")
raise # Not necessary but to be safe
old = final[0]
name = give[0][0].split(' ')[0]
new = (name, give[0][1],receive[0],receive[2])
swapped1 = (old[0],old[1],new[2],new[3])
swapped2 = (new[0],new[1],old[2],new[3])
final.pop(0)
final.append(swapped1,swapped2)
else:
name = give[0][0].split(' ')[0]
final.append((name, give[0][1],receive[0][0],receive[0][2]))
give.pop(0)
receive.pop(0)
break
proper = False
while not proper:
r = randint(0,len(receive)-1)
#print("Length:"+str(len(receive)))
#print("r:"+str(r))
if give[0][1] != receive[r][1]:
proper = True
name = give[0][0].split(' ')[0]
final.append((name, give[0][1],receive[r][0],receive[r][2]))
give.pop(0)
receive.pop(r)
return final
def email_santa(username, server, santa_list):
FROM = username
for element in santa_list:
TO = [element[1]]
msg = '\r\n'.join([
"From: "+FROM,
"To: "+", ".join(TO),
"Subject: Secret Santa Assignment",
"",
"""Hey {},
Thanks for participating in Secret Santa! You've been assigned {} to give a gift to.
Details they wished to share:
"{}"
Remember that the gift exchange will occur on December 14th (around 12:30pm), and that max gift cost is $15.
Thanks again, and good luck with exams!
Emerson
(P.S. This email was autogenerated, if you have any concerns please let me know!)
""".format(element[0],element[2],element[3])
])
try:
server.sendmail(FROM, TO, msg)
except Exception as e:
print("Error")
print(e)
print("msg")
print(str(msg))
server.close()
print("Emails sent")
if __name__ == '__main__':
username, server = initialize()
santa_list = santa("santa.tsv") #Ensure ascii-compliant symbols!
email_santa(username, server, santa_list)