Compare commits
No commits in common. "7732db81d9d3dd539be922ce24c45909557aa6c6" and "a763c653bddc6069fa1802f32ea1a4e6551da7fc" have entirely different histories.
7732db81d9
...
a763c653bd
3 changed files with 69 additions and 115 deletions
11
config.json
11
config.json
|
@ -1,8 +1,7 @@
|
||||||
{
|
{
|
||||||
"server":"your.email.server",
|
"server":"your.email.server",
|
||||||
"port":587,
|
"port":587,
|
||||||
"user":"you@yourmail.com",
|
"user":"you@yourmail.com",
|
||||||
"pass":"yourPassword",
|
"pass":"yourPassword",
|
||||||
"sender":"santa@yourmail.com",
|
"sender":"santa@yourmail.com"
|
||||||
"price": "75€"
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
[
|
[
|
||||||
{"name":"Alice","mail":"alice@stefansterz.com","gender":"e"},
|
{"name":"Alice","mail":"alice@mail.com","gender":"e"},{"name":"Bob","mail":"bob@mail.com","gender":"er"},{"name":"Chris","mail":"chris@mail.com","gender":"er"}
|
||||||
{"name":"Bob","mail":"bob@stefansterz.com","gender":"er"},
|
|
||||||
{"name":"Chris","mail":"chris@stefansterz.com","gender":"er"}
|
|
||||||
]
|
]
|
||||||
|
|
161
wichteln.py
Executable file → Normal file
161
wichteln.py
Executable file → Normal file
|
@ -1,6 +1,3 @@
|
||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
This script creates randoms pairs from a list of participants in a secret santa
|
This script creates randoms pairs from a list of participants in a secret santa
|
||||||
game and sends them an email to notify them who the person is that they should
|
game and sends them an email to notify them who the person is that they should
|
||||||
|
@ -11,120 +8,80 @@ buy presents for.
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.utils import formatdate, make_msgid
|
from email.utils import formatdate
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import smtplib
|
import smtplib
|
||||||
from typing import Literal
|
|
||||||
import types
|
|
||||||
|
|
||||||
|
parser = ArgumentParser()
|
||||||
|
parser.add_argument('-p', '--participants',
|
||||||
|
dest='pFileLocation',
|
||||||
|
default='participants.json',
|
||||||
|
help='read a list of participants from PARTICIPANTFILE',
|
||||||
|
metavar='PARTICIPANTFILE')
|
||||||
|
parser.add_argument('-c', '--config',
|
||||||
|
dest='cFileLocation',
|
||||||
|
default='config.json',
|
||||||
|
help='read the configuration from CONFIGFILE',
|
||||||
|
metavar='CONFIGFILE')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
def secret_santa_matching(participants: list) -> list:
|
# load and prepare a list containing all participants
|
||||||
# create a copy of the list to make choosing a partner easier
|
with open(args.pFileLocation, 'r') as pFile:
|
||||||
copy = list(enumerate(participants[:]))
|
participants = json.load(pFile)
|
||||||
|
|
||||||
# choose a partner for each participant
|
# create a copy of the list to make choosing a partner easier
|
||||||
for i in range(len(participants)):
|
copy = list(enumerate(participants[:]))
|
||||||
|
|
||||||
# if the last participant has only themselves left to choose, make them
|
# choose a partner for each participant
|
||||||
# switch partners with another random participant
|
for i in range(len(participants)):
|
||||||
if len(copy) == 1 and participants[i] == copy[0][1]:
|
|
||||||
|
|
||||||
current = participants[i]
|
# if the last participant has only themselves left to choose, make them
|
||||||
participants.remove(current)
|
# switch partners with another random participant
|
||||||
partner = random.choice(participants)
|
if len(copy) == 1 and participants[i] == copy[0][1]:
|
||||||
|
|
||||||
current['partner'] = partner['partner']
|
current = participants[i]
|
||||||
partner['partner'] = current['name'] + ' (' + current['mail']+')'
|
participants.remove(current)
|
||||||
participants.append(current)
|
partner = random.choice(participants)
|
||||||
break
|
|
||||||
|
|
||||||
# otherwise choose a random partner for each participant
|
current['partner'] = partner['partner']
|
||||||
else:
|
partner['partner'] = current['name'] + ' (' + current['mail']+')'
|
||||||
partner = random.choice(copy)
|
participants.append(current)
|
||||||
while partner[0] == i:
|
break
|
||||||
partner = random.choice(copy)
|
|
||||||
|
|
||||||
participants[i]['partner'] = partner[1]['name'] + \
|
# otherwise choose a random partner for each participant
|
||||||
' ('+partner[1]['mail']+')'
|
else:
|
||||||
copy.remove(partner)
|
partner = random.choice(copy)
|
||||||
|
while partner[0] == i:
|
||||||
|
partner = random.choice(copy)
|
||||||
|
|
||||||
return participants
|
participants[i]['partner'] = partner[1]['name']+' ('+partner[1]['mail']+')'
|
||||||
|
copy.remove(partner)
|
||||||
|
|
||||||
|
# load mail server configuration and open smtp server connection
|
||||||
|
with open(args.cFileLocation, 'r') as cFile:
|
||||||
|
config = json.load(cFile)
|
||||||
|
|
||||||
def send_secret_santa_mails(participants: list, config: dict):
|
server = smtplib.SMTP(config['server'], config['port'])
|
||||||
open smtp connection
|
server.ehlo()
|
||||||
server = smtplib.SMTP(config['server'], config['port'])
|
server.starttls()
|
||||||
server.ehlo()
|
server.ehlo()
|
||||||
server.starttls()
|
server.login(config['user'], config['pass'])
|
||||||
server.ehlo()
|
|
||||||
server.login(config['user'], config['pass'])
|
|
||||||
|
|
||||||
# create emails for all participants and send them
|
# create emails for all participants and send them
|
||||||
# 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['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)
|
|
||||||
|
|
||||||
plain = f'''Lieb{r['gender']} {r['name']}!
|
msg = MIMEMultipart('alternative')
|
||||||
|
msg['From'] = config['sender']
|
||||||
|
msg['To'] = r['mail']
|
||||||
|
msg['Subject'] = 'Wichteln 🎄'
|
||||||
|
msg['Date'] = formatdate(localtime=True)
|
||||||
|
|
||||||
Du bist heuer Wichtel für {r['partner']}.
|
body = 'Lieb'+r['gender']+' '+r['name']+'!\n\n'
|
||||||
Das Geschenk sollte nicht mehr als {config['price']} kosten.
|
body += 'Du bist heuer Wichtel für '+r['partner']+'.\n'
|
||||||
|
body += 'Das Geschenk sollte nicht mehr als 50€ kosten.\n\n'
|
||||||
|
body += 'Frohe Weihnachten!\n— Dein Christkind 👼'
|
||||||
|
|
||||||
Frohe Weihnachten!
|
msg.attach(MIMEText(body, 'plain', 'utf-8'))
|
||||||
— Dein Christkind 👼'''
|
server.sendmail(config['sender'], r['mail'], msg.as_string())
|
||||||
|
|
||||||
html = f'''
|
|
||||||
<html>
|
|
||||||
<head></head>
|
|
||||||
<body>
|
|
||||||
<p>Lieb{r['gender']} {r['name']}!<br><br>
|
|
||||||
Du bist heuer Wichtel für {r['partner']}.<br>
|
|
||||||
Das Geschenk sollte nicht mehr als {config['price']} kosten.<br><br>
|
|
||||||
Frohe Weihnachten!<br>
|
|
||||||
— Dein Christkind 👼
|
|
||||||
</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
'''
|
|
||||||
|
|
||||||
msg.attach(MIMEText(plain, 'plain', 'utf-8'))
|
|
||||||
msg.attach(MIMEText(html, 'html', 'utf-8'))
|
|
||||||
print('Sending email to', r['name'], '...')
|
|
||||||
server.sendmail(config['sender'], r['mail'], msg.as_string())
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
parser = ArgumentParser()
|
|
||||||
parser.add_argument('-p', '--participants',
|
|
||||||
dest='pFileLocation',
|
|
||||||
default='participants.json',
|
|
||||||
help='read list of participants from PARTICIPANTFILE',
|
|
||||||
metavar='PARTICIPANTFILE')
|
|
||||||
parser.add_argument('-c', '--config',
|
|
||||||
dest='cFileLocation',
|
|
||||||
default='config.json',
|
|
||||||
help='read the configuration from CONFIGFILE',
|
|
||||||
metavar='CONFIGFILE')
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
matches = []
|
|
||||||
|
|
||||||
# load and prepare a list containing all participants
|
|
||||||
with open(args.pFileLocation, 'r') as pFile:
|
|
||||||
matches = secret_santa_matching(json.load(pFile))
|
|
||||||
|
|
||||||
# load mail server configuration
|
|
||||||
with open(args.cFileLocation, 'r') as cFile:
|
|
||||||
config = json.load(cFile)
|
|
||||||
send_secret_santa_mails(matches, config)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
||||||
|
|
Loading…
Reference in a new issue