Implemented calendar synchronization

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
Jim Martens 2018-07-18 14:29:27 +02:00
parent 16e12b21dc
commit 4e8c0a74aa
2 changed files with 53 additions and 2 deletions

View File

@ -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('<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)

View File

@ -0,0 +1,8 @@
---
layout: event
title: <name>
date: <date>
start_date: <begin>
end_date: <end>
location: <location>
---