This repository was archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
191 lines (174 loc) · 7.31 KB
/
parse.py
File metadata and controls
191 lines (174 loc) · 7.31 KB
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
import requests
from bs4 import BeautifulSoup
URL = 'https://college.ks.ua/#'
HEADER = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0',
'accept': '*/*'}
dict_weak = {
1: 'Понеділок',
2: 'Вівторок',
3: 'Середа',
4: 'Четвер',
5: "П'ятниця"
}
stick_num = {
'1': '1️⃣',
'2': '2️⃣',
'3': '3️⃣',
'4': '4️⃣',
'5': '5️⃣',
'6': '6️⃣',
'7': '7️⃣',
'8': '8️⃣',
}
def parse():
html = get_html(URL)
if html.status_code == 200:
return get_content(html.text)
else:
return None
def get_html(url, params=None):
r = requests.get(url, headers=HEADER, params=params)
return r
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
return soup.find_all('div', class_='shedule_content')
def clean(text):
return ' '.join(text.split())
def get_timetables(group, dayofweak):
items = parse()
if items:
column_num = None
txt_msg = f'🔸*{dict_weak[dayofweak]}*🔸\n'
one = False
before = True
for item in items:
if not 'Розклад занять' in item.find('p', class_='shedule_content__title').text:
continue
elif 'Розклад занять' in item.find('p', class_='shedule_content__title').text and one == True:
continue
table = item.find('tbody')
rows = table.find_all('tr')
for row in rows:
columns = row.find_all('td')
if group == columns[2].text.strip():
column_num = 2
elif group == columns[5].text.strip():
column_num = 5
elif column_num == 2:
if not dict_weak[dayofweak] in columns[0].text.strip() and before:
continue
if dict_weak[dayofweak] in columns[0].text.strip():
if columns[1].text.split():
txt_msg += f'{stick_num[columns[1].text.strip()]} ' \
f'*{clean(columns[2].text)}* ' \
f'_{clean(columns[3].text)}_\n'
before = False
else:
try:
int(columns[0].text.strip())
txt_msg += f'{stick_num[columns[0].text.strip()]} ' \
f'*{clean(columns[1].text)}* ' \
f'_{clean(columns[2].text)}_\n'
except Exception:
break
elif column_num == 5:
if not dict_weak[dayofweak] in columns[0].text.strip() and before:
continue
if dict_weak[dayofweak] in columns[0].text.strip():
if columns[5].text.split():
txt_msg += f'{stick_num[columns[4].text.strip()]} ' \
f'*{clean(columns[5].text)}* ' \
f'_{clean(columns[6].text)}_\n'
before = False
else:
try:
int(columns[0].text.strip())
txt_msg += f'{stick_num[columns[3].text.strip()]} ' \
f'*{clean(columns[4].text)}* ' \
f'_{clean(columns[5].text)}_\n'
except Exception:
break
else:
continue
if txt_msg:
return txt_msg
else:
return None
else:
return 'error1'
def get_replacements(message):
items = parse()
if items:
num_ = 0
txt_msg = ''
for item in items:
if 'Розклад занять' in item.find('p', class_='shedule_content__title').text:
continue
if num_ < 2:
txt_msg += get_zblock(item, message)
num_ = num_ + 1
else:
break
return txt_msg
else:
return 'Error'
def get_zblock(block, message):
day_title = block.find('p', class_='shedule_content__title').text
table = block.find('tbody')
lines = table.find_all('tr')
txt_msg = f'*{day_title}*' + '\n'
par = True
z_par = ''
z_aud = ''
for line in lines:
columns = line.find_all('td')
if par:
# Пропускаем первую строчку
if columns[0].text.strip() == 'Гр.':
continue
# Ищем совпадения в заменах пар
elif str(message) in columns[0].text.strip() or \
str(message) in columns[2].text.strip() or \
str(message) in columns[3].text.strip():
z_par += f'{stick_num[columns[1].text.strip()]} пара ' \
f'{columns[0].text.strip()} група ' \
f'{columns[2].text.strip()} на ' \
f'*{columns[3].text.strip()}*'
# Если указана аудитория дописываем
if columns[4].text.strip():
z_par += f' в _{columns[4].text.strip()}_\n'
else:
z_par += '\n'
# Пропуск пустых блоков в заменах пар
elif columns[2].text.strip() == '':
continue
# Нахождения блока замен аудиторий и конкатенация замен пар
elif columns[2].text.strip() == 'Заміна аудиторій':
if z_par:
txt_msg += '*Заміни пар:*\n' + z_par
else:
txt_msg += '*Замін пар немає*\n'
par = False
continue
else:
if columns[2].text.strip() == '':
if z_aud:
txt_msg += '*Заміни аудиторій:*\n' + z_aud
else:
txt_msg += '*Замін аудиторій немає*\n'
break
# Ищем совпадения в заменах аудиторий по первому столбцу
elif str(message) in columns[0].text.strip() or \
str(message) in columns[2].text.strip():
z_aud += f'{columns[0].text.strip()} група ' \
f'{stick_num[columns[1].text.strip()]} пара ' \
f'{columns[2].text.strip()} в ' \
f'*{columns[3].text.strip()}*'
# Ищем совпадения в заменах аудиторий по второму столбцу
elif str(message) in columns[4].text.strip() or \
str(message) in columns[6].text.strip():
z_aud += f'{columns[4].text.strip()} група ' \
f'{stick_num[columns[5].text.strip()]} пара ' \
f'{columns[6].text.strip()} в ' \
f'*{columns[7].text.strip()}*'
return txt_msg + '\n\n'