-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
84 lines (63 loc) · 2.47 KB
/
main.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
import re
import datetime
from bs4 import BeautifulSoup
from icalendar import Calendar, Event
def parse_date(year, month, date):
ymd_match = re.compile("(20[0-9]+)년[ ]*([0-9]+)월[ ]*([0-9]+)일.*").match(date)
if ymd_match:
return datetime.date(
int(ymd_match.group(1)), int(ymd_match.group(2)), int(ymd_match.group(3))
)
md_match = re.compile("([0-9]+)월[ ]*([0-9]+)일.*").match(date)
if md_match:
return datetime.date(int(year), int(md_match.group(1)), int(md_match.group(2)))
d_match = re.compile("([0-9]+)일.*").match(date)
if d_match:
return datetime.date(int(year), int(month), int(d_match.group(1)))
calendar = Calendar()
academic_calendar_soup = BeautifulSoup(
open("UNIST-academic-calendar-2023.html", "r").read(), "html.parser"
)
semestor_tag = academic_calendar_soup.find(
"div", attrs={"class": "semester_cal_ro year2023"}
)
monthly_tags = semestor_tag.find_all("tbody")
year = 2023
for tag in monthly_tags:
month = tag.find("th", attrs={"class": "tac"}).text
if "년" in month:
split_text = month.split()
year = split_text[0][:-1]
month = split_text[1]
events = tag.find_all("tr")
for event in events:
td_tags = event.find_all("td")
if len(td_tags) == 0:
continue
date_info = td_tags[0].text
events = list(map(lambda e: e.strip(), td_tags[1].text.strip().split("\n")))
period_match = re.compile("(.*)\~(.*)").match(date_info)
if period_match:
start_date = parse_date(year, month, period_match.group(1).strip())
end_date = parse_date(year, month, period_match.group(2).strip())
else:
start_date = parse_date(year, month, date_info)
end_date = None
event_info = {
"events": events,
"start_date": start_date,
"end_date": end_date,
}
for event_name in event_info["events"]:
ic_event = Event()
ic_event.add("summary", event_name)
ic_event.add("dtstart", event_info["start_date"])
if event_info["end_date"]:
ic_event.add("dtend", event_info["end_date"])
ic_event["dtstart"].params["VALUE"] = "DATE"
if event_info["end_date"]:
ic_event["dtend"].params["VALUE"] = "DATE"
calendar.add_component(ic_event)
f = open("UNIST_academic_calendar_2023.ics", "wb")
f.write(calendar.to_ical())
f.close()