From 7732db81d9d3dd539be922ce24c45909557aa6c6 Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Wed, 26 Oct 2022 14:32:23 +0200 Subject: [PATCH] fix: refactor script to be more modular and add price to config --- config.json | 11 ++-- participants.json | 4 +- wichteln.py | 152 ++++++++++++++++++++++++++-------------------- 3 files changed, 95 insertions(+), 72 deletions(-) diff --git a/config.json b/config.json index 9514eaa..dd1c212 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,8 @@ { -"server":"your.email.server", -"port":587, -"user":"you@yourmail.com", -"pass":"yourPassword", -"sender":"santa@yourmail.com" + "server":"your.email.server", + "port":587, + "user":"you@yourmail.com", + "pass":"yourPassword", + "sender":"santa@yourmail.com", + "price": "75€" } diff --git a/participants.json b/participants.json index 1c9180b..852b79b 100644 --- a/participants.json +++ b/participants.json @@ -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"} ] diff --git a/wichteln.py b/wichteln.py index 2948cb9..9722439 100755 --- a/wichteln.py +++ b/wichteln.py @@ -15,88 +15,78 @@ from email.utils import formatdate, make_msgid import json import random 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 -with open(args.pFileLocation, 'r') as pFile: - participants = json.load(pFile) +def secret_santa_matching(participants: list) -> list: + # create a copy of the list to make choosing a partner easier + copy = list(enumerate(participants[:])) -# 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)): -# choose a partner for each participant -for i in range(len(participants)): + # 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]: - # 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]: + current = participants[i] + participants.remove(current) + partner = random.choice(participants) - current = participants[i] - participants.remove(current) - partner = random.choice(participants) + current['partner'] = partner['partner'] + partner['partner'] = current['name'] + ' (' + current['mail']+')' + participants.append(current) + break - current['partner'] = partner['partner'] - partner['partner'] = current['name'] + ' (' + current['mail']+')' - participants.append(current) - break - - # otherwise choose a random partner for each participant - else: - partner = random.choice(copy) - while partner[0] == i: + # otherwise choose a random partner for each participant + else: partner = random.choice(copy) + while partner[0] == i: + partner = random.choice(copy) - participants[i]['partner'] = partner[1]['name'] + \ - ' ('+partner[1]['mail']+')' - copy.remove(partner) + 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) + return 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: +def send_secret_santa_mails(participants: list, config: dict): + open smtp connection + server = smtplib.SMTP(config['server'], config['port']) + server.ehlo() + server.starttls() + server.ehlo() + server.login(config['user'], config['pass']) - 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) + # 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['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 👼' + plain = f'''Lieb{r['gender']} {r['name']}! - html = f''' +Du bist heuer Wichtel für {r['partner']}. +Das Geschenk sollte nicht mehr als {config['price']} kosten. + +Frohe Weihnachten! +— Dein Christkind 👼''' + + html = f'''

Lieb{r['gender']} {r['name']}!

Du bist heuer Wichtel für {r['partner']}.
- Das Geschenk sollte nicht mehr als 75€ kosten.

+ Das Geschenk sollte nicht mehr als {config['price']} kosten.

Frohe Weihnachten!
— Dein Christkind 👼

@@ -104,7 +94,37 @@ for r in participants: ''' - msg.attach(MIMEText(body, '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()) + 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()