calendar-synchronization/src/twomartens/calendarsync/calendarsync.py

69 lines
2.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Copyright 2018 Jim Martens
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""calendarsync.calendarsync: provides entry point main()"""
import argparse
import datetime
from os.path import isdir
import pkg_resources
from ics import Calendar
from urllib.request import urlopen
from urllib.parse import urlparse, ParseResult
def main() -> None:
"""
Main entry point.
"""
parser = argparse.ArgumentParser(
description="Synchronizes jekyll event collection with remote calendar",
)
parser.add_argument('calendar_url', help='URL to the remote calendar ICS location')
parser.add_argument('event_collection_path', help='Path to event collection directory')
args = parser.parse_args()
# handling exceptions
parsed_url: ParseResult = urlparse(args.calendar_url)
if parsed_url.netloc == '' or parsed_url.scheme == '':
raise ValueError('calendar_url: The given calendar url is not a proper URL.')
if not isdir(args.event_collection_path):
raise ValueError('event_collection_path: The given event collection path is not a directory.')
calendar = Calendar(urlopen(args.calendar_url).read().decode('utf-8'))
template_filename = pkg_resources.resource_filename(
__package__,
'event_template.markdown'
)
with open(template_filename, 'r', encoding='utf-8') as template:
template_content = template.read()
for event in calendar.events:
event_content = template_content.replace('<name>', event.name)
created: datetime.datetime = event.created
created_str = created.isoformat(sep=' ')
event_content = event_content.replace('<date>', created_str)
begin: datetime.datetime = event.begin
begin_str = begin.isoformat(sep=' ')
event_content = event_content.replace('<begin>', begin_str)
end: datetime.datetime = event.end
end_str = end.isoformat(sep=' ')
event_content = event_content.replace('<end>', end_str)
event_content = event_content.replace('<location>', event.location)
event_filename = begin.date().isoformat() + '-' + event.name.replace(' ', '_') + '.markdown'
with open(args.event_collection_path + event_filename, 'w', encoding='utf-8', newline='\n') as event_file:
event_file.write(event_content)