Skip to content

Instantly share code, notes, and snippets.

@tonyskapunk
Created December 28, 2017 18:41
Show Gist options
  • Save tonyskapunk/fa5a23d7479aa8a65faead87ebd43904 to your computer and use it in GitHub Desktop.
Save tonyskapunk/fa5a23d7479aa8a65faead87ebd43904 to your computer and use it in GitHub Desktop.
intercambio 2017
#!/usr/bin/env python
# original: https://bitbucket.org/davidnorth/scripts/src/tip/secret-santa.py?fileviewer=file-view-default
import sys
import random
import requests
api_endpoint = "https://api.mailgun.net/v3"
api_key = ""
domain = "tonyskapunk.net"
sender = "tonyg@{}".format(domain)
api_msg = api_endpoint + "/{}/messages".format(domain)
subject = "Intercambio pingüino 2017"
text = ("Estimado {},\n"
"Regalaras una playera a {}.\n"
"https://www.unixstickers.com/tshirts-and-hoodies/t-shirts")
if sys.stdin.isatty():
print >> sys.stderr, "Oops, you forgot to pipe the input file to the script (try secret-santa.py < names.txt)"
sys.exit(1)
givers = [line.strip().split(',') for line in sys.stdin]
givers = [(name.strip(), email.strip()) for name, email in givers]
takers = [g for g in givers]
for name, email in givers:
recipient, r_email = random.choice([(name2, email2) for name2, email2 in takers if not email==email2])
takers.remove((recipient, r_email))
r = requests.post(
api_msg,
auth=('api', api_key),
data={
'from': sender,
'to': email,
'subject': subject,
'text': text.format(
name,
recipient,
),
},
)
if r.ok:
print('Email to <{}> delivered.'.format(email))
else:
print('There was an issue delivering to: {}.'.format(email))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment