Impartus Bot

[GitHub Repo]

Came alive on March 12, 2020



Motivation

    At the time of writing this, I am back home after having left university due to the infamous corona virus pandemic. When our university notified us that we'd be having a 4-week vacation until further notice, most of us were ecstatic. I, for one, shared the sentiment, but only because I wouldn't have to attend my morning classes - although my thoughts and prayers were with the victims of corona.

    However, much to my dismay, we then received a follow-up email from uni saying that we'd have online classes sticking with the REGULAR SCHEDULE!!! It wasn't about the classes -  like I said, I'm not much of a morning person, and so it was only about the timing of the classes. We had a strict attendance policy at uni, which meant morning classes weren't going anywhere. 

    The desire to sleep can do marvels, and that's when it struck me - these were *online classes* - my point is, you only need to login for the duration of the class to be marked as present. And I was sure that if you can fill out your login credentials, and click on a few buttons to attend lectures, so could python.

 

 

The Script in Action!

    Personally, I get motivated to study something in more detail if have some concrete idea of the big picture. The clearer that end goal is, the more it helps me to decide if its worth my time (and motivates me to dive into the nitty gritty details). I believe you should have that choice as well. So here is the what the end goal looks like. Enjoy!

 

 

 

Making the idea come to life!

[Click here if you just want the final script]

    The selenium "webdriver" api helps you automate the actions made in a web browser, such as clicking on buttons, typing in login credentials and so on. In order to use the webdriver, you must first install the webdriver executable for the browser of your choice. I used chrome, and the webdriver for the same can be found here . If you're confused about your chrome version, then go to the vertical ellipsis symbol (on the top right corner) -> Help -> About Google Chrome .  Also make sure you've installed python selenium on your system.

    The following code snippet sets up the driver object. We then ask the webdriver object to navigate to the url of our choice (here, it's the url where my online lectures happened).

 

# Necessary imports
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Creating a webdriver instance by specifying the path to our webdriver executable
driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.maximize_window()

driver.get("https://a.impartus.com/login/#/")

     

    I then needed to enter my credentials and that was easy to do with the "send_keys" and "click" functions provided by selenium's webdriver.

 

# Fill out your username and password.
username.send_keys("<my_user_name>")
password.send_keys("<my_password>")

# Click on the login button
login = driver.find_element_by_class_name("iu-btn")
login.click()

   

    On the Impartus Platform you get to see your list of upcoming classes. There's usually just a single live class at any given time and it will be the first class on the list. I needed my webdriver to find this button. Fortunately there is a neat functionality in selenium that gets the job done. We can use the "XML Path" or the XPath of an element in unsion with selenium's "find_element_by_xpath" function to find the first "join" button on the list of classes.

 

class_button = driver.find_element_by_xpath('/html/body/div[1]/ui-view/div[1]/div[2]/ui-view/div/div[2]/div[3]/dashboard-interests/div/live-streaming-lectures/md-card/md-list/div[1]/div[1]/div[2]/button')

   

    Don't get intimidated by the length of that xpath. You don't need to know how to specify the xpath, because your browser does all the hard-work for you. In Google Chrome, simply right click on the element of interest, and click on "Inspect". This opens upon a "developer's console" to the right, with the html of the desired element highlighted. To get the Xpath, right click on the highlighted html, go to "Copy" and click on "Copy XPath". And Voila! You've got your xpath.

    If the class is live, the button that we now have control over would be enabled. Once we click the button, we then click another "join" button with the same logic as above, so I'll not bother you with those details. The only interesting bit left is leaving a message in the live chat, to show that you're "actually" present. Here we grab the chatbox element using Xpath again, and type in a message depending upon the time of the day. If the class in the morning ( < 12 ), we send "Good Morning!", else we send "Good Afternoon!" (We don't have classes at night :)). And once that's done I asked the script to sleep for 40 minutes (the duration of our class). You can see all this functionality in the following code.

 

# If the "join" button is live, click on the button.
if class_button.is_enabled():
    class_button.click()

    # Clicking the above button, opens a new tab.
    # So we need to switch tabs using the following.
    driver.switch_to.window(driver.window_handles[1])

    # Click on join button inside class.
    join_button = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/button[2]')
    join_button.click()

    # Send a message on chat, being the diligent student that you are ;)
    chat_message = driver.find_element_by_xpath('//*[@id="imus-chat-input-editor"]')
    if int(time.ctime().split()[3][:2]) < 12:
        chat_message.send_keys("Good Morning!")
    else:
        chat_message.send_keys("Good Afternoon!")

    # Send typed message
    send_button = driver.find_element_by_xpath('/html/body/div[1]/div/ng-include/div/div[3]/div/div[3]/i')
    send_button.click()

    # You check if the "lecture over" alert has come up every 10 minutes (600s).
    # If it has, you exit the loop
    while(flag == 0):
        try:
            class_ended_button = driver.find_element_by_xpath('/html/body/div[4]/md-dialog/md-dialog-actions/button')
            class_ended_button.click()
            flag = 1
        except:
            time.sleep(600)
    
    # switch back to the previous tab.
    driver.switch_to.window(driver.window_handles[0])

 

    Once class is over, I needed to logout of my account. Again the logic of finding the XPath stays the same as usual, you just apply it to different buttons.

 

# find the dropdown menu
dropdown_menu_button = driver.find_element_by_xpath('//*[@id="header"]/div[4]/div[4]/header-settings-menu/div/md-menu/div')
dropdown_menu_button.click()


# find logout button and logout
logout_button = driver.find_element_by_xpath('//*[@id="menu_container_1"]/md-menu-content/md-menu-item[6]/button')
logout_button.click()

driver.quit()

 

    Scheduling to run this script according to your timetable is easy. On Windows, you could use "Windows Task Scheduler" and on Linux based distros, you could you "crontab". The steps to set these up are quite easy, so I won't go over those here. 

    And that's it! So if you're not a morning person and have online classes, you know what to do! 

 

 

Final Script

# Necessary imports
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Creating a webdriver instance by specifying the path to our webdriver executable
driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.maximize_window()

driver.get("https://a.impartus.com/login/#/")

# Fill out your username and password.
username.send_keys("<my_user_name>")
password.send_keys("<my_password>")

# Click on the login button
login = driver.find_element_by_class_name("iu-btn")
login.click()

# If the "join" button is live, click on the button.
if class_button.is_enabled():
    class_button.click()

    # Clicking the above button, opens a new tab.
    # So we need to switch tabs using the following.
    driver.switch_to.window(driver.window_handles[1])

    # Click on join button inside class.
    join_button = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/button[2]')
    join_button.click()

    # Send a message on chat, being the diligent student that you are ;)
    chat_message = driver.find_element_by_xpath('//*[@id="imus-chat-input-editor"]')
    if int(time.ctime().split()[3][:2]) < 12:
        chat_message.send_keys("Good Morning!")
    else:
        chat_message.send_keys("Good Afternoon!")

    # Send typed message
    send_button = driver.find_element_by_xpath('/html/body/div[1]/div/ng-include/div/div[3]/div/div[3]/i')
    send_button.click()

    # You check if the "lecture over" alert has come up every 10 minutes (600s).
    # If it has, you exit the loop
    while(flag == 0):
        try:
            class_ended_button = driver.find_element_by_xpath('/html/body/div[4]/md-dialog/md-dialog-actions/button')
            class_ended_button.click()
            flag = 1
        except:
            time.sleep(600)
    
    # switch back to the previous tab.
    driver.switch_to.window(driver.window_handles[0])

# find the dropdown menu
dropdown_menu_button = driver.find_element_by_xpath('//*[@id="header"]/div[4]/div[4]/header-settings-menu/div/md-menu/div')
dropdown_menu_button.click()


# find logout button and logout
logout_button = driver.find_element_by_xpath('//*[@id="menu_container_1"]/md-menu-content/md-menu-item[6]/button')
logout_button.click()

driver.quit()