-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
185 lines (128 loc) · 4.83 KB
/
server.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
import re
from typing import Optional
from datetime import datetime
from fastapi import FastAPI
from core.repositories.asset_repository import AssetRepository
from core.repositories.faculty_repository import FacultyRepository
from core.repositories.group_repository import GroupRepository
from core.request import unecon_request
from core.schedule.site_parser import UneconParser
from core.types.lesson import Lesson
from core.utils.date_utils import get_study_week_number
from db import Session
app = FastAPI()
@app.get("/faculty")
async def get_faculties(name: Optional[str] = None):
session = Session()
faculties_repository = FacultyRepository(session)
faculties = faculties_repository.get(name=name)
return {"faculties": faculties}
@app.get("/faculty/{faculty_id}")
async def get_faculty_by_id(faculty_id: int):
session = Session()
faculties_repository = FacultyRepository(session)
faculties = faculties_repository.get_by_id(faculty_id)
return faculties
@app.get("/group")
async def get_groups(course: Optional[int] = None, faculty_id: Optional[int] = None, name: Optional[str] = None):
session = Session()
group_repository = GroupRepository(session)
groups = group_repository.get(course=course, faculty_id=faculty_id, name=name)
return {"groups": groups}
@app.get("/group/{group_id}")
async def get_group_by_id(group_id: int):
session = Session()
group_repository = GroupRepository(session)
group = group_repository.get_group_by_id(group_id)
return group
@app.get("/group/{group_id}/schedule")
async def get_group_schedule(group_id: int, week: Optional[int] = None):
page = unecon_request(group_id=group_id, week=week)
if page.status_code == 200:
page_parser = UneconParser(page.text)
lessons = page_parser.parse_page()
week = page_parser.get_current_week_number()
dict_lessons = lessons_to_dict(lessons)
return {
'week': week,
'lessons': dict_lessons
}
return {
'lessons': []
}
@app.get("/group/{group_id}/lessons/next")
async def get_next_lessons(group_id: int, after_date: Optional[str] = None):
current_date = None
now_date = datetime.now()
if after_date is not None:
current_date = datetime.fromisoformat(after_date)
else:
current_date = now_date
week = get_study_week_number(current_date, now_date)
page = unecon_request(group_id=group_id, week=week)
if page.status_code != 200:
return {
'lessons': []
}
page_parser = UneconParser(page.text)
lessons = page_parser.parse_page()
# get remainder of 52 (number of weeks in year) because api gives 53
# when last study year ends but new year has not started yet
week = page_parser.get_current_week_number() % 52
lessons_after_date = get_lessons_after_date(lessons, current_date)
print(lessons_after_date)
if len(lessons_after_date) != 0:
return {
'lessons': lessons_to_dict(lessons_after_date)
}
page = unecon_request(group_id=group_id, week=week + 1)
if page.status_code != 200:
return {
'lessons': []
}
page_parser = UneconParser(page.text)
lessons = page_parser.parse_page()
week = page_parser.get_current_week_number()
lessons_after_date = get_lessons_after_date(lessons, current_date)
if len(lessons_after_date) != 0:
return {
'lessons': lessons_to_dict(lessons_after_date)
}
return {
'lessons': []
}
@app.get("/asset/{asset_id}")
async def get_asset(asset_id: str):
session = Session()
asset_repository = AssetRepository(session)
asset = asset_repository.get_by_id(asset_id)
return asset
@app.get("/hello/{name}")
async def say_hello(name: str):
return {"message": f"Hello {name}"}
def get_lessons_after_date(lessons: list[Lesson], date: datetime) -> list[Lesson]:
sorted_lessons = list(sorted(lessons, key=lambda l: l.get_start_date()))
for i in range(len(sorted_lessons)):
lesson = sorted_lessons[i]
if lesson.get_start_date() > date:
return sorted_lessons[i:]
return []
def lessons_to_dict(lessons: list[Lesson]) -> list[dict]:
dict_lessons = []
for lesson in lessons:
day = lesson.get_day_start_date()
start_time = lesson.get_start_date()
end_time = lesson.get_end_date()
dict_lesson = {
'name': lesson.name,
'day': day.isoformat(),
'day_of_week': lesson.day_of_week,
'start': start_time,
'end': end_time,
'professor': lesson.professor,
'location': lesson.location,
'lesson_type': lesson.get_lesson_type(),
'is_elective': lesson.get_is_elective()
}
dict_lessons.append(dict_lesson)
return dict_lessons