-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamigo.py
executable file
·123 lines (95 loc) · 3.19 KB
/
amigo.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
#!/usr/bin/python
import smtplib
import random
import sys
class Player(object):
name = ""
mail = ""
msg = ""
def to_string(self):
return self.name + ": " + self.mail
# The class "constructor" - It's actually an initializer
def __init__(self, name, mail):
self.name = name
self.mail = mail
class Pairplayer(object):
playerfrom = Player("", "")
playerto = Player("", "")
def to_string(self):
return "From: " + self.playerfrom.to_string() + " to: " + self.playerto.to_string()
# The class "constructor" - It's actually an initializer
def __init__(self, playerfrom, playerto):
self.playerfrom = playerfrom
self.playerto = playerto
dictionary_from = {}
dictionary_to = {}
text = ""
subject = "Amigo invisible 2018"
username = "manoinocenteavila@gmail.com"
password = "password"
def fill_dictionary(arg):
global dictionary_from
wo_new_line = arg[:-1]
friend_list = wo_new_line.split(':', 1)
dictionary_from[friend_list[0]] = friend_list[1]
def read_list(name):
file = open(name, 'rw')
print "Reading list"
for line in file:
fill_dictionary(line)
global dictionary_to
dictionary_to = dictionary_from.copy()
print "List readed"
def mix_names():
print "Mixing names"
length = len(dictionary_from)
i = 0
time = 0
list_players = []
while i < length:
namefrom = random.choice(dictionary_from.keys())
mailfrom = dictionary_from[namefrom]
playerfrom = Player(namefrom, mailfrom)
nameto = random.choice(dictionary_to.keys())
mailto = dictionary_to[nameto]
playerto = Player(nameto, mailto)
if mailto != mailfrom:
del dictionary_to[nameto]
del dictionary_from[namefrom]
new_player = Pairplayer(playerfrom, playerto)
list_players.append(new_player)
i = i + 1
time = time + 1
if time == (length * 3):
print "Error, you have to repeat the process. A person has been matched with himself/herself"
sys.exit()
print "Names mixed"
return list_players
def send_mail(players):
server = create_server_connection()
i = 1
print "Sending mails"
for value in players:
value.playerfrom.msg = text.replace('[namefrom]', value.playerfrom.name).replace('[nameto]',
value.playerto.name)
message = 'Subject: {}\n\n{}'.format(subject, value.playerfrom.msg)
server.sendmail(username, value.playerfrom.mail, message)
print "The " + str(i) + " mail has been sent"
i = i + 1
server.quit()
print "All mails have been sent"
def create_server_connection():
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.ehlo()
server.login(username, password)
return server
if __name__ == "__main__":
if len(sys.argv) > 2:
f = open(str(sys.argv[2]), 'rw')
text = f.read();
read_list(str(sys.argv[1]))
list_players = mix_names()
send_mail(list_players)
else:
print "Script not properly executed: amigo.py [list_path] [text]"