Commit 27ee5a27 authored by erdnaxe's avatar erdnaxe 🎇

Initial commit

parents
# IDE specific
.idea
# User specific
config.yml
# Python specific
__pycache__
import re
from datetime import date
from icalendar_tools import get_event_start_date
def filter_date(event) -> bool:
"""Return true if event day is today or in the future"""
event_date = get_event_start_date(event)
if event_date:
return event_date >= date.today()
else:
# If there is a bug in data then do not filter
return True
def filter_group(event, selected_groups) -> bool:
"""Filter group according to the group selected"""
summary = event.get('summary')
summary_search = re.search('([0-9]{3}) - ([A-Z]{2}) \(grpe : ([^\)]*)\)', summary, re.IGNORECASE)
if summary_search:
ue, group = summary_search.group(1), summary_search.group(3)
if int(ue) in selected_groups:
if group in selected_groups[int(ue)]:
# Group was selected
return True
else:
# Group is not the one selected
print('-- UE', ue, 'group', group, 'removed')
return False
# If there is not a group specified do not filter
return True
from datetime import datetime, date
from typing import Union
from icalendar import Calendar
from requests import get
from requests.auth import HTTPBasicAuth
def download_icalendar(url: str, login: str, password: str) -> Calendar:
"""Download and parse an icalendar file with authentication"""
# Download calendar
print('Downloading', url)
request = get(url, auth=HTTPBasicAuth(login, password))
content = request.text
# Parse to create a calendar
return Calendar.from_ical(content)
def get_event_start_date(event) -> Union[date, bool]:
"""Return start date of an event"""
event_date = event.decoded('dtstart')
if isinstance(event_date, datetime):
return event_date.date()
elif isinstance(event_date, date):
return event_date
else:
return False
from urllib.parse import urljoin
import keyring
import yaml
from filters import filter_date, filter_group
from icalendar_tools import download_icalendar, get_event_start_date
with open("config.yml", 'r') as f:
config = yaml.load(f)
base_url = config['base_url']
calendars_url = [urljoin(base_url, r) for r in config['calendars_url']]
login = config['login']
selected_groups = config['selected_groups']
password = keyring.get_password('sogo', login)
for url in calendars_url:
cal = download_icalendar(url, login, password)
for event in cal.walk():
# If it is an event in the future
if event.name == 'VEVENT' and filter_date(event) and filter_group(event, selected_groups):
print(event.get('summary'), get_event_start_date(event))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment