Using Python to automate the registration process of my university’s lab classes
First of all, allow me to introduce myself by stating that I’m a Computer Science student at Athens University of Economics and Business.
In my uni, we have several courses that include extra lessons, called CS lab lessons (or simply “labs”), where we exercise and showcase our technical knowledge on some specific subjects (mainly programming languages).
In order to attend those classes, you are required to register for them using the university’s online education platform, called “Open eClass”. Each lab has a maximum limit of 40 members.
The thing is, you need to do this every single week, in order for the professors to make sure that you are actively attending their lessons and not just occupying someone else’s position, while not being in class. Legit enough!
But, there is a kinda frustrating issue with that. The registration process is not automated but up to the professor, meaning that we, the students, do not know when to expect for it to be open. We simply know that the registrations will be available sometime on the first day of the week, the exact time that this will happen is unclear.
I myself, am the type of guy that would always attend lessons that have to do with technical stuff but hates to be anxious about whether or not they will be able to register in time for the desired lab, so what would be more fitting than making my life simpler by automating the registration process?
Isn’t that what programming was invented for after all?
And so it begins! While I’m a huge JavaScript fan, Python sounds ideal for this specific task. Let’s have a look at the university’s online platform.
I, first of all, had to find out if the platform uses JavaScript to dynamically load some of its content or not, so I performed the lab registration process with JavaScript turned off in my browser and everything worked just as fine. That means I won’t have to use Selenium webdriver in order to fetch the labs’ information, which makes things even easier than I originally expected.
We could use some more complex scraping frameworks like Scrapy, but that would be an overkill and I like to keep it simple. Beautiful Soup it is then!
I like clean code so I’m splitting it into three different files:
- config.py: includes some essential info like my login credentials, the desired lab day and time etc.
- main.py: acts as the main script
- func.py: contains the functions and logic which will be used by the main file
I started by creating the login function, based on the login page’s form request, using, well… requests.
# Dependencies
import config
import requests
# Session Setup
s = requests.Session()
# Login to your account
def login(username, password):
payload = {
'pass': password,
'submit': '',
'uname': username
}
s.get(config.URL)
s.post(config.URL, data=payload)
return
Moving on, I would have to visit the course’s page, check through the available lessons and submit my registration for the one I’m interested in.
This can be easily translated to code using beautiful soup (bs). I checked each one of the table’s rows, looking for the one containing the desired day and time.
When it’s found, bs grabs the registration URL, which can be fetched through the last column, and the requests library makes a get request using it.
# Dependencies
from bs4 import BeautifulSoup as bs
# Register for a lab
def register(day, time):
# Labs Page Source Code
groupsHTML = s.get('%smodules/group/?course=%s' %(config.URL, config.COURSE))
# Beautiful Soup Setup
soup = bs(groupsHTML.content, 'lxml')
# Labs' Info Scraping
a = ''
for group in soup.find_all('td'):
if (day in group.text and time+'-' in group.text and not 'η ομάδα μου' in group.text):
reg_ele = group.find_next_siblings('td')[3]
a = reg_ele.find('a')['href']
# Registration
if (a): s.get('%smodules/group/%s' %(config.URL, a))
return
Those of you who went through the code after carefully seeing the preview listed above are probably thinking "why the heck isn't this guy checking if the members limit is reached?".
And you are right. I should. But the script is supposed to run endlessly, constantly checking if the registrations are open and only stop executing when the registration is successful. It usually takes more than an hour before the group is full because the rest of the students have to register manually. Why bother checking the number of members when I’m always one of the first ones to sign up?
Next, I need to create a function so as to verify that the registration was complete, and I can do that by checking if the substring “η ομάδα μου” (meaning my group) is included in the lab row’s text.
# Verify successful lab registration
def isRegistered():
sourceTMP = s.get('%smodules/group/?course=%s' %(config.URL, config.COURSE))
soupTMP = bs(sourceTMP.content, 'lxml')
for group in soupTMP.find_all('td'):
if (config.DATE['day'] in group.text and config.DATE['time']+'-' in group.text and 'η ομάδα μου' in group.text):
return True
return False
Everything seems great till now, let’s create the main file that will run those functions.
# Dependencies
import config
import func
import time
# Login
func.login(config.USERNAME, config.PASSWORD)
# Check if already registered
if func.isRegistered():
print('Είστε ήδη εγγεγραμμένοι στο επιθυμητό group.') #meaning: You've already registered for this specific lab lesson
raise SystemExit(0)
# Register to your desired group
starttime = time.time()
tries = 1
while not func.isRegistered():
print('%sη προσπάθεια' %(str(tries))) #meaning: Number of tries
func.register(config.DATE['day'], config.DATE['time'])
time.sleep((config.INTERVAL * 60.0) - ((time.time() - starttime) % (config.INTERVAL * 60.0)))
tries += 1
# Registration Complete
print('Έχετε εγγραφεί επιτυχώς στο group που θα πραγματοποιηθεί ημέρα %s και ώρα %s.' %(config.DATE['day'], config.DATE['time'])) #meaning: You have successfully registered for your desired lab lesson
The platform’s URL, course’s ID, login credentials, group’s info and time interval are all located in the config.py file we created earlier, which looks like this:
# Base
URL = 'https://eclass.aueb.gr/'
# Course
COURSE = 'INF111' # your course's ID
# Login Credentials
USERNAME = 'YOUR_USERNAME' # your eClass username
PASSWORD = 'YOUR_PASSWORD' #your eClass password
# Lab Info
DATE = {
'day': 'DESIRED_DAY', # your desired day (e.g. Wednesday)
'time': 'DESIRED_HOUR' # your desired hour (e.g. 19:00)
}
# Interval (in minutes)
INTERVAL = 5
Voilà! We managed to automate the registration process in just a few lines of code! No more need to patiently wait for the registrations to be open, repeatedly refreshing the website’s page, being unsure about whether or not I make it in time.
Some of you may also question the morality of this code or even believe that I’m cheating by being the only one of my classmates using it. Well, that’s no longer the case.
The full code included in this article is available on my github and is free for everyone to use, alter or optimize.
You did a great job! Hopefully your university will aknowledge that.