Last active
May 15, 2022 11:27
-
-
Save pratyushmittal/68f67c20e4fb6f0cd072 to your computer and use it in GitHub Desktop.
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
*.pyc | |
*.jpe |
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 | |
import urllib | |
from robobrowser import RoboBrowser | |
from PIL import Image | |
import pytesseract | |
from IPython import embed | |
def parse_captcha(filename): | |
"""Retrieves text from given filename""" | |
image = Image.open(filename) | |
#removing the alpha channel | |
r, g, b = image.split() | |
without_alpha = Image.merge('RGB', (r, g, b)) | |
text = pytesseract.image_to_string(without_alpha) | |
return text.replace(' ', '').strip().upper() | |
class WebBot: | |
browser = None | |
form = None | |
def __init__(self, roll_no): | |
self.roll_no = roll_no | |
def get_form(self): | |
"""Opens course page and returns form""" | |
self.browser = RoboBrowser() | |
# Get initial page to select program | |
self.browser.open('http://result.rgpv.ac.in/exam/ProgramSelect.aspx') | |
form = self.browser.get_form(id='form1') | |
form['radlstProgram'].value = '11' | |
self.browser.submit_form(form) | |
# Get main form from second page | |
self.form = self.browser.get_form(id='form1') | |
def save_captcha_image(self, filename): | |
image = self.browser.find('img', alt='Captcha') | |
source = 'http://result.rgpv.ac.in/exam/' + image['src'] | |
return urllib.urlretrieve(source, filename) | |
def submit_form(self, captcha): | |
"""Returns filled form""" | |
self.form['txtrollno'].value = self.roll_no | |
self.form['drpSemester'].value = '4' | |
self.form['TextBox1'].value = captcha | |
# It seems that sleep is compulsary | |
# else it will give an error of wront text | |
time.sleep(5) | |
self.browser.submit_form(self.form) | |
def has_result(self): | |
if 'you have entered a wrong text' in self.browser.response.content: | |
return False | |
return True | |
def with_retries(func): | |
attempts = 3 | |
def func_call(*args, **kwargs): | |
tries = 0 | |
while tries < attempts: | |
tries += 1 | |
student = func(*args, **kwargs) | |
if student.has_result(): | |
return student | |
print "{} Attempt failed. Retrying.".format(tries) | |
print "Aborted!" | |
return func_call | |
@with_retries | |
def get_result(roll_no): | |
captcha_file = 'captcha.jpe' | |
student = WebBot(roll_no) | |
student.get_form() | |
student.save_captcha_image(captcha_file) | |
captcha_text = parse_captcha(captcha_file) | |
student.submit_form(captcha_text) | |
return student | |
if __name__ == '__main__': | |
ROLL_NO = '0842ar121011' | |
RESULT = get_result(ROLL_NO) | |
if RESULT: | |
embed() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment