Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,128 +1,129 @@
|
|
1 |
-
import uvicorn
|
2 |
-
from aiohttp import ClientSession
|
3 |
-
from gen_session import gen_session
|
4 |
-
from get_marks import get_marks_data
|
5 |
-
from get_grades import get_grades_data
|
6 |
-
from get_profile import get_profile_data
|
7 |
-
from fastapi.responses import JSONResponse
|
8 |
-
from get_timetable import get_timetable_data
|
9 |
-
from get_attendance import get_attendance_data
|
10 |
-
from get_exam_schedule import get_examSchedule_data
|
11 |
-
from get_sem_id import _get_all_sem_ids, _get_sem_id
|
12 |
-
from fastapi import FastAPI, Request, HTTPException, status, Form
|
13 |
-
|
14 |
-
app = FastAPI()
|
15 |
-
|
16 |
-
|
17 |
-
def basic_creds_check(username: str | None, password: str | None):
|
18 |
-
if not username or not password:
|
19 |
-
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
20 |
-
|
21 |
-
|
22 |
-
@app.get("/")
|
23 |
-
async def root():
|
24 |
-
return {"message": "VTOP-AP API"}
|
25 |
-
|
26 |
-
|
27 |
-
async def handle_request(data_func, num_parameters, username, password):
|
28 |
-
basic_creds_check(username, password)
|
29 |
-
async with ClientSession() as sess:
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
session_result,
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
@app.post("/api/
|
46 |
-
@app.post("/api/
|
47 |
-
|
48 |
-
|
49 |
-
):
|
50 |
-
|
51 |
-
|
52 |
-
"/api/
|
53 |
-
"/api/
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
@app.post("/api/
|
60 |
-
@app.post("/api/
|
61 |
-
|
62 |
-
|
63 |
-
):
|
64 |
-
|
65 |
-
|
66 |
-
"/api/
|
67 |
-
"/api/
|
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 |
-
|
|
|
|
1 |
+
import uvicorn
|
2 |
+
from aiohttp import ClientSession
|
3 |
+
from gen_session import gen_session
|
4 |
+
from get_marks import get_marks_data
|
5 |
+
from get_grades import get_grades_data
|
6 |
+
from get_profile import get_profile_data
|
7 |
+
from fastapi.responses import JSONResponse
|
8 |
+
from get_timetable import get_timetable_data
|
9 |
+
from get_attendance import get_attendance_data
|
10 |
+
from get_exam_schedule import get_examSchedule_data
|
11 |
+
from get_sem_id import _get_all_sem_ids, _get_sem_id
|
12 |
+
from fastapi import FastAPI, Request, HTTPException, status, Form
|
13 |
+
|
14 |
+
app = FastAPI()
|
15 |
+
|
16 |
+
|
17 |
+
def basic_creds_check(username: str | None, password: str | None):
|
18 |
+
if not username or not password:
|
19 |
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
20 |
+
|
21 |
+
|
22 |
+
@app.get("/")
|
23 |
+
async def root():
|
24 |
+
return {"message": "VTOP-AP API"}
|
25 |
+
|
26 |
+
|
27 |
+
async def handle_request(data_func, num_parameters, username, password):
|
28 |
+
basic_creds_check(username, password)
|
29 |
+
async with ClientSession() as sess:
|
30 |
+
print("password :" + password)
|
31 |
+
session_result = await gen_session(sess, username, password)
|
32 |
+
if session_result == 0:
|
33 |
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
34 |
+
if num_parameters == 3:
|
35 |
+
return await data_func(sess, username, session_result)
|
36 |
+
else:
|
37 |
+
return await data_func(
|
38 |
+
sess,
|
39 |
+
username,
|
40 |
+
await _get_sem_id(sess, username, session_result),
|
41 |
+
session_result,
|
42 |
+
)
|
43 |
+
|
44 |
+
|
45 |
+
@app.post("/api/attendance")
|
46 |
+
@app.post("/api/timetable")
|
47 |
+
@app.post("/api/examSchedule")
|
48 |
+
async def handle_4param_data_functions(
|
49 |
+
request: Request, username: str = Form(...), password: str = Form(...)
|
50 |
+
):
|
51 |
+
data_func = {
|
52 |
+
"/api/attendance": get_attendance_data,
|
53 |
+
"/api/timetable": get_timetable_data,
|
54 |
+
"/api/examSchedule": get_examSchedule_data,
|
55 |
+
}
|
56 |
+
return await handle_request(data_func[request.url.path], 4, username, password)
|
57 |
+
|
58 |
+
|
59 |
+
@app.post("/api/grades")
|
60 |
+
@app.post("/api/profile")
|
61 |
+
@app.post("/api/semIDs")
|
62 |
+
async def handle_3param_data_functions(
|
63 |
+
request: Request, username: str = Form(...), password: str = Form(...)
|
64 |
+
):
|
65 |
+
data_func = {
|
66 |
+
"/api/grades": get_grades_data,
|
67 |
+
"/api/profile": get_profile_data,
|
68 |
+
"/api/semIDs": _get_all_sem_ids,
|
69 |
+
}
|
70 |
+
return await handle_request(data_func[request.url.path], 3, username, password)
|
71 |
+
|
72 |
+
|
73 |
+
@app.post("/api/verify")
|
74 |
+
async def verify_creds(username: str = Form(...), password: str = Form(...)):
|
75 |
+
basic_creds_check(username, password)
|
76 |
+
|
77 |
+
async with ClientSession() as sess:
|
78 |
+
session_result = await gen_session(sess, username, password)
|
79 |
+
if session_result == 0:
|
80 |
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
81 |
+
else:
|
82 |
+
return JSONResponse(
|
83 |
+
content={"csrf_token": session_result}, status_code=status.HTTP_200_OK
|
84 |
+
)
|
85 |
+
|
86 |
+
|
87 |
+
@app.post("/api/all")
|
88 |
+
async def all_data(username: str = Form(...), password: str = Form(...)):
|
89 |
+
basic_creds_check(username, password)
|
90 |
+
|
91 |
+
async with ClientSession() as sess:
|
92 |
+
session_result = await gen_session(sess, username, password)
|
93 |
+
if session_result == 0:
|
94 |
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
95 |
+
semID = await _get_sem_id(sess, username, session_result)
|
96 |
+
data = {
|
97 |
+
"profile": await get_profile_data(sess, username, session_result),
|
98 |
+
"attendance": await get_attendance_data(
|
99 |
+
sess, username, semID, session_result
|
100 |
+
),
|
101 |
+
"semIDs": await _get_all_sem_ids(sess, username, session_result),
|
102 |
+
"grades": await get_grades_data(sess, username, session_result),
|
103 |
+
"examSchedule": await get_examSchedule_data(
|
104 |
+
sess, username, semID, session_result
|
105 |
+
),
|
106 |
+
"timetable": await get_timetable_data(
|
107 |
+
sess, username, semID, session_result
|
108 |
+
),
|
109 |
+
}
|
110 |
+
|
111 |
+
return data
|
112 |
+
|
113 |
+
|
114 |
+
@app.post("/api/marks")
|
115 |
+
async def marks(
|
116 |
+
username: str = Form(...), password: str = Form(...), semID: str = Form(...)
|
117 |
+
):
|
118 |
+
basic_creds_check(username, password)
|
119 |
+
|
120 |
+
async with ClientSession() as sess:
|
121 |
+
session_result = await gen_session(sess, username, password)
|
122 |
+
if session_result == 0:
|
123 |
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
124 |
+
|
125 |
+
return await get_marks_data(sess, username, semID, session_result)
|
126 |
+
|
127 |
+
|
128 |
+
if __name__ == "__main__":
|
129 |
+
uvicorn.run(app, host="localhost", port=7860)
|