/
helloworld_backup_1.py
239 lines (212 loc) · 8.43 KB
/
helloworld_backup_1.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#updated on 11/10/2012 - "SLot" on "last updated" issue fix.
import _mechanize
import webapp2, cookielib
from _mechanize import Browser
from BeautifulSoup import BeautifulSoup
from google.appengine.ext import db
import cookielib
from cookielib import Cookie
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('VITattendace Scraping Base \nRunning On The Google App Engine. \nLast Update: 22 December 2012 \n\n(c) 2012 Siddharth Gupta (B.Tech ECE - VIT)')
class DetailsExtractor(webapp2.RequestHandler):
def get(self, regno, dob, subject):
self.response.headers['Content-Type'] = 'text/html'
br= _mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
n=262
while(n<=262):
m=str(n).zfill(4) # filling zeros for roll no like 001,002 etc.
n=n+1
#self.response.write('11BEC') # This is where roll no goes, for 09BCE just replace by 09BCE.
u=regno
r=br.open('https://academics.vit.ac.in/parent/parent_login.asp')
html=r.read()
soup=BeautifulSoup(html)
final=""
e = soup("img")
decider = len(e)
if (decider==5):
op="*"
else:
op = "+"
for i in range(1, len(e), 1):
z= str(e[i]).split("/")
y= str(z[1]).split(".png")
if (len(y[0])==1):
final=final+y[0]
else :
final=final+op
captcha=str(eval(final)) #final captcha
br.select_form('parent_login')
br.form['wdregno']=u
br.form['vrfcd']=str(captcha)
br.form['wdpswd'] = dob
response=br.submit()
resp = br.open('https://academics.vit.ac.in/parent/attn_report.asp?sem=FS')
l = int(subject)
br.select_form(nr=l)
response2 = br.submit()
details = BeautifulSoup(response2)
last = details("td")
self.response.write(last[len(last)-5].text)
class CaptchaGen(webapp2.RequestHandler):
def get(self, regno):
#self.response.headers['Content-Type'] = 'text/html'
br= _mechanize.Browser()
cj = cookielib.CookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
n=262
while(n<=262):
m=str(n).zfill(4) # filling zeros for roll no like 001,002 etc.
n=n+1
#self.response.write('11BEC') # This is where roll no goes, for 09BCE just replace by 09BCE.
#u=regno
r=br.open('https://academics.vit.ac.in/parent/parent_login.asp')
html=r.read()
soup=BeautifulSoup(html)
img = soup.find('img', id='imgCaptcha')
image_response = br.open_novisit(img['src'])
captcha = Captcha()
#captcha.cookie = "123456788sids"
#captcha.image = db.Blob(image_response.read())
captcha.regno = regno
for cook in cj:
captcha.cookie = cook.value
captcha.cookiename = cook.name
captcha.put()
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(image_response.read())
#self.response.write("captcha stored in db<br/>")
#self.response.write(captcha.cookie)
class Marks(webapp2.RequestHandler):
def get(self, regno):
q = Captcha.all()
q.filter("regno =", regno)
q.order("time")
thevalue = "i didnt get it"
thecookiename = "ASPSESSIONIDQUFTTQDA"
for x in q:
thevalue = x.cookie
thecookiename = x.cookiename
#self.response.write(thevalue)
br1 = _mechanize.Browser()
ck = cookielib.Cookie(version=0, name=thecookiename, value=thevalue, port=None, port_specified=False, domain='academics.vit.ac.in', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=True, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
#setting the existing cookie for marks.
newcj = cookielib.CookieJar()
newcj.set_cookie(ck)
br1.set_cookiejar(newcj)
br1.set_handle_equiv(True)
br1.set_handle_redirect(True)
br1.set_handle_referer(True)
br1.set_handle_robots(False)
r = br1.open("https://academics.vit.ac.in/parent/marks.asp?sem=FS")
html = r.read()
soup = BeautifulSoup(html)
tr=soup('tr', bgcolor="#EDEADE", height="40") # taking all the tr tags
if(tr==[]):
continue
else:
x=0
for i in tr:
x=x+1
l=[[0 for j in range(20)] for i in range(x)]
l1=[[0 for j in range(20)] for i in range(x)]
td=[i.findAll('td') for i in tr]
for i in range(x):
for j in range(20):
if(td[i][4].contents[0]=="Lab Only" or td[i][4].contents[0]=="Embedded Lab" or td[i][4].contents[0]=="Project" ):
l[i][j]='null'
continue
else:
if td[i][j].contents==[]:
l[i][j]='null'
else:
l[i][j]=td[i][j].contents[0].encode('ascii','ignore')
l=[x for x in l if x !=['null' for j in range(20)]]
# print l[0],l[1]
print l[0]
class Extractor(webapp2.RequestHandler):
def get(self, regno, dob, captcha):
q = Captcha.all()
q.filter("regno =", regno)
q.order("time")
thevalue = "i didnt get it"
thecookiename = "ASPSESSIONIDQUFTTQDA"
for x in q:
thevalue = x.cookie
thecookiename = x.cookiename
#self.response.write(thevalue)
captcha = captcha.upper()
br1 = _mechanize.Browser()
ck = cookielib.Cookie(version=0, name=thecookiename, value=thevalue, port=None, port_specified=False, domain='academics.vit.ac.in', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=True, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
r=br1.open('https://academics.vit.ac.in/parent/parent_login.asp')
html=r.read()
soup=BeautifulSoup(html)
#print "Setting cookie from from other browser"
newcj = cookielib.CookieJar()
newcj.set_cookie(ck)
br1.set_cookiejar(newcj)
br1.set_handle_equiv(True)
br1.set_handle_redirect(True)
br1.set_handle_referer(True)
br1.set_handle_robots(False)
br1.select_form('parent_login')
br1.form['wdregno']=regno
br1.form['vrfcd']=str(captcha)
br1.form['wdpswd'] = dob
response=br1.submit()
#self.response.write(response.geturl())
if(response.geturl()=="https://academics.vit.ac.in/parent/home.asp"):
resp = br1.open('https://academics.vit.ac.in/parent/attn_report.asp?sem=FS')
page=resp.read()
soup=BeautifulSoup(page)
tr=soup('td') # taking all the tr tags
length = len(tr) -3
self.response.write("<table>")
#self.response.write("<tr id=\"content\">")
for i in range(16, length, 1):
if tr[i].text == "-":
self.response.write("<tr>Not Uploaded</tr>")
elif tr[i].text == "":
self.response.write("<tr>Not Available</tr>")
else:
self.response.write("<tr>" + tr[i].text + "</tr>")
self.response.write("</table>")
else:
self.response.write("redirect")
class Viewer(webapp2.RequestHandler):
def get(self):
q = User.all()
self.response.write("<ul>")
for x in q:
self.response.write("<li>")
self.response.write(x.number)
self.response.write(" ---- ")
self.response.write(x.dob)
self.response.write("</li>")
self.response.write("</ul>")
class User(db.Model):
number = db.StringProperty()
dob = db.StringProperty()
class Captcha(db.Model):
regno = db.StringProperty()
cookiename = db.StringProperty()
cookie = db.StringProperty()
#image = db.BlobProperty(default=None)
time = db.DateTimeProperty(auto_now_add=True)
class About(webapp2.RequestHandler):
def get(self):
self.response.write("Created by: <br/> Siddharth Gupta")
app = webapp2.WSGIApplication([('/', MainPage),('/view', Viewer), ('/captcha/(.*)', CaptchaGen),('/marks/(.*)', Marks), ('/about', MainPage), ('/att/(.*)/(.*)/(.*)', Extractor) , ('/det/(.*)/(.*)/(.*)', DetailsExtractor)],
debug=True)