Created
November 18, 2011 09:33
-
-
Save patricksnape/1376005 to your computer and use it in GitHub Desktop.
CATE results alerter
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
#!/usr/bin/python | |
from urllib2 import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener, install_opener, urlopen | |
from base64 import b64decode | |
from BeautifulSoup import BeautifulSoup | |
import re | |
import smtplib | |
import pickle | |
username = 'USERNAME' | |
password = 'PASSWORD' # base64 encode password (allows editing with giving away password) | |
theurl = 'https://cate.doc.ic.ac.uk/student.cgi?key=2011:c4:' + username | |
def email(message): | |
try: | |
smtpObj = smtplib.SMTP('smtp.cc.ic.ac.uk') | |
smtpObj.ehlo() | |
smtpObj.starttls() | |
smtpObj.login(username, b64decode(password)) | |
smtpObj.sendmail(username + '@ic.ac.uk', username + '@ic.ac.uk', message) | |
smtpObj.close() | |
except: | |
pass | |
def compare_results(current_results, previous_results): | |
for course, coursework_set in current_results.items(): | |
if previous_results[course]: | |
prev_coursework_set = previous_results[course] | |
diff = [ k for k in coursework_set if coursework_set[k] != prev_coursework_set[k] ] #lc | |
if diff: | |
message = '' | |
for k in diff: | |
message += course + '\n' | |
message += k + ' changed from ' + prev_coursework_set[k] + ' to ' + coursework_set[k] + '\n' | |
email(message) | |
def main(): | |
passman = HTTPPasswordMgrWithDefaultRealm() | |
# because we have put None at the start it will always | |
# use this username/password combination for urls | |
# for which `theurl` is a super-url | |
passman.add_password(None, theurl, username, b64decode(password)) | |
# create the AuthHandler | |
authhandler = HTTPBasicAuthHandler(passman) | |
opener = build_opener(authhandler) | |
install_opener(opener) | |
# authentication is now handled automatically for us | |
pagehandle = urlopen(theurl) | |
htmlparser = BeautifulSoup(pagehandle.read()) | |
rows = htmlparser.findAll('tr') | |
results = [] | |
courses = set([]) | |
coursework_results = dict() | |
for row in rows: | |
results.extend(row.findAll('td', align='left', colspan='5')) | |
for r in results: | |
try: | |
course = r | |
coursework_row = r.findNext('tr') | |
courses.add(coursework_row) | |
if not 'No exercise activities recorded for this module' in coursework_row.text: | |
coursename = r.text[3:] | |
if not coursename in courses: | |
courses.add(coursename) | |
coursework_results[coursename] = dict() | |
while coursework_row.text and not 'No student interaction for this exercise' in coursework_row.text: | |
courseworkname = coursework_row.findAll(bgcolor='white')[2].text.replace(' ', '') | |
grade = coursework_row.findAll(align='center')[-1].text | |
coursework_results[coursename][courseworkname] = grade | |
coursework_row = coursework_row.findNext('tr') | |
except: | |
pass | |
try: | |
try: | |
prev_coursework_results = pickle.load(open('results.pkl', 'rb')) | |
compare_results(coursework_results, prev_coursework_results) | |
except: | |
pass | |
pickle.dump(coursework_results, open('results.pkl', 'w+b')) | |
except IOError as e: | |
pass | |
# script entry point | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment