From 4e8c0a74aacf06917ad3ad5cc21c214115b05d0d Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Wed, 18 Jul 2018 14:29:27 +0200 Subject: [PATCH] Implemented calendar synchronization Signed-off-by: Jim Martens --- src/twomartens/calendarsync/calendarsync.py | 47 ++++++++++++++++++- .../calendarsync/event_template.markdown | 8 ++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/twomartens/calendarsync/event_template.markdown diff --git a/src/twomartens/calendarsync/calendarsync.py b/src/twomartens/calendarsync/calendarsync.py index e95bd91..60b6a03 100644 --- a/src/twomartens/calendarsync/calendarsync.py +++ b/src/twomartens/calendarsync/calendarsync.py @@ -15,11 +15,54 @@ # 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. """ - pass - + 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('', event.name) + created: datetime.datetime = event.created + created_str = created.isoformat(sep=' ') + event_content = event_content.replace('', created_str) + begin: datetime.datetime = event.begin + begin_str = begin.isoformat(sep=' ') + event_content = event_content.replace('', begin_str) + end: datetime.datetime = event.end + end_str = end.isoformat(sep=' ') + event_content = event_content.replace('', end_str) + event_content = event_content.replace('', 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) diff --git a/src/twomartens/calendarsync/event_template.markdown b/src/twomartens/calendarsync/event_template.markdown new file mode 100644 index 0000000..b024f19 --- /dev/null +++ b/src/twomartens/calendarsync/event_template.markdown @@ -0,0 +1,8 @@ +--- +layout: event +title: +date: +start_date: +end_date: +location: +---