-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
129 lines (126 loc) · 4.91 KB
/
bot.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
#imports
import webbrowser
from bs4 import BeautifulSoup as bs
import requests
from alive_progress import alive_bar
import openpyxl as op
import sys
from openpyxl.styles import Font
#i/p o/p file details:
URlch = input('1: exam.msrit.edu\n2: exam.msrit.edu/examresultseven\nEnter working website choice 1 or 2: ').strip()
if(URlch=='1'):
URLpre = "http://exam.msrit.edu/index.php/component/examresult/?usn="
elif(URlch=='2'):
URLpre = "http://exam.msrit.edu/eresultseven/index.php/component/examresult/?usn="
else:
print("Invalid choice")
sys.exit(0)
usnList = input("Enter name of desired list of USN, (in sample.txt-like format): ")
resultFile = input("Enter name of desired Result .xlsx file(without extension), and PLEASE CLOSE THE FILE, IF ALREADY OPENED: ")+".xlsx"
try:
file = open(usnList,'r')
except:
print("Incorrect file name")
sys.exit(0)
wb = op.Workbook()
allUsn = file.read().split()
total = len(allUsn)
faildata = {}
count=1
with alive_bar(total) as bar:
for usn in allUsn:
bar()
URL = URLpre+usn+"&task=getResult&stage=1"
branch = usn[5:7]
try:
has_f_x_i=False
r = requests.get(URL)
soup = bs(r.text, "lxml")
# print(soup.prettify())
name = soup.find("h3").contents[0]
# imgUrl = "http://exam.msrit.edu"+soup.find(class_="uk-preserve-width uk-border").attrs['src']
# # print(imgUrl)
# response = requests.get(imgUrl, stream=True)
# with open('img.png', 'wb') as out_file:
# shutil.copyfileobj(response.raw, out_file)
# del response
basicdata = soup.find(class_= "detail3").find_all("p")
cred_reg = basicdata[0].contents[0]
cred_earn = basicdata[1].contents[0]
sgpa = basicdata[2].contents[0]
if(URlch=='1'):
cgpa = basicdata[3].contents[0]
advdata = soup.find(class_="uk-table uk-table-striped res-table").findAll("td")
i=0
if(URlch=='2'):
toinsert = [usn,name,cred_reg,cred_earn,sgpa]
else:
toinsert = [usn,name,cred_reg,cred_earn,sgpa,cgpa]
while(i<len(advdata)):
toinsert.append(''.join([x[0] for x in advdata[i+1].contents[0].split()])+'/'+advdata[i].contents[0])
toinsert.append(advdata[i+4].contents[0])
if((advdata[i+4].contents[0]=='X')or(advdata[i+4].contents[0]=='I')or(advdata[i+4].contents[0]=='F')):
has_f_x_i=True
try:
faildata[''.join([x[0] for x in advdata[i+1].contents[0].split()])+'/'+advdata[i].contents[0]]+=1
except:
faildata[''.join([x[0] for x in advdata[i+1].contents[0].split()])+'/'+advdata[i].contents[0]]=1
i+=5
sheet = wb.active
# count = 2
if(branch not in wb.sheetnames):
if(count-1):
# print(faildata,count)
sheet.append([])
sheet.append([])
sheet.append(["Failure Data: "])
for a,b in faildata.items():
sheet.append([a,b])
faildata = {}
wb.create_sheet(branch)
wb.active = wb[branch]
sheet = wb.active
count=1
if(URlch=='2'):
sheet.append(["USN","Name","Creds Registered","Creds Earned","SGPA","Subject","Grade"])
else:
sheet.append(["USN","Name","Creds Registered","Creds Earned","SGPA","CGPA","Subject","Grade"])
# img = op.drawing.image.Image('img.png')
# img.height = 100
# img.width=100
# sheet.row_dimensions[count].height = 100
# sheet.column_dimensions['A'].width = 30
# img.anchor = 'A'+str(count)
count+=1
# sheet.add_image(img)
sheet.append(toinsert)
# count+=1
# print(usn+"Done")
if(has_f_x_i):
try:
faildata["Subs >=1"]+=1
except:
faildata["Subs >=1"]=1
for cell in sheet[str(count)+":"+str(count)]:
cell.font = Font(color='00FF0000', italic=True)
# wb.save(filename=resultFile)
except:
print("Unexpected Error :( at USN: ",usn," please check if it's a valid USN, or still in database")
wb.remove(wb['Sheet'])
# print(faildata,count)
if(count-1):
# print(faildata,count)
sheet.append([])
sheet.append([])
sheet.append(["Failure Data: "])
for a,b in faildata.items():
sheet.append([a,b])
wb.save(filename=resultFile)
print("Generated Result file")
try:
print("Attempting to open")
webbrowser.open(resultFile)
except:
print("Failed to open")
sys.exit()
sys.exit()