Commit 1acb4b46 authored by ynerant's avatar ynerant
Browse files

First working version


Signed-off-by: default avatarynerant <ynerant@zamokv5.crans.org>
parents
.idea
output
*.csv
Ho ho ho ! Bonjour chèr⋅e {{ p1.username }} !
C'est le père Noël ! 🎅 J'espère que tu as été bien sage cette année !
Vois-tu, je commence à être un peu fatigué. Je suis malheureusement cloué
au lit pour les prochaines semaines, et j'ai besoin d'aide pour ma tournée
de cette année. Peux-tu m'aider ?
J'aimerais que tu offres quelque chose à « {{ p2.username }} », quelque
chose qui puisse lui faire plaisir. Penses-tu en être capable ?
{% if p2.additional_information -%}
Si tu ne vois pas qui c'est, iel a laissé cette information :
{{ p2.additional_information }}
{% endif -%}
Joyeux Noël ! ⛄🎄
--
Le Père Noël
#!/usr/bin/env python
from dataclasses import dataclass
from email.mime.text import MIMEText
from email.utils import formatdate, make_msgid
from random import shuffle
from smtplib import SMTP
from jinja2 import Template
@dataclass(frozen=True)
class Person:
username: str
additional_information: str
email: str
def load_people() -> list[Person]:
"""
Load people from CSV
"""
people = []
with open('secret_santa.csv') as f:
for line in f:
line = line.replace('\n', '')
line = line.replace('"', '')
sp = line.split(',')
sp = (s.strip() for s in sp)
person = Person(*sp)
people.append(person)
return people
def shuffle_people(people: list[Person]) -> list[Person]:
"""
Take random permutation of people,
and ensure this permutation has no single point.
This seems to be not efficient, but the expected number
of generated permutations is e < 3, which is bounded.
This is so satisfying.
"""
shuffled = people.copy()
while True:
shuffle(shuffled)
for p1, p2 in zip(people, shuffled):
if p1 == p2:
break
else:
return shuffled
def main() -> None:
people = load_people()
shuffled = shuffle_people(people)
with open('output', 'w') as f:
for p1, p2 in zip(people, shuffled):
print(p1.username, p2.username, sep='\t', file=f)
print(p1.username, p2.username, sep='\t')
with open('secret_santa.j2') as f:
mail_template = f.read()
mail_template = Template(mail_template)
with SMTP('smtp.crans.org') as smtp:
for p1, p2 in zip(people, shuffled):
content = mail_template.render(p1=p1, p2=p2)
mail = MIMEText(content)
mail['From'] = "Le Père Noël <med@crans.org>"
# mail['To'] = f"{p2.username} <{p2.email}>"
mail['To'] = f"{p2.username} <ynerant@crans.org>"
mail['Subject'] = "Père Noël secret de la Med"
mail['Date'] = formatdate(localtime=True)
mail['Message-ID'] = make_msgid(idstring='secret-santa-med', domain='crans.org')
print(mail.as_string())
smtp.sendmail('med@crans.org', ['ynerant@crans.org'], mail.as_string())
exit(1)
if __name__ == '__main__':
main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment