-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalid_groups.py
executable file
·64 lines (46 loc) · 2.05 KB
/
valid_groups.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
import re
import typing as tp
import requests
from bs4 import BeautifulSoup
# keep groups cached since they rarely change
# groups_cache[group_id] == group_name
groups_cache: tp.Dict[int, str] = {}
def fetch_groups() -> tp.Dict[int, str]:
global groups_cache
if groups_cache:
return groups_cache
# fetch groups if they are not cached
# regex for finding group ids in html params
# 5 digits after 'id_group='
group_id_re = re.compile("(?<=id_group=)\\d{5}")
# fetch html and make soup
html = requests.get("https://guide.herzen.spb.ru/static/schedule.php").content
soup = BeautifulSoup(html, features="lxml")
# get the element with the relevant information
schedule = soup.select("td.body > div")[0]
# get departments (institute, faculty, etc.)
dept_names = [d.get_text() for d in schedule.select("h3 a")]
dept_forms = schedule.find_all("div", recursive=False)
for i in range(len(dept_names)):
dept = dept_forms[i]
# get forms of education (full time, part time or both)
form_names = [d.get_text() for d in dept.find_all("h4", recursive=False)]
form_groups = dept.find_all("ul", recursive=False)
for j in range(len(form_names)):
form = form_groups[j]
# get groups
groups = form.find_all("li", recursive=False)
for group in groups:
# get name and join it with dept and form names
group_name = group.contents[0]
group_full_name = ", ".join([dept_names[i], form_names[j], group_name])
# find group id param in the element
group_id = int(group_id_re.search(str(group)).group())
# capitalize first letter and write to cache
groups_cache[group_id] = group_full_name[0].upper() + group_full_name[1:]
return groups_cache
# check if a group id is present among all groups
def group_id_is_valid(group_id: int) -> bool:
# call in case groups are not fetched yet
groups = fetch_groups()
return group_id in groups