-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
156 lines (133 loc) · 4.23 KB
/
main.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
from fastapi import FastAPI, Response, Cookie, HTTPException
from pydantic import BaseModel
from scrapper import Scrapper
from typing import Union
from datetime import date
from dateutil.relativedelta import relativedelta
description = """
Attendance API helps you grab your attendance details from RIT Soft quickly and easily.
## What can this API do?
This API can do :
* **Login remotely**
* **View basic student information**
* **Get attendance details of your current semester**
* **View last updated attendance dates of subjects**
* **Get absent details**
* **Get present details**
"""
tags_metadata = [
{
"name": "status",
"description": "To check the status of the API.",
},
{"name": "login", "description": "To login."},
{
"name": "attendance",
"description": "To view attendance details (_after logging in_)",
},
]
app = FastAPI(
title="Attendance API", description=description, openapi_tags=tags_metadata
)
scrapper = Scrapper()
class User(BaseModel):
username: str
password: str
@app.get(
"/",
tags=["status"],
summary="Check status of API",
response_description="Returns a json object with alive = true on success",
)
def is_alive():
return {
"alive": "yes",
"message": "Check out https://attendance-api.onrender.com/docs for more details",
}
@app.get(
"/login",
tags=["login"],
summary="Check logged in status",
response_description="Returns a json object with basic student details on success",
)
def is_logged_in(
session_cookie: str = Cookie(None),
):
if not session_cookie:
raise HTTPException(status_code=401, detail="You are not logged in.")
else:
return scrapper.check_login(session_cookie)
@app.post(
"/login",
tags=["login"],
summary="Log in and generate a cookie",
response_description="Returns a json object with a cookie on success",
)
def login(
response: Response,
user: User,
):
cookie = scrapper.login(user.username, user.password)
response.set_cookie(key="session_cookie", value=cookie, httponly=True)
return {
"message": "Login success. Cookie has been set successfully.",
"session-cookie": cookie,
}
@app.get(
"/attendance",
tags=["attendance"],
summary="View attendance details",
response_description="Returns a json object with basic student details and attendance details of the student on success",
)
def get_attendance(
session_cookie: str = Cookie(None),
starting_date: date = date.today() + relativedelta(months=-6),
ending_date: date = date.today(),
):
if not session_cookie:
raise HTTPException(
status_code=401, detail="Not logged in. Please log in and try again."
)
else:
return scrapper.scrape_attendance(session_cookie, starting_date, ending_date)
@app.get(
"/attendance/lastupdate",
tags=["attendance"],
summary="View last attendance update details",
response_description="Returns a json object with subjects and their last attendance update details on success",
)
def get_last_update(
session_cookie: str = Cookie(None),
):
if not session_cookie:
raise HTTPException(
status_code=401, detail="Not logged in. Please log in and try again."
)
else:
return scrapper.scrape_last_update(session_cookie)
@app.get(
"/attendance/absent",
tags=["attendance"],
summary="View absent hours",
response_description="Returns a json object with absent dates and details on success",
)
def get_absent_hours(session_cookie: str = Cookie(None)):
if not session_cookie:
raise HTTPException(
status_code=401, detail="Not logged in. Please log in and try again."
)
else:
return scrapper.scrape_absent(session_cookie)
@app.get(
"/attendance/present",
tags=["attendance"],
summary="View present hours",
response_description="Returns a json object with present dates and details on success",
)
def get_absent_hours(session_cookie: str = Cookie(None)):
if not session_cookie:
raise HTTPException(
status_code=401, detail="Not logged in. Please log in and try again."
)
else:
return scrapper.scrape_present(session_cookie)