-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
236 lines (220 loc) · 6.96 KB
/
main.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
import webapp2, string, random, urllib2, json
from datetime import *
from PIL import Image
from StringIO import StringIO
from mechanize import _mechanize
from BeautifulSoup import BeautifulSoup
from cookielib import Cookie
from google.appengine.ext import db
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.api import mail
from ttform import TimeTable
from CaptchaParser import CaptchaParser
class Notice(db.Model):
text = db.TextProperty()
author = db.StringProperty()
timestamp = db.DateTimeProperty(auto_now = True)
cnum = db.IntegerProperty()
class GCMclient(db.Model):
gcmclients = db.StringListProperty()
class AddClient(webapp2.RequestHandler):
def post(self):
cnums = self.request.get_all("cnum")
gcmid = self.request.get("gcmid")
if gcmid == '' or cnums == None:
status = {'status':'failure'}
else:
for cnum in cnums:
GCMclientobject = GCMclient.get_by_key_name(cnum)
if GCMclientobject == None:
GCMclientobject = GCMclient(key_name = cnum)
GCMclientobject.gcmclients.append(str(gcmid))
GCMclientobject.put()
status = {'status':'success'}
else:
GCMclientobject.gcmclients.append(str(gcmid))
GCMclientobject.put()
status = {'status':'success'}
self.response.write(json.dumps(status))
class AddEditNotice(webapp2.RequestHandler):
def post(self):
text = self.request.get("text")
author = self.request.get("author")
cnum = self.request.get("cnum")
nid = self.request.get("id")
if nid == '':
notice = Notice(text = text, author = author, cnum = int(cnum)).put()
status = {'status':'created'}
#Add GCM push here for all students belonging to the cnum mentioned above
else:
notice = Notice.get_by_id(int(nid))
if text != '':
notice.text = text
if author != '':
notice.author = author
if cnum != '':
notice.cnum = int(cnum)
notice.put()
status = {'status':'edited'}
self.response.write(json.dumps(status))
class RetrieveNoticeByClassNum(webapp2.RequestHandler):
def get(self, cnum):
notices = Notice.all()
notices.filter("cnum =", int(cnum))
notices.order("-timestamp")
njsons = []
for notice in notices:
njson = {'text':notice.text, 'author':notice.author, 'id':notice.key().id(), 'timestamp':str(notice.timestamp), 'cnum':notice.cnum}
njsons.append(njson)
njsons = {'notices':njsons}
self.response.write(json.dumps(njsons))
class RetrieveNoticesByStudent(webapp2.RequestHandler):
def post(self):
cnums = self.request.get_all("cnum")
masternotices = []
for cnum in cnums:
notices = Notice.all()
notices.filter("cnum =", int(cnum))
notices.order("-timestamp")
njsons = []
for notice in notices:
njson = {'text':notice.text, 'author':notice.author, 'id':notice.key().id(), 'timestamp':str(notice.timestamp), 'cnum':notice.cnum}
njsons.append(njson)
njsons = {'cnum':cnum, 'notices':njsons}
masternotices.append(njsons)
masternotices = {'masternotices':masternotices}
self.response.write(json.dumps(masternotices))
class DeleteNotice(webapp2.RequestHandler):
def get(self, nid):
notice = Notice.get_by_id(int(nid))
if notice != None:
notice.delete()
status = {'status':'success'}
else:
status = {'status':'failure'}
self.response.write(json.dumps(status))
def token_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
class GetToken(webapp2.RequestHandler):
def get(self, regno, dob):
flag = True
while flag is True:
token = token_generator()
checkvalue = memcache.get(token)
if checkvalue is None:
flag = False
memcache.set(key = token, value = {"regno":regno.upper(),"dob":dob}, time = 86400)
time = datetime.now()
expiry = time + timedelta(seconds = 86400)
result = {'regno':regno.upper(), 'token':token, 'expiry':expiry.strftime('%Y-%m-%d %H:%M:%S'), 'dob':dob}
self.response.write(json.dumps(result))
class AccessToken(webapp2.RequestHandler):
def get(self, token):
token = token.upper()
response = memcache.get(token)
if response is None:
result = {'status':'failure','regno':''}
else:
result = {'status':'success','regno':response["regno"],'dob':response["dob"]}
self.response.write(json.dumps(result))
class GetTimeTable(webapp2.RequestHandler):
def get(self, regno, dob):
result = memcache.get(regno.upper())
if result != None:
self.response.write(json.dumps(result))
return
regno = regno.upper()
br = _mechanize.Browser()
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
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'])
image = Image.open(StringIO(image_response.read()))
parser = CaptchaParser()
captcha = parser.getCaptcha(image)
br.select_form('parent_login')
br.form['wdregno'] = regno
br.form['vrfcd'] = str(captcha)
br.form['wdpswd'] = dob
response = br.submit()
if (response.geturl() == "https://academics.vit.ac.in/parent/home.asp"):
r=br.open('https://academics.vit.ac.in/parent/timetable.asp?sem=WS')
page=r.read()
soup=BeautifulSoup(page)
trs=soup.findAll('tr')
data=""
count=0
for tr in trs:
if(count!=0):
if("Total Credits" in tr.text):
break
else:
tas=tr.findChildren()
for ta in tas:
data=data+ta.text+"\n"
count=count+1
refinedData=""
temp=1
for da in data.split("\n"):
if (temp==1 and da!=""):
refinedData=refinedData+da+"\n"
temp=0
else:
temp=1
data=refinedData[135:]
subs=[]
temp=1
sub={}
for da in data.split("\n"):
if(temp==1):
sub.update({"slno":da})
elif(temp==2):
sub.update({"cnum":da})
elif(temp==3):
sub.update({"code":da})
elif(temp==4):
sub.update({"title":da})
elif(temp==6):
sub.update({"ltpc":da})
elif(temp==7):
sub.update({"bl":da})
elif(temp==9):
sub.update({"slot":da})
elif(temp==10):
sub.update({"venue":da})
elif(temp==11):
sub.update({"faculty":da})
elif(temp==12):
sub.update({"billdate":da})
elif(temp==13):
temp=0
subs.append(sub)
sub={}
temp=temp+1
final = {'subjects':subs}
t = TimeTable(json.dumps(final))
tt = t.formTT()
final = {"subjects":subs}
final.update({"timetable":tt})
final.update({"dob":dob})
memcache.set(key = regno, value = final)
self.response.write(json.dumps(final))
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world!')
app = webapp2.WSGIApplication([
('/', MainHandler),
('/gettimetable/(.*)/(.*)', GetTimeTable),
('/accesstoken/(.*)', AccessToken),
('/getnewtoken/(.*)/(.*)', GetToken),
('/addeditnotice', AddEditNotice),
('/noticesbycnum/(.*)', RetrieveNoticeByClassNum),
('/noticesbystudent', RetrieveNoticesByStudent),
('/addclient', AddClient)
], debug=True)