-
Notifications
You must be signed in to change notification settings - Fork 1
/
santa.py
98 lines (76 loc) · 2.75 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
#!/usr/bin/python
import smtplib
from email.mime.text import MIMEText
import argparse
import os
import json
import random
parser = argparse.ArgumentParser(
description='make e-mail list')
parser.add_argument("--u", required=True, help='e-mail account')
parser.add_argument("--p", required=True, help='e-mail password')
parser.add_argument("--s", required=True, help='e-mail server')
args = parser.parse_args()
class OfficeBoy(object):
def __init__(self, username, password, server):
self.username = username
self.password = password
self.server_address = server
self.recipients = list()
self.msg_text = 'Prepare your gift for '
self.server = smtplib.SMTP(self.server_address)
self.server.starttls()
self.server.login(self.username, self.password)
self.msg = None
def __add_recipient(self, recipient):
self.recipients.append(recipient)
def add_recipients(self):
source_dir = os.getenv("PWD", os.getcwd())
path = os.path.join(source_dir, "santas_list.json")
with open(path, 'rb') as gift_list:
json_list = json.load(gift_list)
recipients_list = json_list["email"]
for r in recipients_list:
self.__add_recipient(r)
def send_email(self, kid):
msg_text = self.msg_text + kid.get_next().email
self.msg = MIMEText(msg_text)
self.msg['From'] = 'Santa Claus'
self.msg['To'] = kid.email
self.msg['Subject'] = 'Santas Anonymous'
self.server.sendmail(self.username, kid.email, self.msg.as_string())
def terminate(self):
self.server.quit()
class Player(object):
def __init__(self, email):
self.email = email
self.gifted = None
def set_next(self, gifted):
self.gifted = gifted
def get_next(self):
return self.gifted
class Shuffle(object):
def __init__(self, emails):
self.players = list()
for email in emails:
player = Player(email)
self.players.append(player)
def get_gifted_players(self):
random.shuffle(self.players)
for i in range(0, len(self.players) - 1):
self.players[i].set_next(self.players[i + 1])
self.players[-1].set_next(self.players[0])
return self.players
if __name__ == '__main__':
try:
santa = OfficeBoy(args.u, args.p, args.s)
santa.add_recipients()
shuffle = Shuffle(santa.recipients)
players = shuffle.get_gifted_players()
with open("roles_backup.txt", 'w+') as f:
for player in players:
santa.send_email(player)
f.write(player.email + '-->' + player.get_next().email + '\n')
finally:
santa.terminate()
f.close()