This repository was archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (39 loc) · 1.49 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
from datetime import datetime, timedelta
import itertools
from bs4 import BeautifulSoup
from icalendar import Calendar, Event
import requests
def scrape_holidays(year=None):
holidays = []
year_path = f'{year}-dates/' if year else ''
url = f'https://publicholidays.com.mt/{year_path}'
res = requests.get(url)
root = BeautifulSoup(res.text, "html.parser")
events = root.find_all('tr', class_='vevent')
for event in events:
date_string = event.find('time').attrs['datetime']
date = datetime.strptime(date_string, '%Y-%m-%d').date()
name = event.find(class_='summary').text
holidays.append(dict(date=date, name=name))
return holidays
def create_icsv(holidays, filename='malta-holidays.ics'):
# print(holidays)
cal = Calendar()
cal.add('prodid', '-//Malta Public Holidays//github.com/PyMalta/malta-holidays//EN')
cal.add('version', '2.0')
for holiday in holidays:
event = Event()
event.add('summary', holiday['name'])
event.add('dtstart', holiday['date'])
event.add('dtend', holiday['date'] + timedelta(days=1))
event.add('dtstamp', datetime.now())
cal.add_component(event)
with open(filename, 'wb') as f:
f.write(cal.to_ical())
return filename
if __name__ == '__main__':
years = [None, 2018, 2019]
holidays = itertools.chain(*[scrape_holidays(year) for year in years])
print(holidays)
filename = create_icsv(holidays)
print(f'Created ICS {filename}')