fix: refactor script to be more modular and add price to config

This commit is contained in:
Shannon Sterz 2022-10-26 14:32:23 +02:00
parent fb0f8e1777
commit 7732db81d9
Signed by: sterzy
GPG key ID: 2370EE3A271DF05B
3 changed files with 95 additions and 72 deletions

View file

@ -1,7 +1,8 @@
{ {
"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€"
} }

View file

@ -1,3 +1,5 @@
[ [
{"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":"Alice","mail":"alice@stefansterz.com","gender":"e"},
{"name":"Bob","mail":"bob@stefansterz.com","gender":"er"},
{"name":"Chris","mail":"chris@stefansterz.com","gender":"er"}
] ]

View file

@ -15,29 +15,16 @@ from email.utils import formatdate, make_msgid
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()
# load and prepare a list containing all participants def secret_santa_matching(participants: list) -> list:
with open(args.pFileLocation, 'r') as pFile: # create a copy of the list to make choosing a partner easier
participants = json.load(pFile) copy = list(enumerate(participants[:]))
# create a copy of the list to make choosing a partner easier # choose a partner for each participant
copy = list(enumerate(participants[:])) for i in range(len(participants)):
# choose a partner for each participant
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
@ -62,20 +49,20 @@ for i in range(len(participants)):
' ('+partner[1]['mail']+')' ' ('+partner[1]['mail']+')'
copy.remove(partner) copy.remove(partner)
# load mail server configuration and open smtp server connection return participants
with open(args.cFileLocation, 'r') as cFile:
config = json.load(cFile)
server = smtplib.SMTP(config['server'], config['port'])
server.ehlo()
server.starttls()
server.ehlo()
server.login(config['user'], config['pass'])
# create emails for all participants and send them def send_secret_santa_mails(participants: list, config: dict):
# change the text for the mail here if you want to open smtp connection
for r in participants: server = smtplib.SMTP(config['server'], config['port'])
server.ehlo()
server.starttls()
server.ehlo()
server.login(config['user'], config['pass'])
# create emails for all participants and send them
# change the text for the mail here if you want to
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']
@ -85,10 +72,13 @@ for r in participants:
.strip('>')) .strip('>'))
msg['Date'] = formatdate(localtime=True) msg['Date'] = formatdate(localtime=True)
body = 'Lieb'+r['gender']+' '+r['name']+'!\n\n' plain = f'''Lieb{r['gender']} {r['name']}!
body += 'Du bist heuer Wichtel für '+r['partner']+'.\n'
body += 'Das Geschenk sollte nicht mehr als 75€ kosten.\n\n' Du bist heuer Wichtel für {r['partner']}.
body += 'Frohe Weihnachten!\n— Dein Christkind 👼' Das Geschenk sollte nicht mehr als {config['price']} kosten.
Frohe Weihnachten!
Dein Christkind 👼'''
html = f''' html = f'''
<html> <html>
@ -96,7 +86,7 @@ for r in participants:
<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 75 kosten.<br><br> Das Geschenk sollte nicht mehr als {config['price']} kosten.<br><br>
Frohe Weihnachten!<br> Frohe Weihnachten!<br>
Dein Christkind 👼 Dein Christkind 👼
</p> </p>
@ -104,7 +94,37 @@ for r in participants:
</html> </html>
''' '''
msg.attach(MIMEText(body, 'plain', 'utf-8')) msg.attach(MIMEText(plain, '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())
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()