Created
September 15, 2012 05:45
-
-
Save JonCooperWorks/3726493 to your computer and use it in GitHub Desktop.
A Python wrapper for the OurVLE student note portal at UWI.
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
#Usage | |
#Initiate a new instance of an OurVleBrowser | |
browser = OurVleBrowser() | |
''' | |
Returns dict in the form: | |
{ | |
'name' : '[NAME]', | |
'courses' : [ { 'id' : '[COURSE_ID]', 'title' : '[COURSE_TITLE]', 'url' : '[COURSE_URL]' } ] | |
} | |
''' | |
student = browser.login('[STUDENT ID]', '[PASSWORD]') | |
#Should return True | |
browser.is_logged_in() | |
#Should return True | |
browser.logout() | |
#Should return False | |
browser.is_logged_in() |
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
''' | |
OurVLE wrapper. Created by Jonathan Cooper | |
Uses regex to extract data, because regex. | |
Currently is only capable of logging in, getting data and logging out. | |
''' | |
import urllib2 | |
import urllib | |
import cookielib | |
import re | |
class OurVleBrowser(object): | |
'''Allows for downloading of data from OurVLE.''' | |
def __init__(self): | |
self._login_url = 'http://ourvle.mona.uwi.edu/login/index.php' | |
self._logout_url = None | |
self.student = None | |
def _get(self, url): | |
'''Send GET request to a URL''' | |
request = urllib2.Request(url) | |
response = urllib2.urlopen(request) | |
return response.read() | |
def _post(self, url, values): | |
'''Send POST request and handle cookies''' | |
cj = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
data = urllib.urlencode(values) | |
headers = { | |
'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12', | |
'Connection' : 'Keep-Alive' | |
} | |
request = urllib2.Request(url, data, headers) | |
response = opener.open(request) | |
return response.read() | |
def _parse(self, text, regexp): | |
'''Return all matches of a certain regex''' | |
match = re.compile(regexp) | |
return match.findall(text) | |
def _get_courses(self, response): | |
'''Gets all subjects from OurVLE. Returns a dict with keys 'url', 'code' and 'title' ''' | |
matches = self._parse(response, '<a title="(.*)" href="(.*)">(.*)</a>') | |
if matches is not None: | |
labels = ('code', 'url', 'title') | |
subjects = [dict(zip(labels, match)) for match in matches] | |
return subjects | |
return None | |
def _get_name(self, response): | |
'''Gets name from response body''' | |
matches = self._parse(response, '<div class="logininfo">You are logged in as <a href=".*">(.*)</a> ') | |
return matches[0] | |
def login(self, username, password): | |
'''Accepts a username : password combination and attempts to log in to OurVLE. Returns a dict with keys 'name' and 'courses' upon | |
success and sets self.student to the student object, or None upon failure.''' | |
login = {'username' : username, 'password' : password, 'testcookies' : 0} | |
response = self._post(self._login_url, login) | |
if not self.is_logged_in() and response is not None: | |
self._logout_url = self._parse(response, '<a href="(http://ourvle.mona.uwi.edu/login/logout.php\?sesskey=.*)">Logout</a>')[0] | |
self.student = {'name' : self._get_name(response), 'courses' : self._get_courses(response)} | |
return self.student | |
else: | |
return None | |
def is_logged_in(self): | |
return self.student is not None and self._logout_url is not None | |
def logout(self): | |
if self.is_logged_in(): | |
self._get(self._logout_url) | |
self.student = None | |
self._logout_url = None | |
return True | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment