sanjay7178 commited on
Commit
33b8269
·
verified ·
1 Parent(s): a03d56a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -128
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
- session_result = await gen_session(sess, username, password)
31
- if session_result == 0:
32
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
33
- if num_parameters == 3:
34
- return await data_func(sess, username, session_result)
35
- else:
36
- return await data_func(
37
- sess,
38
- username,
39
- await _get_sem_id(sess, username, session_result),
40
- session_result,
41
- )
42
-
43
-
44
- @app.post("/api/attendance")
45
- @app.post("/api/timetable")
46
- @app.post("/api/examSchedule")
47
- async def handle_4param_data_functions(
48
- request: Request, username: str = Form(...), password: str = Form(...)
49
- ):
50
- data_func = {
51
- "/api/attendance": get_attendance_data,
52
- "/api/timetable": get_timetable_data,
53
- "/api/examSchedule": get_examSchedule_data,
54
- }
55
- return await handle_request(data_func[request.url.path], 4, username, password)
56
-
57
-
58
- @app.post("/api/grades")
59
- @app.post("/api/profile")
60
- @app.post("/api/semIDs")
61
- async def handle_3param_data_functions(
62
- request: Request, username: str = Form(...), password: str = Form(...)
63
- ):
64
- data_func = {
65
- "/api/grades": get_grades_data,
66
- "/api/profile": get_profile_data,
67
- "/api/semIDs": _get_all_sem_ids,
68
- }
69
- return await handle_request(data_func[request.url.path], 3, username, password)
70
-
71
-
72
- @app.post("/api/verify")
73
- async def verify_creds(username: str = Form(...), password: str = Form(...)):
74
- basic_creds_check(username, password)
75
-
76
- async with ClientSession() as sess:
77
- session_result = await gen_session(sess, username, password)
78
- if session_result == 0:
79
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
80
- else:
81
- return JSONResponse(
82
- content={"csrf_token": session_result}, status_code=status.HTTP_200_OK
83
- )
84
-
85
-
86
- @app.post("/api/all")
87
- async def all_data(username: str = Form(...), password: str = Form(...)):
88
- basic_creds_check(username, password)
89
-
90
- async with ClientSession() as sess:
91
- session_result = await gen_session(sess, username, password)
92
- if session_result == 0:
93
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
94
- semID = await _get_sem_id(sess, username, session_result)
95
- data = {
96
- "profile": await get_profile_data(sess, username, session_result),
97
- "attendance": await get_attendance_data(
98
- sess, username, semID, session_result
99
- ),
100
- "semIDs": await _get_all_sem_ids(sess, username, session_result),
101
- "grades": await get_grades_data(sess, username, session_result),
102
- "examSchedule": await get_examSchedule_data(
103
- sess, username, semID, session_result
104
- ),
105
- "timetable": await get_timetable_data(
106
- sess, username, semID, session_result
107
- ),
108
- }
109
-
110
- return data
111
-
112
-
113
- @app.post("/api/marks")
114
- async def marks(
115
- username: str = Form(...), password: str = Form(...), semID: str = Form(...)
116
- ):
117
- basic_creds_check(username, password)
118
-
119
- async with ClientSession() as sess:
120
- session_result = await gen_session(sess, username, password)
121
- if session_result == 0:
122
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
123
-
124
- return await get_marks_data(sess, username, semID, session_result)
125
-
126
-
127
- if __name__ == "__main__":
128
- uvicorn.run(app, host="localhost", port=7860)
 
 
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)