Commit a4c2bb20 authored by erdnaxe's avatar erdnaxe 🎇

First pass, remove useless code and get components with CalDav

parent 180f58bc
url: ''
ue_101: ['TD_3', 'TP_1', 'BE_1']
ue_102: ['TD_3', 'TP_1', 'BE_1']
......@@ -10,10 +10,3 @@ selected_groups:
ue_107: ['TD_3', 'TP_1', 'BE_1']
ue_112: ['TD_1', 'TP_1', 'BE_1']
ue_113: ['TD_1', 'TP_1', 'BE_1']
base_url: ''
- 'PUT_YOUR_USERNAME/Calendar/personal.ics'
- 'OTHER_USERNAME_TO_SUSCRIBE/Calendar/example.ics'
......@@ -10,10 +10,9 @@ class Configuration:
def __init__(self, yaml_file):
with open(yaml_file, 'r') as f:
config = yaml.load(f)
self.base_url = config['base_url']
self.calendars_url = [urljoin(self.base_url, r) for r in config['calendars_url']]
self.login = config['login']
self.url = config['url']
self.username = config['username']
self.selected_groups = config['selected_groups']
# Load password
self.password = keyring.get_password('sogo', self.login)
self.password = keyring.get_password('sogo', self.username)
import logging
import re
from datetime import date, timedelta
from icalendar_tools import get_event_start_date
logger = logging.getLogger('Filters')
def filter_date(event) -> bool:
"""Return true if event day is after the beginning of last week"""
event_date = get_event_start_date(event)
if event_date:
from_date = - timedelta(weeks=1)
return event_date >= from_date
# If there is a bug in data then do not filter
summary = event.get('summary')
logger.warn('There was an issue with the date of {}'.format(summary))
return True
def filter_group(event, selected_groups) -> bool:
"""Filter group according to the group selected"""
summary = event.get('summary')
import logging
from datetime import datetime, date
from typing import Union
from icalendar import Calendar
from requests import get
from requests.auth import HTTPBasicAuth
class OnlineCalendar:
logger = logging.getLogger('OnlineCalendar')
def __init__(self, url: str, login: str, password: str):
"""Download and parse an icalendar file with authentication""""Downloading {}".format(url))
request = get(url, auth=HTTPBasicAuth(login, password))
content = request.text
# Parse components
self.components = Calendar.from_ical(content)
def get_events(self):
"""Return all calendar events"""
for component in self.components.walk():
if == 'VEVENT':
yield component
def get_todos(self):
"""Return all calendar tasks"""
for component in self.components.walk():
if == 'VTODO':
yield component
def get_timezone(self):
"""Return one timezone"""
for component in self.components.walk():
if == 'VTIMEZONE':
return component
def get_other_components(self):
"""Return all unrecognised components"""
for component in self.components.walk():
if not in blacklist:
yield component
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):
elif isinstance(event_date, date):
return event_date
return False
......@@ -2,12 +2,35 @@ import logging
from icalendar import Calendar
from configuration import Configuration
from filters import filter_date, filter_group
from icalendar_tools import OnlineCalendar
import configuration
import filters
from caldav.objects import dav
from datetime import datetime, timedelta
from caldav import DAVClient
def get_calendars(url, username, password):
""" Get all calendars from DAV server """"Downloading {}".format(url))
client = DAVClient(url, username=username, password=password)
principal = client.principal()
calendars = principal.calendars()
return calendars
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
config = Configuration('config.yml')
config = configuration.Configuration('config.yml')
dav_calendars = get_calendars(config.url, config.username, config.password)
for calendar in dav_calendars:
properties = calendar.get_properties([dav.DisplayName(), ])
name = properties['{DAV:}displayname']
print("Traitement de " + name)
from_date = - timedelta(weeks=1)
events = calendar.date_search(from_date)
todos = calendar.todos()
journals = calendar.journals()
print(events, todos, journals)
# Create master calendar
calendar = Calendar()
......@@ -16,9 +39,10 @@ calendar.add('prodid', '-//SaphSync//EN')
calendar.add('method', 'PUBLISH')
calendar.add('x-wr-calname', 'SaphSync')
# Fetch online calendar and copy components
for url in config.calendars_url:
online_calendar = OnlineCalendar(url, config.login, config.password)
online_calendar = OnlineCalendar(config.url, config.login, config.password)
# Copy timezone definitions
tz = online_calendar.get_timezone()
......@@ -44,3 +68,4 @@ for url in config.calendars_url:
# Write resulting calendar
with open('calendar.ics', 'wb') as f:
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