saphsync.py 1.6 KB
Newer Older
erdnaxe's avatar
erdnaxe committed
1
import logging
erdnaxe's avatar
erdnaxe committed
2

erdnaxe's avatar
erdnaxe committed
3
from icalendar import Calendar
erdnaxe's avatar
erdnaxe committed
4

erdnaxe's avatar
erdnaxe committed
5
from configuration import Configuration
erdnaxe's avatar
erdnaxe committed
6
from filters import filter_date, filter_group
erdnaxe's avatar
erdnaxe committed
7 8 9 10 11
from icalendar_tools import OnlineCalendar

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
config = Configuration('config.yml')

erdnaxe's avatar
erdnaxe committed
12
# Create master calendar
erdnaxe's avatar
erdnaxe committed
13
calendar = Calendar()
erdnaxe's avatar
erdnaxe committed
14
calendar.add('version', '2.0')
erdnaxe's avatar
erdnaxe committed
15 16 17
calendar.add('prodid', '-//SaphSync//EN')
calendar.add('method', 'PUBLISH')
calendar.add('x-wr-calname', 'SaphSync')
erdnaxe's avatar
erdnaxe committed
18 19

# Fetch online calendar and copy components
erdnaxe's avatar
erdnaxe committed
20 21 22
for url in config.calendars_url:
    online_calendar = OnlineCalendar(url, config.login, config.password)

erdnaxe's avatar
erdnaxe committed
23 24 25 26 27
    # Copy timezone definitions
    tz = online_calendar.get_timezone()
    calendar.add_component(tz)

    # Copy events
erdnaxe's avatar
erdnaxe committed
28
    for event in online_calendar.get_events():
erdnaxe's avatar
erdnaxe committed
29
        # If it is an event more recent than past week and in the correct group
erdnaxe's avatar
erdnaxe committed
30 31 32 33
        if filter_date(event) and filter_group(event, config.selected_groups):
            logging.debug('An event was added : {}'.format(event.get('summary')))
            calendar.add_component(event)

erdnaxe's avatar
erdnaxe committed
34
    # Copy tasks
erdnaxe's avatar
erdnaxe committed
35 36 37 38
    for todo in online_calendar.get_todos():
        logging.debug('A task was added : {}'.format(todo.get('summary')))
        calendar.add_component(todo)

erdnaxe's avatar
erdnaxe committed
39
    # Copy other components
erdnaxe's avatar
erdnaxe committed
40 41 42 43 44 45 46
    for component in online_calendar.get_other_components():
        logging.info('A unrecognised component ({}) was added : {}'.format(component.name, component.to_ical()))
        calendar.add_component(component)

# Write resulting calendar
with open('calendar.ics', 'wb') as f:
    f.write(calendar.to_ical())