This python2 program reads in a csv of kerberoses (i.e. from Google Docs), creates a gift-giving chain, and sends cruftmas emails. The CSV should include column headings (i.e. Kerberos, Time, etc.) so that you can choose the correct heading. It asks for your athena password so that it can log into the MIT SMTP server and send the emails; it can also dump a list of assignments for you to send yourself.
Created
October 25, 2016 06:23
-
-
Save kazimuth/01b4b0f26eb15c54b46d51f437a059bf to your computer and use it in GitHub Desktop.
Cruftmas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
import smtplib | |
import string | |
import csv | |
import random | |
import itertools as it | |
from datetime import datetime | |
from getpass import getpass | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
MAIL_SERVER = 'outgoing.mit.edu' | |
DEFAULT_CSV = 'cruftmas_users.csv' | |
MESSAGE_TEMPLATE = u'''πππ YOUR CRUFTMAS ASSIGNMENT πππ | |
π {} π | |
- S. Claus π | |
''' | |
SUBJECT_TEMPLATE = u'π Cruftmas Assignment π' | |
FROM_TEMPLATE = u'S. Claus <{}>' | |
def login(sender, password): | |
# http://kb.mit.edu/confluence/display/mitcontrib/Send+Email+from+mit.edu+address+in+gmail | |
server = smtplib.SMTP_SSL(MAIL_SERVER) | |
server.login(sender, password) | |
return server | |
def address(kerberos): | |
return kerberos + '@mit.edu' | |
def message(sender, gifter, assignment): | |
msg = MIMEMultipart('alternative') | |
msg['Subject'] = SUBJECT_TEMPLATE | |
msg['From'] = FROM_TEMPLATE.format(address(sender)) | |
msg['To'] = address(gifter) | |
body = MIMEText(MESSAGE_TEMPLATE.format(assignment), 'plain', 'utf-8') | |
msg.attach(body) | |
return msg.as_string() | |
class c: | |
R = '\033[31m' | |
G = '\033[32m' | |
W = '\033[37m' | |
END = '\033[0m' | |
def read_gifters(gifters_file): | |
'''Read in the list of kerberoses from an input CSV filename, prompting to select the kerberos header''' | |
with open(gifters_file, 'rb') as f: | |
table = csv.DictReader(f) | |
if len(table.fieldnames) > 1: | |
field_id = raw_input(c.W+'Enter # for username heading: '+' '.join(map(str, zip(table.fieldnames, it.count()))) + c.R + ' (1) ') | |
if field_id == '': | |
field = table.fieldnames[1] | |
else: | |
field = table.fieldnames[int(field_id)] | |
else: | |
field = table.fieldnames[0] | |
return sorted(list(set(map(lambda row: row[field] | |
.lower() | |
.replace('@mit.edu', '') | |
.strip(), | |
table)))) | |
def main(): | |
# UI stuff | |
print c.W+'cruftmas.py'+c.R+' 0.0.1 π'+c.END | |
gifters_file = raw_input(c.W+'CSV file containing emails:'+c.R+' ('+DEFAULT_CSV+') ') | |
if gifters_file == '': | |
gifters_file = DEFAULT_CSV | |
gifters = read_gifters(gifters_file) | |
print gifters | |
print c.W + 'Creating gift-giving chain...' + c.END | |
# guaranteed to be one big cycle! | |
random.shuffle(gifters) | |
gift_pairs = zip(gifters, gifters[-1:] + gifters[:-1]) | |
gift_pairs.sort() | |
output_file = './cruftmas-assignments-{}.csv'.format(datetime.now().strftime('%m-%d-%y_%H%M')) | |
with open(output_file, 'wb') as f: | |
out = csv.writer(f) | |
out.writerow(['Gifter', 'Assignment']) | |
for (gifter, assignment) in gift_pairs: | |
out.writerow([gifter, assignment]) | |
print c.W + 'Logged assignments to {}. Look at your own risk!'.format(output_file) + c.END | |
go = raw_input(c.W + 'Send assignments? No going back! (y/N) ' + c.R).lower() == 'y' | |
if go: | |
sender = raw_input(c.W+'sender kerberos: '+c.R) | |
password = getpass(c.W+"sender athena password (don't worry, santa is discreet): "+c.R) | |
print c.W + 'Logging in to {}...'.format(MAIL_SERVER) + c.R | |
server = login(sender, password) | |
for (gifter, assignment) in gift_pairs: | |
print c.W + 'Sending assignment for ' + gifter + '...' + c.END | |
# Send mail FROM sender TO gifter, with a message from sender, to gifter, saying they have an assignment | |
server.sendmail(address(sender), address(gifter), message(sender, gifter, assignment)) | |
else: | |
print c.W + 'Okay, giving up' + c.END | |
print c.W + 'Done!' + c.END | |
if __name__ == '__main__': | |
try: | |
main() | |
except (KeyboardInterrupt, EOFError) as e: | |
print c.W + '\n...never mind' + c.END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment