forked from jagui/secret-santa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecret_santa.py
300 lines (247 loc) · 9.54 KB
/
secret_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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import datetime
import getopt
import os
import random
# sudo pip install pyyaml
import re
import smtplib
import socket
import sys
import time
import pytz
import yaml
help_message = """
To use, fill out config.yml with your own participants. You can also specify
DONT_PAIR so that people don't get assigned their significant other.
You'll also need to specify your mail server settings. An example is provided
for routing mail through gmail.
For more information, see README.
-s,--send :: Send the emails with optionally specified RNG_SEED in config.yaml
-h,--help :: Show this message on usage
-r,--reveal :: Reveal all pairings with optionally specified RNG_SEED in config.yaml
-g,--getassignment :: Get assignments for one or more individual people.
"""
CONFIG_REQRD = (
"SMTP_SERVER",
"SMTP_PORT",
"SMTP_USERNAME",
"SMTP_PASSWORD",
"TIMEZONE",
"PARTICIPANTS",
"DONT_PAIR",
"FROM",
"SUBJECT",
"MESSAGE",
)
CONFIG_OPTIONAL = {
"SMTP_SECURITY" : "SSL",
"RNG_SEED" : random.randrange(sys.maxsize)
}
HEADER = """Date: {date}
Content-Type: text/plain; charset="utf-8"
From: {frm}
To: {to}
Subject: {subject}
"""
CONFIG_PATH = os.path.join(os.path.dirname(__file__), "config.yml")
class Person:
def __init__(self, name, email, invalid_matches):
self.name = name
self.email = email
self.invalid_matches = invalid_matches
def __str__(self):
return "%s <%s>" % (self.name, self.email)
class Pair:
def __init__(self, giver, receiver):
self.giver = giver
self.receiver = receiver
def __str__(self):
return "%s ---> %s" % (self.giver.name, self.receiver.name)
def parse_yaml(yaml_path=CONFIG_PATH):
return yaml.load(open(yaml_path), Loader=yaml.FullLoader)
def choose_receiver(giver, receivers):
choice = random.choice(receivers)
if choice.name in giver.invalid_matches or giver.name == choice.name:
if len(receivers) is 1:
raise Exception("Only one receiver left, try again")
return choose_receiver(giver, receivers)
else:
return choice
def create_pairs(g, r):
givers = g[:]
receivers = r[:]
pairs = []
for giver in givers:
try:
receiver = choose_receiver(giver, receivers)
receivers.remove(receiver)
pairs.append(Pair(giver, receiver))
except:
return create_pairs(g, r)
return pairs
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "shrgc", ["send", "help", "reveal","getassignment"])
except getopt.error as msg:
raise Usage(msg)
# option processing
send = False
revealPairs = False
getAssignment = False
for option, value in opts:
if option in ("-s", "--send"):
send = True
if option in ("-h", "--help"):
raise Usage(help_message)
if option in ("-r", "--reveal"):
revealPairs = True
if option in ("-g", "--getassignment"):
getAssignment = True
config = parse_yaml()
for key in CONFIG_REQRD:
if key not in config.keys():
raise Exception(
"Required parameter {} not in yaml config file!".format(key)
)
for key, val in CONFIG_OPTIONAL.items():
if key not in config.keys():
config.update({key:val})
print("Optional parameter {} not in yaml config file, using default of {}".format(key,str(val)))
#Setup the RNG with a seed value for repeatable draws (if desired)
if str(config["RNG_SEED"]).upper() == "YEAR":
zone = pytz.timezone(config["TIMEZONE"])
now = zone.localize(datetime.datetime.now())
config["RNG_SEED"] = int(now.strftime('%Y'))
random.seed(int(config["RNG_SEED"]))
participants = config["PARTICIPANTS"]
dont_pair = config["DONT_PAIR"]
if len(participants) < 2:
raise Exception("Not enough participants specified.")
givers = []
for person in participants:
name, email = re.match(r"([^<]*)<([^>]*)>", person).groups()
name = name.strip()
invalid_matches = []
for pair in dont_pair:
names = [n.strip() for n in pair.split(",")]
if name in names:
# is part of this pair
for member in names:
if name != member:
invalid_matches.append(member)
person = Person(name, email, invalid_matches)
givers.append(person)
receivers = givers[:]
pairs = create_pairs(givers, receivers)
if not send and not getAssignment:
print(
"""
Test pairings:
{}
To send out emails with new pairings,
call with the --send argument:
$ python secret_santa.py --send
""".format("\n".join([str(p) for p in pairs]))
)
if getAssignment:
print('*'*25)
print('Get Assignment Mode')
print('*'*25)
#print("\n".join([str(p.giver) for p in pairs]))
i = 0
tempPairArray = []
for p in pairs:
print("{}){}".format(i,p.giver.name))
tempPairArray.append(p)
i+=1
print("*"*25)
giverNumber = int(input('Which person do you want to get assignment for?\r\nEnter number here: '))
print('You entered : {} - {}'.format(giverNumber,tempPairArray[giverNumber].giver.name))
selectedPair = tempPairArray[giverNumber]
print("*"*25)
print('Do you want to (r)esend assignment or (d)isplay assignment for {}" '.format(selectedPair.giver.name))
print("Enter 'r' to resend")
print("Enter 'd' to display")
action = str(input('Desired Action: ')).upper()
if action == "D":
print("*"*25)
print("{santa} has been assigned {santee} to buy a gift for".format(
santa=selectedPair.giver.name, santee=selectedPair.receiver.name)
)
print("*"*25)
sys.exit(0)
elif action == "R":
confirmation = "N"
print("*"*25)
print("Enter 'y' for yes")
print("Enter 'n' for no")
confirmation = str(input('Are you sure?: ')).upper()
if confirmation == 'Y':
send = True
print("Resending paring for {}".format(selectedPair.giver.name))
pairs = []
pairs.append(selectedPair)
#print("\n".join([str(p) for p in pairs]))
else:
print("OK, exiting script. No emails resent")
sys.exit(0)
else:
print("Invalid choice. Rerun script and try again")
sys.exit(1)
if send:
if str(config["SMTP_SECURITY"]).upper() == "TLS":
server = smtplib.SMTP(config["SMTP_SERVER"], config["SMTP_PORT"])
server.ehlo()
server.starttls()
else:
server = smtplib.SMTP_SSL(config["SMTP_SERVER"], config["SMTP_PORT"])
server.login(config["SMTP_USERNAME"], config["SMTP_PASSWORD"])
for pair in pairs:
zone = pytz.timezone(config["TIMEZONE"])
now = zone.localize(datetime.datetime.now())
date = now.strftime("%a, %d %b %Y %T %Z") # Sun, 21 Dec 2008 06:25:23 +0000
frm = config["FROM"]
to = pair.giver.email
subject = config["SUBJECT"].format(
santa=pair.giver.name, santee=pair.receiver.name, year=int(now.strftime('%Y'))
)
msgbody=re.sub(r'(?<!\r)\n',
r'\r\n',
config["MESSAGE"],
flags=re.MULTILINE)
body = (HEADER + "\n\n" + msgbody).format(
date=date,
frm=frm,
to=to,
subject=subject,
santa=pair.giver.name,
santee=pair.receiver.name,
year=now.strftime('%Y')
)
if send:
server.sendmail(frm, [to], body.encode('utf8'))
print("Emailed {} <{}>".format(pair.giver.name, to))
if send:
server.quit()
print("""\nNOTE: The RNG seed value was: {}
Write this number down somewhere, if you ever want to regenerate this exact draw (for re-sending pairings, etc).
""".format(config["RNG_SEED"]))
if send and revealPairs:
print("You chosen to reveal the pairings, are you sure?")
selection = input("Enter 'Y' to reveal, anything else to skip: ")
if str(selection[0]).upper() == "Y":
print("\n".join([str(p) for p in pairs]))
else:
print("Reveal aborted, game integrity preserved.")
except Usage as err:
print(sys.argv[0].split("/")[-1] + ": " + str(err.msg))
print("\t for help use --help")
return 2
if __name__ == "__main__":
sys.exit(main())