chore: add proper code formatting
This commit is contained in:
parent
b61d1dd8e2
commit
1bea9f70c3
1 changed files with 42 additions and 41 deletions
75
wichteln.py
75
wichteln.py
|
@ -31,7 +31,7 @@ args = parser.parse_args()
|
||||||
|
|
||||||
# load and prepare a list containing all participants
|
# load and prepare a list containing all participants
|
||||||
with open(args.pFileLocation, 'r') as pFile:
|
with open(args.pFileLocation, 'r') as pFile:
|
||||||
participants = json.load(pFile)
|
participants = json.load(pFile)
|
||||||
|
|
||||||
# create a copy of the list to make choosing a partner easier
|
# create a copy of the list to make choosing a partner easier
|
||||||
copy = list(enumerate(participants[:]))
|
copy = list(enumerate(participants[:]))
|
||||||
|
@ -39,31 +39,32 @@ copy = list(enumerate(participants[:]))
|
||||||
# choose a partner for each participant
|
# choose a partner for each participant
|
||||||
for i in range(len(participants)):
|
for i in range(len(participants)):
|
||||||
|
|
||||||
# if the last participant has only themselves left to choose, make them
|
# if the last participant has only themselves left to choose, make them
|
||||||
# switch partners with another random participant
|
# switch partners with another random participant
|
||||||
if len(copy) == 1 and participants[i] == copy[0][1]:
|
if len(copy) == 1 and participants[i] == copy[0][1]:
|
||||||
|
|
||||||
current = participants[i]
|
current = participants[i]
|
||||||
participants.remove(current)
|
participants.remove(current)
|
||||||
partner = random.choice(participants)
|
partner = random.choice(participants)
|
||||||
|
|
||||||
current['partner'] = partner['partner']
|
current['partner'] = partner['partner']
|
||||||
partner['partner'] = current['name'] + ' (' + current['mail']+')'
|
partner['partner'] = current['name'] + ' (' + current['mail']+')'
|
||||||
participants.append(current)
|
participants.append(current)
|
||||||
break
|
break
|
||||||
|
|
||||||
# otherwise choose a random partner for each participant
|
# otherwise choose a random partner for each participant
|
||||||
else:
|
else:
|
||||||
partner = random.choice(copy)
|
partner = random.choice(copy)
|
||||||
while partner[0] == i:
|
while partner[0] == i:
|
||||||
partner = random.choice(copy)
|
partner = random.choice(copy)
|
||||||
|
|
||||||
participants[i]['partner'] = partner[1]['name']+' ('+partner[1]['mail']+')'
|
participants[i]['partner'] = partner[1]['name'] + \
|
||||||
copy.remove(partner)
|
' ('+partner[1]['mail']+')'
|
||||||
|
copy.remove(partner)
|
||||||
|
|
||||||
# load mail server configuration and open smtp server connection
|
# load mail server configuration and open smtp server connection
|
||||||
with open(args.cFileLocation, 'r') as cFile:
|
with open(args.cFileLocation, 'r') as cFile:
|
||||||
config = json.load(cFile)
|
config = json.load(cFile)
|
||||||
|
|
||||||
server = smtplib.SMTP(config['server'], config['port'])
|
server = smtplib.SMTP(config['server'], config['port'])
|
||||||
server.ehlo()
|
server.ehlo()
|
||||||
|
@ -75,27 +76,27 @@ server.login(config['user'], config['pass'])
|
||||||
# change the text for the mail here if you want to
|
# change the text for the mail here if you want to
|
||||||
for r in participants:
|
for r in participants:
|
||||||
|
|
||||||
msg = MIMEMultipart('alternative')
|
msg = MIMEMultipart('alternative')
|
||||||
msg['From'] = config['sender']
|
msg['From'] = config['sender']
|
||||||
msg['To'] = r['mail']
|
msg['To'] = r['mail']
|
||||||
msg['Subject'] = 'Wichteln 🎄'
|
msg['Subject'] = 'Wichteln 🎄'
|
||||||
msg['Message-ID'] = make_msgid(domain=config['sender']
|
msg['Message-ID'] = make_msgid(domain=config['sender']
|
||||||
.split('@')[1]
|
.split('@')[1]
|
||||||
.strip('>'))
|
.strip('>'))
|
||||||
msg['Date'] = formatdate(localtime=True)
|
msg['Date'] = formatdate(localtime=True)
|
||||||
|
|
||||||
body = 'Lieb'+r['gender']+' '+r['name']+'!\n\n'
|
body = 'Lieb'+r['gender']+' '+r['name']+'!\n\n'
|
||||||
body += 'Du bist heuer Wichtel für '+r['partner']+'.\n'
|
body += 'Du bist heuer Wichtel für '+r['partner']+'.\n'
|
||||||
body += 'Das Geschenk sollte nicht mehr als 50€ kosten.\n\n'
|
body += 'Das Geschenk sollte nicht mehr als 75€ kosten.\n\n'
|
||||||
body += 'Frohe Weihnachten!\n— Dein Christkind 👼'
|
body += 'Frohe Weihnachten!\n— Dein Christkind 👼'
|
||||||
|
|
||||||
html = f"""
|
html = f"""
|
||||||
<html>
|
<html>
|
||||||
<head></head>
|
<head></head>
|
||||||
<body>
|
<body>
|
||||||
<p>Lieb{r['gender']} {r['name']}!<br><br>
|
<p>Lieb{r['gender']} {r['name']}!<br><br>
|
||||||
Du bist heuer Wichtel für {r['partner']}.<br>
|
Du bist heuer Wichtel für {r['partner']}.<br>
|
||||||
Das Geschenk sollte nicht mehr als 50€ kosten.<br><br>
|
Das Geschenk sollte nicht mehr als 75€ kosten.<br><br>
|
||||||
Frohe Weihnachten!<br>
|
Frohe Weihnachten!<br>
|
||||||
— Dein Christkind 👼
|
— Dein Christkind 👼
|
||||||
</p>
|
</p>
|
||||||
|
@ -103,7 +104,7 @@ for r in participants:
|
||||||
</html>
|
</html>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
msg.attach(MIMEText(body, 'plain', 'utf-8'))
|
msg.attach(MIMEText(body, 'plain', 'utf-8'))
|
||||||
msg.attach(MIMEText(html, 'html', 'utf-8'))
|
msg.attach(MIMEText(html, 'html', 'utf-8'))
|
||||||
print("Sending email to", r['name'])
|
print("Sending email to", r['name'])
|
||||||
server.sendmail(config['sender'], r['mail'], msg.as_string())
|
server.sendmail(config['sender'], r['mail'], msg.as_string())
|
||||||
|
|
Loading…
Reference in a new issue