2022-10-26 14:04:04 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-09-08 23:02:29 +02:00
|
|
|
'''
|
|
|
|
This script creates randoms pairs from a list of participants in a secret santa
|
2018-09-09 13:01:28 +02:00
|
|
|
game and sends them an email to notify them who the person is that they should
|
2018-09-08 23:02:29 +02:00
|
|
|
buy presents for.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
2022-10-26 14:05:29 +02:00
|
|
|
from email.utils import formatdate, make_msgid
|
2018-09-08 16:08:49 +02:00
|
|
|
import json
|
|
|
|
import random
|
2018-09-08 23:02:29 +02:00
|
|
|
import smtplib
|
|
|
|
|
|
|
|
parser = ArgumentParser()
|
|
|
|
parser.add_argument('-p', '--participants',
|
|
|
|
dest='pFileLocation',
|
|
|
|
default='participants.json',
|
2018-09-09 22:37:01 +02:00
|
|
|
help='read a list of participants from PARTICIPANTFILE',
|
2018-09-08 23:02:29 +02:00
|
|
|
metavar='PARTICIPANTFILE')
|
|
|
|
parser.add_argument('-c', '--config',
|
|
|
|
dest='cFileLocation',
|
|
|
|
default='config.json',
|
2018-09-09 22:37:01 +02:00
|
|
|
help='read the configuration from CONFIGFILE',
|
2018-09-08 23:02:29 +02:00
|
|
|
metavar='CONFIGFILE')
|
|
|
|
args = parser.parse_args()
|
2018-09-08 16:08:49 +02:00
|
|
|
|
|
|
|
# load and prepare a list containing all participants
|
2018-09-08 23:02:29 +02:00
|
|
|
with open(args.pFileLocation, 'r') as pFile:
|
2022-10-26 14:08:21 +02:00
|
|
|
participants = json.load(pFile)
|
2018-09-08 16:08:49 +02:00
|
|
|
|
|
|
|
# create a copy of the list to make choosing a partner easier
|
|
|
|
copy = list(enumerate(participants[:]))
|
|
|
|
|
|
|
|
# choose a partner for each participant
|
|
|
|
for i in range(len(participants)):
|
|
|
|
|
2022-10-26 14:08:21 +02:00
|
|
|
# if the last participant has only themselves left to choose, make them
|
|
|
|
# switch partners with another random participant
|
|
|
|
if len(copy) == 1 and participants[i] == copy[0][1]:
|
2018-09-08 16:08:49 +02:00
|
|
|
|
2022-10-26 14:08:21 +02:00
|
|
|
current = participants[i]
|
|
|
|
participants.remove(current)
|
|
|
|
partner = random.choice(participants)
|
2018-09-08 16:08:49 +02:00
|
|
|
|
2022-10-26 14:08:21 +02:00
|
|
|
current['partner'] = partner['partner']
|
|
|
|
partner['partner'] = current['name'] + ' (' + current['mail']+')'
|
|
|
|
participants.append(current)
|
|
|
|
break
|
2018-09-08 16:08:49 +02:00
|
|
|
|
2022-10-26 14:08:21 +02:00
|
|
|
# otherwise choose a random partner for each participant
|
|
|
|
else:
|
|
|
|
partner = random.choice(copy)
|
|
|
|
while partner[0] == i:
|
|
|
|
partner = random.choice(copy)
|
2018-09-08 16:08:49 +02:00
|
|
|
|
2022-10-26 14:08:21 +02:00
|
|
|
participants[i]['partner'] = partner[1]['name'] + \
|
|
|
|
' ('+partner[1]['mail']+')'
|
|
|
|
copy.remove(partner)
|
2018-09-08 16:08:49 +02:00
|
|
|
|
2018-09-08 23:02:29 +02:00
|
|
|
# load mail server configuration and open smtp server connection
|
|
|
|
with open(args.cFileLocation, 'r') as cFile:
|
2022-10-26 14:08:21 +02:00
|
|
|
config = json.load(cFile)
|
|
|
|
|
2018-09-08 23:02:29 +02:00
|
|
|
server = smtplib.SMTP(config['server'], config['port'])
|
|
|
|
server.ehlo()
|
|
|
|
server.starttls()
|
|
|
|
server.ehlo()
|
|
|
|
server.login(config['user'], config['pass'])
|
2018-09-08 16:08:49 +02:00
|
|
|
|
2018-09-08 23:02:29 +02:00
|
|
|
# create emails for all participants and send them
|
|
|
|
# change the text for the mail here if you want to
|
|
|
|
for r in participants:
|
2022-10-26 14:08:21 +02:00
|
|
|
|
|
|
|
msg = MIMEMultipart('alternative')
|
|
|
|
msg['From'] = config['sender']
|
|
|
|
msg['To'] = r['mail']
|
|
|
|
msg['Subject'] = 'Wichteln 🎄'
|
|
|
|
msg['Message-ID'] = make_msgid(domain=config['sender']
|
|
|
|
.split('@')[1]
|
|
|
|
.strip('>'))
|
|
|
|
msg['Date'] = formatdate(localtime=True)
|
|
|
|
|
|
|
|
body = 'Lieb'+r['gender']+' '+r['name']+'!\n\n'
|
|
|
|
body += 'Du bist heuer Wichtel für '+r['partner']+'.\n'
|
|
|
|
body += 'Das Geschenk sollte nicht mehr als 75€ kosten.\n\n'
|
|
|
|
body += 'Frohe Weihnachten!\n— Dein Christkind 👼'
|
|
|
|
|
2022-10-26 14:09:42 +02:00
|
|
|
html = f'''
|
2022-10-26 14:05:29 +02:00
|
|
|
<html>
|
|
|
|
<head></head>
|
|
|
|
<body>
|
|
|
|
<p>Lieb{r['gender']} {r['name']}!<br><br>
|
|
|
|
Du bist heuer Wichtel für {r['partner']}.<br>
|
2022-10-26 14:08:21 +02:00
|
|
|
Das Geschenk sollte nicht mehr als 75€ kosten.<br><br>
|
2022-10-26 14:05:29 +02:00
|
|
|
Frohe Weihnachten!<br>
|
|
|
|
— Dein Christkind 👼
|
|
|
|
</p>
|
|
|
|
</body>
|
|
|
|
</html>
|
2022-10-26 14:09:42 +02:00
|
|
|
'''
|
2022-10-26 14:08:21 +02:00
|
|
|
|
|
|
|
msg.attach(MIMEText(body, 'plain', 'utf-8'))
|
|
|
|
msg.attach(MIMEText(html, 'html', 'utf-8'))
|
2022-10-26 14:09:42 +02:00
|
|
|
print('Sending email to', r['name'], '...')
|
2022-10-26 14:08:21 +02:00
|
|
|
server.sendmail(config['sender'], r['mail'], msg.as_string())
|