-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
75 lines (59 loc) · 2.16 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from PIL import Image
import mechanize,cookielib
from mechanize import Browser
from BeautifulSoup import BeautifulSoup
from StringIO import StringIO
import json, timeit,os
from CaptchaParser import CaptchaParser
import urllib2, urllib
from cookielib import CookieJar
# input-type values from the html form
def login():
if os.path.isfile('cre.json'):
with open('cre.json', 'r') as f:
data = json.load(f)
regno = data['username']
passw = data['password']
else:
regno=raw_input("Registration Number: ")
passw=raw_input("Password: ")
with open('cre.json', 'w') as f:
f.write(json.dumps({
'username': regno,
'password': passw
}, ensure_ascii=False))
f.close()
cj = cookielib.CookieJar()
br= mechanize.Browser()
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_cookiejar(cj)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
print "Fetching Captcha"
r=br.open('https://vtop.vit.ac.in/student/stud_login.asp')
html=r.read()
soup=BeautifulSoup(html)
im = soup.find('img', id='imgCaptcha')
image_response = br.open_novisit(im['src'])
img=Image.open(StringIO(image_response.read()))
imgcpy=img.copy()
starttime = timeit.default_timer()
parser=CaptchaParser()
captcha=parser.getCaptcha(img)
stoptime = timeit.default_timer()
print "Recognized Captcha:"+str(captcha)+" in "+str(stoptime-starttime)
formdata = { "regno" : regno, "passwd": passw, "vrfcd" : str(captcha) }
data_encoded = urllib.urlencode(formdata)
response = opener.open('https://vtop.vit.ac.in/student/stud_login_submit.asp', data_encoded)
print "Logging in User:"+str(regno)
print response.geturl()
if(response.geturl()=="https://vtop.vit.ac.in/student/home.asp"):
print"Success!"
else:
print '''Failed :(
Either Username or Password is wrong or there is some fault in decoding captcha try again'''
return opener
if __name__=="__main__":
br=login()