-
Notifications
You must be signed in to change notification settings - Fork 0
/
ph2ical.py
51 lines (45 loc) · 1.65 KB
/
ph2ical.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
import logging
from bs4 import BeautifulSoup
import sys
import requests
from datetime import datetime, timedelta
from icalendar import Calendar, Event
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
if __name__ == "__main__":
# get source code from publicholidays.com
year = sys.argv[1]
url = f'https://publicholidays.ch/bern/{year}-dates/'
r = requests.get(url)
log.info(f'GET {url} STATUS-CODE={r.status_code}')
html = r.text
# parse the source code for holidays
holidays = []
soup = BeautifulSoup(html, 'html.parser')
table = soup.find('table', {'class': 'publicholidays phgtable'})
tablebody = table.find('tbody')
for row in tablebody.find_all('tr'):
holiday = row.find_all('td')
if len(holiday) == 3:
daymonth = holiday[0].text.lstrip('0')
date = datetime.strptime(f'{daymonth} {year}', '%d %b %Y')
day = date.strftime('%a')
name = holiday[2].text.strip()
log.info(f'parsed holiday from webpagge: {day}, {date.date()} ({name})')
holidays.append({'date': date, 'name': name})
# create ical file
cal = Calendar()
for holiday in holidays:
event = Event()
dtstart = holiday['date']
dtend = dtstart + timedelta(days=1)
event.add('dtstart', dtstart.date())
event.add('dtend', dtend.date())
event.add('summary', holiday['name'])
event.add('description', f'#holiday {holiday["name"]}')
log.debug(event)
cal.add_component(event)
f = open(f'swissholidays_{year}.ical', 'wb')
f.write(cal.to_ical())
f.close()