Last active
June 7, 2021 13:55
-
-
Save skshetry/6ba696ea0aa24a471aba3bd65517b5c5 to your computer and use it in GitHub Desktop.
Python script that provides notification when result has arrived.(For IOE, using selenium and chromium driver. Providing notification using notify2 in GNOME)
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
import time # to sleep for some time | |
import logging #logging | |
from selenium import webdriver #selenium | |
from gi.repository import Notify #to provide notifications in GNOME | |
##################################################### Requirements ################################ | |
#selenium | |
#chromedriver(bundled with chromium, separately available for google-chrome) | |
################################################################################################### | |
# config options | |
config = { | |
'URL': 'http://exam.ioe.edu.np/', # The url to scan for | |
'TIME_DELAY': 10, # time to delay the refreshing | |
'TEXT_TO_FIND':["Second", 'II',], # text to search for in the url | |
'NO_OF_TRAVERSAL': 5, # no. of rows to traverse | |
'APP_NAME': 'Aayo aayo!', #Name of our app to show notification | |
'NOTIF_SUMM': 'May the force be with you!', | |
} | |
Notify.init(config['APP_NAME']) # Init app to provide notification | |
result = Notify.Notification.new(summary=config['NOTIF_SUMM']) #summary of our notification | |
logger = logging.getLogger() #Logging | |
#function to provide notification | |
def notifyme(shortdescr): | |
result.update('Aayo aayo', shortdescr) #update notif | |
logger.warning('Showing/Updating notification.') | |
result.show() #show notif | |
#find required row | |
def find_latest_row_with_result(rows): | |
i = 0 #row counter | |
for row in rows: #for every row | |
text = scan_each_row(row) # scan every row | |
if text is not None: | |
return text #return row's content | |
if i >= config['NO_OF_TRAVERSAL']: | |
break #not found, move on. leave the loop. | |
#check for only five latest notices. If not found, break. | |
i += 1 #increment counter | |
def scan_each_row(row): | |
# Don;t check all, check only recents | |
if config['TEXT_TO_FIND'][0] in row.text or config['TEXT_TO_FIND'][1] in row.text: | |
return row.text | |
def get_rows(browser): | |
browser.get(config['URL']) #open url | |
table = browser.find_elements_by_xpath("//div[@id='targetDiv']/div/table") | |
#search for 'div' with id 'targetDiv' | |
#and go to child 'div' and then 'table' | |
rows = table[0].find_elements_by_tag_name('tr') # search row | |
return rows | |
#checking function to find result | |
def check_result(browser): | |
rows = get_rows(browser) #get rows of the table | |
text = find_latest_row_with_result(rows) #find row with result | |
if text is not None: | |
notifyme(text) #send if there's result | |
def setup(): #Setup | |
options = webdriver.ChromeOptions() # initialize chrome with options we will be using | |
options.add_argument('headless') #run without GUI, in headless mode | |
options.add_argument('incognito') # run in incognito, don't want to mess up with my history | |
browser = webdriver.Chrome(chrome_options=options) #provide chrome with the options | |
return browser #send browser to 'main' module | |
def tearup(browser): #cler every mess you have made | |
result.close() #remove notification | |
browser.quit() #quit browser | |
if __name__ == '__main__': #main module | |
browser = setup() # setup everything | |
while True: # Go On and On | |
try: | |
check_result(browser) #check for result | |
logger.warning("Going to sleep for {0} seconds.".format(config['TIME_DELAY'])) | |
time.sleep(config['TIME_DELAY']) # sleep for certain amount of time | |
except KeyboardInterrupt: #if 'Ctrl+Z', close everything | |
logger.warning("Clearing up the mess") | |
break #break the loop | |
except Exception as e: | |
logging.exception(e) | |
print("Exceptions occured. Retrying...") | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment