TGBotReffBook

Форк
0
/
scheduleparser.py 
184 строки · 8.7 Кб
1
import json
2
import datetime
3
import itertools
4
from aiohttp import request
5

6
from async_lru import alru_cache
7

8
from app.data.keyspace import LessonsKeyWords
9
from app.data.keyspace import Separators
10
from app.data.urls import SCHEDULE_API_URL
11

12

13
class ScheduleParser:
14
    """Парсер сайта СПбПУ с расписание с помощью API"""
15

16
    @staticmethod
17
    @alru_cache
18
    async def getInstitutes() -> dict:
19
        """API запрос для получения списка всех институтов"""
20
        url = SCHEDULE_API_URL + f"/faculties"
21
        async with request("GET", url) as html_page:
22
            answer = await html_page.json()
23
            institutes_dict = dict()
24
            for inst in answer["faculties"]:
25
                institutes_dict[inst["id"]] = inst["name"]
26
            return institutes_dict
27

28
    @staticmethod
29
    @alru_cache
30
    async def getCourses(faculty: int, ed_form: str, degree: int) -> list:
31
        """API запрос для получения количечества курсов при заданных параметрах"""
32
        json_pack = await ScheduleParser.getGroups(faculty)
33
        levels = set()
34
        for group in json_pack["groups"]:
35
            if group["type"] == ed_form and group["kind"] == degree:
36
                levels.add(group['level'])
37
        return list(levels)
38

39
    @staticmethod
40
    @alru_cache
41
    async def getGroupsByParameters(faculty: int, ed_form: str, degree: int, level: int) -> dict:
42
        """API запрос для получения всех групп по парамтерам: институт, форма обучения, степень обучения, курс"""
43
        json_pack = await ScheduleParser.getGroups(faculty)
44
        groups = dict()
45
        for group in json_pack["groups"]:
46
            if group["type"] == ed_form and group["kind"] == degree and group["level"] == level:
47
                groups[group["name"] + Separators.DATA_META + str(group["id"])] = group["name"]
48
        return {i: groups[i] for i in sorted(groups)}
49

50
    @staticmethod
51
    async def getGroupsByText(group: str) -> dict:
52
        """API запрос для получения подходящих групп по тексту"""
53
        url = SCHEDULE_API_URL + f"/search/groups?q={group}"
54
        async with request("GET", url) as html_page:
55
            answer = await html_page.json()
56
            if answer["groups"] is None:
57
                return dict()
58

59
            groups_dict = dict()
60
            for gr in answer["groups"]:
61
                groups_dict[gr["name"] + Separators.DATA_META + str(gr["id"])] = gr["name"]
62
            return groups_dict
63

64
    @staticmethod
65
    @alru_cache
66
    async def getTeacherNameByID(id: int) -> str:
67
        """API запрос для получения подходящих преподавателей по тексту"""
68
        url = SCHEDULE_API_URL + f"/teachers/{id}"
69
        async with request("GET", url) as html_page:
70
            answer = await html_page.json()
71
            if answer.get("error"):
72
                return str()
73
            return answer["full_name"]
74

75
    @staticmethod
76
    async def getTeacherByText(teacher: str) -> dict:
77
        """API запрос для получения подходящих преподавателей по тексту"""
78
        url = SCHEDULE_API_URL + f"/search/teachers?q={teacher}"
79
        async with request("GET", url) as html_page:
80
            answer = await html_page.json()
81
            if answer["teachers"] is None:
82
                return dict()
83

84
            teachers_dict = dict()
85
            for tcr in answer["teachers"]:
86
                teachers_dict[str(tcr["id"])] = tcr["full_name"]
87
            return {i[0]: i[1] for i in sorted(teachers_dict.items(), key=lambda t: t[1])}
88

89
    @staticmethod
90
    async def getPlacesByText(place: str) -> dict:
91
        """API запрос для получения подходящих групп по тексту"""
92
        url = SCHEDULE_API_URL + f"/search/rooms?q={place}"
93
        async with request("GET", url) as html_page:
94
            answer = await html_page.json()
95
            if answer["rooms"] is None:
96
                return dict()
97

98
            groups_dict = dict()
99
            for gr in answer["rooms"]:
100
                groups_dict[str(gr["building"]["id"]) + Separators.DATA_META + str(gr["id"])] = gr["building"]["name"] \
101
                                                                                                + ", " + gr["name"]
102
            return groups_dict
103

104
    @staticmethod
105
    @alru_cache
106
    async def getLessons(group: int, date: datetime.date) -> list:
107
        """API запрос для получения занятий группы на неделю, где присутсвуте дата = date"""
108
        url = SCHEDULE_API_URL + '/scheduler/' + str(group) + '?date=' + date.isoformat()
109
        async with request("GET", url) as html_page:
110
            answer = await html_page.json()
111
            return ScheduleParser.parseLessons(answer)
112

113
    @staticmethod
114
    @alru_cache
115
    async def getTeacherLessons(teacher: int, date: datetime.date) -> list:
116
        """API запрос для получения занятий преподавателя на неделю, где присутсвуте дата = date"""
117
        url = SCHEDULE_API_URL + '/teachers/' + str(teacher) + "/scheduler?date=" + date.isoformat()
118
        async with request("GET", url) as html_page:
119
            answer = await html_page.json()
120
            return ScheduleParser.parseLessons(answer)
121

122
    @staticmethod
123
    @alru_cache
124
    async def getPlaceLessons(building: int, place: int, date: datetime.date) -> list:
125
        """API запрос для получения занятий преподавателя на неделю, где присутсвуте дата = date"""
126
        url = SCHEDULE_API_URL + '/buildings/' + str(building) + '/rooms/' + str(place) + "/scheduler?date=" + date.isoformat()
127
        async with request("GET", url) as html_page:
128
            answer = await html_page.json()
129
            return ScheduleParser.parseLessons(answer)
130

131
    @staticmethod
132
    def parseLessons(answer: json) -> list:
133
        """Парсит json расписания"""
134
        lessons_result = []
135
        for day in answer["days"]:
136
            day_dict = dict()
137
            day_dict[LessonsKeyWords.DAY] = day["date"]
138
            lessons_at_day = []
139
            for lesson in day["lessons"]:
140
                lesson_dict = dict()
141
                lesson_dict[LessonsKeyWords.START_TIME] = lesson["time_start"]
142
                lesson_dict[LessonsKeyWords.END_TIME] = lesson["time_end"]
143
                lesson_dict[LessonsKeyWords.NAME] = lesson["subject_short"]
144
                # Type
145
                if lesson.get("typeObj") is not None:
146
                    lesson_dict[LessonsKeyWords.TYPE] = lesson.get("typeObj")["name"]
147

148
                # Type
149
                if lesson.get("additional_info") is not None:
150
                    lesson_dict[LessonsKeyWords.ADD_INFO] = lesson.get("additional_info")
151

152
                # Groups
153
                if lesson.get("groups") is not None:
154
                    lesson_dict[LessonsKeyWords.GROUPS_NAME] = [group["name"] for group in lesson["groups"]]
155
                    lesson_dict[LessonsKeyWords.GROUPS_LINK] = [
156
                        f"/faculty/{group['faculty']['id']}/groups/{group['id']}" for group in lesson["groups"]]
157

158
                # Teacher
159
                if lesson.get("teachers") is not None:
160
                    lesson_dict[LessonsKeyWords.TEACHER_NAME] = lesson["teachers"][0]["full_name"]
161
                    lesson_dict[LessonsKeyWords.TEACHER_LINK] = f"/teachers/{lesson['teachers'][0]['id']}"
162

163
                # Place
164
                if lesson.get("auditories") is not None:
165
                    auditori = lesson["auditories"][0]
166
                    lesson_dict[LessonsKeyWords.PLACE_NAME] = auditori["building"]["name"] + ", " + auditori["name"]
167
                    lesson_dict[LessonsKeyWords.PLACE_LINK] = f"/places/{auditori['building']['id']}/{auditori['id']}"
168

169
                # Resource
170
                if lesson.get("lms_url") is not None and lesson.get("lms_url") != "":
171
                    lesson_dict[LessonsKeyWords.RESOURCE_NAME] = "СДО"
172
                    lesson_dict[LessonsKeyWords.RESOURCE_LINK] = lesson["lms_url"]
173
                lessons_at_day.append(lesson_dict)
174
                day_dict[LessonsKeyWords.LESSONS] = lessons_at_day
175
            lessons_result.append(day_dict)
176
        return lessons_result
177

178
    @staticmethod
179
    @alru_cache
180
    async def getGroups(faculty):
181
        """API запрос для получения всех групп в институте"""
182
        group_url = SCHEDULE_API_URL + "faculties/" + str(faculty) + "/groups"
183
        async with request("GET", group_url) as html_page:
184
            return await html_page.json()
185

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.