-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.py
205 lines (157 loc) · 6.36 KB
/
Utility.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
import os
import re
import pdfkit
import pickle
import requests
import pytesseract
import urllib.request
from PIL import Image
from bs4 import BeautifulSoup
from config import URL,HEADER
def connect():
if not os.path.isdir('.temp'):
os.mkdir('.temp')
with requests.Session() as request:
form_data={}
try:
response = request.get(URL,headers=HEADER)
soup = BeautifulSoup(response.text,'html.parser')
#Bypassing Captcha
#-----------------
link = soup.find('img' , {'id': 'imgCaptcha'})
captcha = link.get('src')
captchaLink = URL.split('Combine_GradeCard.aspx')[0]+captcha
urllib.request.urlretrieve(captchaLink,'.temp/captcha.jpg')
captchaText = pytesseract.image_to_string(Image.open('.temp/captcha.jpg'))
#-----------------
viewstate = soup.select("#__VIEWSTATE")[0]['value']
eventValidation = soup.select("#__EVENTVALIDATION")[0]['value']
viewstateGenerator = soup.select('#__VIEWSTATEGENERATOR')[0]['value']
form_data={
'__EVENTTARGET':'',
'__EVENTARGUMENT':'',
'__VIEWSTATE': viewstate,
'__VIEWSTATEGENERATOR': viewstateGenerator,
'__EVENTVALIDATION': eventValidation,
'captcha':"rb_captcha_image",
'txtcaptcha': captchaText,
'btnsearch': 'Print+Score+Card'
}
except:
return form_data
return form_data
def fetchGradeCard(clgCode,rollno,dd,mm,yyyy,generatePDF=True):
form_data = connect()
if len(form_data)==0:
return 0
form_data['ddlcollege']=str(clgCode)
form_data['txtrollno']=str(rollno)
form_data['ddlDD']=str(dd)
form_data['ddlMM']=str(mm)
form_data['ddlYYYY']=str(yyyy)
try:
result = requests.post(URL, data=form_data,headers=HEADER)
result = BeautifulSoup(result.text,'html.parser')
while len(result.body.findAll(text=re.compile('^Sorry! Invalid captch code.$')))!=0:
form_data = connect()
form_data['ddlcollege']=str(clgCode)
form_data['txtrollno']=str(rollno)
result = requests.post(URL, data=form_data,headers=HEADER)
result = BeautifulSoup(result.text,'html.parser')
if len(result.body.findAll(text=re.compile('^Sorry! no record found.$')))!=0:
return 1
for img in result.findAll('img'):
img.decompose()
if generatePDF and not os.path.isdir('Downloads'):
os.mkdir('Downloads')
filepath = 'Downloads/ScoreCard_'+rollno+'.pdf'
with open(f'.temp/{rollno}.html','w',encoding='utf-8') as f:
f.write(str(result))
options = {
'quiet': '',
'encoding': "UTF-8",
'print-media-type': '',
'page-size': 'A4',
'margin-top': '5mm',
'margin-bottom': '5mm',
'margin-left': '5mm',
'margin-right': '5mm',
'zoom': '1.5'
}
if generatePDF:
pdfkit.from_file(f'.temp/{rollno}.html',filepath,options=options)
except:
return 0
return filepath
def isResultOut(courseName, sem):
try:
if courseName=='' and sem == '':
return True
response = requests.get(URL.split('Combine_GradeCard.aspx')[0]+'List_Of_Declared_Results.aspx',headers=HEADER)
soup = BeautifulSoup(response.text,'html.parser')
cells = soup.find('table',attrs={'id':"gvshow_Reg"}).findAll('td')[2:]
courseName = ''.join([s for s in courseName if s.isalnum()])
course = []
semester = []
for i in range(1,len(cells),6):
course.append(''.join([s for s in cells[i].text.lower() if s.isalnum()]))
semester.append(cells[i+2].text)
course_sem_dict = dict(zip(course,semester))
flag=0
for course,semester in course_sem_dict.items():
if (courseName.lower() in course) and (sem.lower() == semester.lower()) :
flag=1
if flag==1:
return True
else:
return False
except:
#print('Error occurred in fetching result. Retrying...')
pass
return False
def getCoursesNames():
try:
response = requests.get(URL.split('Combine_GradeCard.aspx')[0]+'List_Of_Declared_Results.aspx',headers=HEADER)
soup = BeautifulSoup(response.text,'html.parser')
cells = soup.find('table',attrs={'id':"gvshow_Reg"}).findAll('td')[2:]
courses = []
for i in range(1,len(cells),6):
courses.append(''.join([s for s in cells[i].text]))
courses = sorted(set(courses))
with open('Resources/CoursesNames.txt','w') as f:
for i,name in enumerate(courses):
f.write(f'{i+1}) {name}\n')
except:
#print('Error in fetching Course names.')
return False
def fetchAndSaveClgCodes():
try:
response = requests.get(URL,headers=HEADER)
soup = BeautifulSoup(response.text,'html.parser')
s=soup.find('select',{'id':'ddlcollege'})
items=s.find_all('option')[1:]
clgCode = [item.get('value') for item in items]
clgName = [item.text for item in items]
clgCodeDict = dict(zip(clgName,clgCode))
with open('Resources/collegeCodes.pkl','wb') as f:
pickle.dump(clgCodesDict,f)
except:
#print('Error in fetching!')
return {}
return clgCodeDict
def printClgCodes():
with open('Resources/collegeCodes','rb') as g:
clgCodeDict = pickle.load(g)
print('\n{0:65} {1:^5}'.format('College Name','College Code'))
print('{0:65} {1:^5}'.format('------------','------------\n'))
for clg,code in clgCodeDict.items():
print('{0:-<65} {1:^5}'.format(clg,code))
def printCourseNames():
with open('Resources/CoursesNames.txt','r',encoding='utf-8') as f:
courses = f.read()
print()
[print(course) for course in courses.split('\n')]
def getClgCodes():
with open('Resources/collegeCodes','rb') as g:
clgCodeList = pickle.load(g)
return list(clgCodeList.values())