-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecretSanta.py
More file actions
executable file
·170 lines (131 loc) · 5.29 KB
/
secretSanta.py
File metadata and controls
executable file
·170 lines (131 loc) · 5.29 KB
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
#!/usr/bin/python3
import json
import random
import argparse
import smtplib
import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
class Participant(object):
def __init__(self, name, email):
self.name = name
self.email = email
def __repr__(self):
return self.name
class Exclusion(object):
def __init__(self, user1, user2):
self.user1 = user1
self.user2 = user2
class SecretSanta(object):
def __init__(self, participants, exclusions=[]):
self.participants = participants
self.exclusions = exclusions
def assign(self):
if (len(self.participants) < 2):
print("You must have at least two participants")
exit(1)
sanityCheckCounter = 0
while True:
random.shuffle(participants)
assignments = {}
for i, participant in enumerate(self.participants):
source = participant
target = participants[(i+1) % len(participants)]
assignments[source.email] = target.email
if(self.areAssignmentsAcceptable(assignments)):
return assignments
sanityCheckCounter += 1
if sanityCheckCounter == 10_000:
print('Impossible problem, check exclusions')
exit(1)
def areAssignmentsAcceptable(self, assignments):
for exclusion in self.exclusions:
if assignments[exclusion.user1.email] == exclusion.user2.email:
return False
return True
def sendEmail(message, recipient):
emailCredentials = None
with open("secrets.json") as fin:
emailCredentials = json.load(fin)
subject = f'#SecretSanta{datetime.datetime.now().year} 🎄 ({recipient.name})'
gmail_user = emailCredentials['email']
gmail_pwd = emailCredentials['password']
smtpMessage = MIMEMultipart('alternative')
smtpMessage['Subject'] = subject
smtpMessage['From'] = f'Santa Claus <{gmail_user}>'
smtpMessage['To'] = f'{recipient.name} <{recipient.email}>'
text = message
html = f'''<html><head></head><body>{message}</body></html>'''
part1 = MIMEText(text.encode('utf-8'), 'plain', 'utf-8')
part2 = MIMEText(html.encode('utf-8'), 'html', 'utf-8')
smtpMessage.attach(part1)
smtpMessage.attach(part2)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(gmail_user, [recipient.email], smtpMessage.as_string())
server.close()
print(f'Email sent')
except:
print(f'Failed to send mail to: {recipient}')
raise
def notify(source, target, budget):
if budget is None:
budget = 20 #€
message = f'''
Ciao {source.name},<br />
<br />
Ho bisogno di te!<br />
Come saprai, questo Natale sarà molto particolare. E io, Babbo Natale, lo so bene.<br />
Tra l'età e l'obesità invalidante che mi contraddistingue, non credo che intrufolarmi nei camini sia la scelta più responsabile.<br />
<br />
La tua missione è diventare un portatore sano di regali. E la persona a cui dovrai potrarlo è: <strong>{target.name}</strong> ({target.email})!<br />
Ti ricordo che puoi scegliere un unico consigliere con cui confidarti, e che il budget massimo è di {budget}€.<br />
<br />
Buon lavoro, e che lo spirito dei maglioni brutti sia con te! 😜<br />
<br />
Babbo Natale 🎅<br />
<img src="https://media.giphy.com/media/3o6ZtdulyqqoJjWB6U/giphy.gif" />
'''
# print(f"{source.name} -> {target.name}")
sendEmail(message, source)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Secret Santa Fun!")
parser.add_argument(
'participants', help='File containing csv for participants')
parser.add_argument("-n", "--exclusions",
help="File containing csv for exclusions")
parser.add_argument("-b", "--budget",
help="Maximum budget (in €)")
args = parser.parse_args()
# Check secrets file
if not os.path.exists("secrets.json"):
print("You must have a secrets.json file with email credentials!")
participants = []
with open(args.participants) as fin:
for line in fin:
nameAndEmail = line.rstrip().split(',')
participants.append(Participant(nameAndEmail[0], nameAndEmail[1]))
exclusions = []
if args.exclusions:
with open(args.exclusions) as fin:
for line in fin:
namesPair = line.rstrip().split(',')
participant1 = list(
p for p in participants if p.name == namesPair[0])[0]
participant2 = list(
p for p in participants if p.name == namesPair[1])[0]
exclusions.append(Exclusion(participant1, participant2))
budget = None
if args.budget:
budget = args.budget
ss = SecretSanta(participants, exclusions)
assignments = ss.assign()
# print(json.dumps(assignments))
for participant in participants:
target = [p for p in participants if p.email ==
assignments[participant.email]][0]
notify(participant, target, budget)