forked from profjsb/python-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:kialio/python-bootcamp
- Loading branch information
Showing
11 changed files
with
205 additions
and
10 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
#! /usr/bin/env python | ||
|
||
import sqlite3, os, urllib2, smtplib | ||
from lxml import etree | ||
from email.MIMEMultipart import MIMEMultipart | ||
from email.MIMEText import MIMEText | ||
import NothingToSeeHere # Email password stored in this (private) file | ||
|
||
# Global variables | ||
MYSNURL = "http://astro.berkeley.edu/~cenko/public/BootCamp/SNeInfo.html" | ||
ASTROPEEPSDB = "/Users/cenko/BootCamp/2012B/appetite/astropeeps.sql" | ||
# Need to change this to a path you can write to | ||
|
||
########################################################################### | ||
|
||
def create_astro_table(filename=ASTROPEEPSDB): | ||
|
||
"""Creates sqlite database to store basic information on astronomy department""" | ||
|
||
conn = sqlite3.connect(filename) | ||
c = conn.cursor() | ||
|
||
c.execute('''CREATE TABLE ASTROPEEPS (f_name text, l_name text, | ||
email text, status text)''') | ||
c.execute('''INSERT INTO ASTROPEEPS VALUES ("Josh", "Bloom", | ||
"[email protected]", "Faculty")''') | ||
c.execute('''INSERT INTO ASTROPEEPS VALUES ("Berian", "James", | ||
"[email protected]", "Postdoc")''') | ||
c.execute('''INSERT INTO ASTROPEEPS VALUES ("Joey", "Richards", | ||
"[email protected]", "Postdoc")''') | ||
c.execute('''INSERT INTO ASTROPEEPS VALUES ("Adam", "Morgan", | ||
"[email protected]", "Student")''') | ||
c.execute('''INSERT INTO ASTROPEEPS VALUES ("Chris", "Klein", | ||
"[email protected]", "Student")''') | ||
c.execute('''INSERT INTO ASTROPEEPS VALUES ("Isaac", "Shivvers", | ||
"[email protected]", "Student")''') | ||
c.execute('''INSERT INTO ASTROPEEPS VALUES ("Dan", "Starr", | ||
"[email protected]", "Staff")''') | ||
c.execute('''INSERT INTO ASTROPEEPS VALUES ("Henrik", "Brink", | ||
"[email protected]", "Student")''') | ||
|
||
conn.commit() | ||
c.close() | ||
|
||
return | ||
|
||
############################################################################ | ||
|
||
def retrieve_random_gradstudent(filename=ASTROPEEPSDB, student="Student"): | ||
|
||
"""Returns the name and email address of a random graduate student""" | ||
|
||
conn = sqlite3.connect(filename) | ||
c = conn.cursor() | ||
|
||
c.execute("SELECT f_name, l_name, email FROM ASTROPEEPS WHERE status" + \ | ||
" = '%s' ORDER BY RANDOM() LIMIT 1" % student) | ||
row = c.fetchall() | ||
|
||
conn.commit() | ||
c.close() | ||
|
||
return [row[0][0], row[0][1], row[0][2]] | ||
|
||
########################################################################### | ||
|
||
def retrieve_sn_info(sn_name, url=MYSNURL): | ||
|
||
"""Given the name of a supernova, retrieves the object's coordinates, | ||
host galaxy, and type (if they exists) by reading through the provided | ||
URL. Otherwise returns a list of None.""" | ||
|
||
# Download the HTML from the SNe URL | ||
flob = urllib2.urlopen(url) | ||
s = flob.read() | ||
flob.close() | ||
html = etree.HTML(s) | ||
|
||
# Find all lines following the <table> tag | ||
rows = html.find('.//table') | ||
|
||
# Loop over the table rows | ||
for row in rows: | ||
|
||
# Check if matches name given SN | ||
if (sn_name == row[0].text): | ||
|
||
# Get important info | ||
coords = [row[1].text.replace(" ", ":"), | ||
row[2].text.replace(" ", ":")] | ||
host = row[3].text | ||
sntype = row[4].text | ||
return [host, coords, sntype] | ||
|
||
# If no match found, return a whole lot of nothing | ||
return [None, None, None] | ||
|
||
############################################################################### | ||
|
||
def email_student(address, f_name, l_name, sn_name, host, coords, sntype, | ||
myemail="[email protected]"): | ||
|
||
"""Generate and send an email to address with a request to observe | ||
the given supernova.""" | ||
|
||
# Create the message | ||
msg = MIMEMultipart() | ||
msg["From"] = myemail | ||
msg["To"] = address | ||
|
||
# Write the body, making sure all variables are defined. | ||
msgstr = "Hi %s %s,\n\n" % (f_name, l_name) | ||
msgstr += "I just found out about %s, and it seems neat. " % sn_name | ||
if (host == None): | ||
msgstr += "The host galaxy is unknown. " | ||
else: | ||
msgstr += "The host galaxy is %s. " % host | ||
if (coords == None): | ||
msgstr += "I do not know the coordinates. " | ||
else: | ||
msgstr += "The location is: RA=%s; Dec=%s. " % (coords[0], coords[1]) | ||
if (sntype == None): | ||
msgstr += "I do not know the type.\n\n" | ||
else: | ||
msgstr += "The type is %s.\n\n" % sntype | ||
msgstr += "Could you please arrange some new observations? " | ||
msgstr += "I am really busy drinking right now.\n\n" | ||
msgstr += "Thanks,\nBrad" | ||
msg.attach(MIMEText(msgstr)) | ||
|
||
# Configure the outgoing mail server | ||
mailServer = smtplib.SMTP("smtp.gmail.com", 587) | ||
mailServer.starttls() | ||
mailServer.login(myemail, NothingToSeeHere.passwd) | ||
|
||
# Send the message | ||
mailServer.sendmail(myemail, address, msg.as_string()) | ||
mailServer.close() | ||
|
||
return | ||
|
||
############################################################################### | ||
|
||
def do_science(sn_name, filename=ASTROPEEPSDB, url=MYSNURL, | ||
myemail="[email protected]"): | ||
|
||
"""Script to do cutting edge science. Takes a supernova name, finds | ||
some information about it on a webpage, picks a random graduate student, | ||
and emails that student to request follow-up observations.""" | ||
|
||
# See if the department database exists. If not, create it. | ||
if not os.path.exists(filename): | ||
create_astro_table(filename=filename) | ||
|
||
# Select a random graduate student to do our bidding | ||
[f_name, l_name, address] = retrieve_random_gradstudent(filename=filename) | ||
|
||
# Find out some information about the supernova | ||
[host, coords, sntype] = retrieve_sn_info(sn_name, url=url) | ||
|
||
# Email the student | ||
email_student(address, f_name, l_name, sn_name, host, coords, sntype, | ||
myemail=myemail) | ||
|
||
print "I emailed %s %s at %s about %s." % (f_name, l_name, address, sn_name) | ||
|
||
# Stockholm here I come! | ||
return | ||
|
||
############################################################################### |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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