-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
198 lines (140 loc) · 5.29 KB
/
api.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#for logging
import logging
# main entry point for the app
from flask import Flask, request, session, Response
# for handling cookie storage
from flask_cors import CORS
# For rate limiting the API
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
# Core functionality of the app, authentication and fetching commonly used data
# exists within this module
from utils import core_utils
#metadata
#from uptime_kuma_api import UptimeKumaApi
#from utils.SITE import KUMA_USER, KUMA_PASS, KUMA_URL
app = Flask(__name__)
app.secret_key = 'this_college_fucking_sucks'
app.config['SESSION_COOKIE_SECURE'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'None'
CORS(app, supports_credentials=True, origins=[
'http://localhost:58391'
])
limiter = Limiter(
key_func=get_remote_address,
storage_uri="memory://" # I know that using default in memory storage is bad practice, but its acceptable for the small scale API that this is
) # This is going on be an a 1gb RAM machine anyway
limiter.init_app(app)
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# will lead to N number of instances being created when using gunicorn with N number
# of workers, but eh
# kuma_api = UptimeKumaApi(
# url=KUMA_URL
# )
# try:
# kuma_api.login(username=KUMA_USER, password=KUMA_PASS)
# logging.debug("Logged into uptime-kuma")
# except Exception as e:
# logging.warning(f"Login failed: {e}")
# print(e)
# exit()
@app.route('/')
@limiter.limit("100 per minute")
def hello_warudo():
return f'''
向かって来るのか?\n
ザワールドー
'''
@app.route('/healthcheck')
@limiter.limit("100 per minute")
def healthcheck():
# fdy_monitor = kuma_api.get_monitor_beats(1, 24)
# lms_monitor = kuma_api.get_monitor_beats(2, 24)
# return {
# 'fdy_api':fdy_monitor,
# 'lms_api':lms_monitor
# }
return "kuma is disabled temporarily"
@app.route('/login', methods=['POST'])
@limiter.limit("6 per minute")
def login():
if request.method == 'POST':
# data from the end user
username = request.form.get('username')
password = request.form.get('password')
# Send login request to the dypatil site
response = core_utils.attempt_login(username=username, password=password)
# print(response)
#set the session cookie obtained previously
print("login response: ", response)
if response != None:
session['session_cookie'] = response
return response
#return the cookies of the session object, or the status code if it fails to login
return Response(status=401)
@app.route('/subjects', methods=['GET'])
@limiter.limit("3 per minute")
def subjects():
cookies = session.get('session_cookie')
if cookies == None:
return Response(status=401)
logging.debug(f"{cookies} obtained")
response = core_utils.get_subjects(cookies=cookies)
return response
@app.route('/materials', methods=['POST'])
@limiter.limit("15 per minute")
def subject_material():
if request.method == "POST":
target_link = request.form.get('link') # this link must be of the form https://mydy.dypatil.edu/rait/course/view.php?id=[SUBJECT_ID]
cookies = session.get('session_cookie')
if cookies == None:
return Response(status=401)
response = core_utils.get_subject_materials(target_link, cookies=cookies)
return response
@app.route('/download', methods=['POST'])
@limiter.limit("15 per minute")
def download_resource():
if request.method == "POST":
target_link = request.form.get('link') #this link must be of the form https://https://mydy.dypatil.edu/rait/mod/flexpaper/view.php?id=606779
# or https://mydy.dypatil.edu/rait/mod/presentation/view.php?id=611990
link_type = request.form.get('type')
cookies = session.get('session_cookie')
if cookies == None:
return Response(status=401)
response = core_utils.get_download_link(target_link, link_type, cookies)
if(response != None):
return response
return Response(status=401)
@app.route('/attendance', methods=['GET'])
@limiter.limit("15 per minute")
def fetch_attendance_summary():
cookies = session.get('session_cookie')
if cookies == None:
return Response(status=401)
response = core_utils.get_attendance_summary(cookies=cookies)
# print(response)
if response == None:
return Response(status=404)
return response
@app.route('/timetable', methods=['GET'])
@limiter.limit("15 per minute")
def fetch_timetable():
cookies = session.get('session_cookie')
if cookies == None:
return Response(status=401)
response = core_utils.get_timetable(cookies=cookies)
# print(response)
if response == None:
return Response(status=404)
return response
if __name__ == "__main__":
logging.debug(f"Started {__file__} at 0.0.0.0:8000")
# 0.0.0.0 == every address
app.run(
debug=False,
host='0.0.0.0',
port=8000
)