-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnew.py
172 lines (145 loc) · 5.25 KB
/
new.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
from datetime import timedelta
import aiohttp
import json
import asyncio
from bs4 import BeautifulSoup
from utils.utils import get_hallticket_helper, calculate_sgpa
from utils.utils import get_results_info, get_student_info
async def create(session, examCode, etype, type, result, htno, redis_client):
# Check if the given specific hallticket along with other details are already cached in redis
current_key = f"calculate-{htno}-'btech'-{examCode}-{etype}-{type}-{result}"
redis_response = redis_client.get(current_key)
if redis_response != None:
redis_out = json.loads(redis_response)
return redis_out
# Else, do some async stuff
try:
link = "http://results.jntuh.ac.in/results/resultAction?degree=btech"
resp = await session.get(
link + examCode + etype + type + result + "&grad=null" + f"&htno={htno}")
print(link + examCode + etype + type + result + "&grad=null" + f"&htno={htno}")
if resp.status == 500:
raise Exception("First link failed to get details")
html = await resp.text()
try:
sel_soup = BeautifulSoup(html, "html.parser")
student = get_student_info(sel_soup)
results = get_results_info(sel_soup)
new_result = calculate_sgpa([student, results])
# Now, cache it with the current_key in the redis client and the return
redis_client.set(current_key, json.dumps(new_result))
redis_client.expire(current_key, timedelta(minutes=30))
return new_result
except Exception as e:
print("INSIDE SOUP: ", e)
redis_client.set(current_key, json.dumps({htno: "FALSE"}))
redis_client.expire(current_key, timedelta(minutes=30))
except Exception as e:
try:
link = "http://202.63.105.184/results/resultAction?degree=btech"
resp = await session.get(
link
+ examCode
+ etype
+ type
+ result
+ "&grad=null"
+ f"&hallticket={htno}"
)
print(
link + examCode + etype + type + result + "&grad=null" + f"&htno={htno}"
)
if resp.status == 500:
raise Exception("Second link also failed to get details")
html = await resp.text()
try:
sel_soup = BeautifulSoup(html, "html.parser")
student = get_student_info(sel_soup)
results = get_results_info(sel_soup)
new_result = calculate_sgpa([student, results])
# Now, cache it with the current_key in the redis client and the return
redis_client.set(current_key, json.dumps(new_result))
redis_client.expire(current_key, timedelta(minutes=30))
return new_result
except Exception as e:
print("INSIDE SOUP", e)
redis_client.set(current_key, json.dumps({htno: "FALSE"}))
redis_client.expire(current_key, timedelta(minutes=30))
except Exception as e:
print("BOTH THE LINKS FAILED TO GET RESULT: ", e)
redis_client.set(current_key, json.dumps({htno: "FALSE"}))
redis_client.expire(current_key, timedelta(minutes=30))
def get_tasks(
session, examCode, etype, type, result, roll_number, start, end, redis_client
):
tasks = []
for i in range(start, end + 1):
htno = get_hallticket_helper(roll_number, i)
print(htno)
tasks.append(
asyncio.create_task(
create(
session,
examCode,
etype,
type,
result,
htno,
redis_client=redis_client,
)
)
)
return tasks
async def get_result(
roll_number, start, end, examCode, etype, type, result, redis_client
):
results = []
async with aiohttp.ClientSession() as session:
tasks = get_tasks(
session,
examCode,
etype,
type,
result,
roll_number,
start,
end,
redis_client,
)
responses = await asyncio.gather(*tasks)
for result in responses:
if result:
results.append(result)
return results
def get_results_async(
hallticket_from, hallticket_to, examCode, etype, type, result, redis_client
):
from utils.constants import string_dict
roll_number = hallticket_from[0:8]
s1 = str(hallticket_from[8:10])
s2 = str(hallticket_to[8:10])
def test(s1):
try:
s1 = int(s1)
return s1
except:
s1 = str(string_dict[s1[0]]) + str(s1[1])
s1 = 100 + int(s1)
return s1
start = test(s1)
end = test(s2)
results = asyncio.run(
get_result(
roll_number,
start,
end,
f"&examCode={examCode}",
f"&etype={etype}",
f"&type={type}",
f"&result={result}",
redis_client,
)
)
if results.count(None):
results.remove(None)
return results