2018-07-18 12:39:35 +02:00
|
|
|
# -*- 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()"""
|
2018-07-18 14:29:27 +02:00
|
|
|
import argparse
|
|
|
|
import datetime
|
2018-07-18 17:21:11 +02:00
|
|
|
import glob
|
|
|
|
import os
|
2018-07-18 14:29:27 +02:00
|
|
|
from os.path import isdir
|
|
|
|
|
|
|
|
import pkg_resources
|
|
|
|
from ics import Calendar
|
|
|
|
from urllib.request import urlopen
|
|
|
|
from urllib.parse import urlparse, ParseResult
|
2018-07-18 12:39:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
"""
|
|
|
|
Main entry point.
|
|
|
|
"""
|
2018-07-18 14:29:27 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Synchronizes jekyll event collection with remote calendar",
|
|
|
|
)
|
2018-07-18 15:01:21 +02:00
|
|
|
parser.add_argument('calendar_url', type=validate_url, help='URL to the remote calendar ICS location')
|
|
|
|
parser.add_argument('event_collection_path', type=validate_dir, help='Path to event collection directory')
|
2018-07-18 14:29:27 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-07-18 14:49:02 +02:00
|
|
|
sync(args.calendar_url, args.event_collection_path)
|
|
|
|
|
|
|
|
|
|
|
|
def sync(calendar_url: str, event_collection_path: str) -> None:
|
|
|
|
"""
|
|
|
|
Synchronizes the event collection with a remote calendar.
|
|
|
|
|
|
|
|
:param calendar_url: URL to remote calendar ICS
|
|
|
|
:param event_collection_path: path to event collection directory
|
|
|
|
"""
|
|
|
|
calendar = Calendar(urlopen(calendar_url).read().decode('utf-8'))
|
2018-07-18 14:29:27 +02:00
|
|
|
template_filename = pkg_resources.resource_filename(
|
|
|
|
__package__,
|
|
|
|
'event_template.markdown'
|
|
|
|
)
|
|
|
|
with open(template_filename, 'r', encoding='utf-8') as template:
|
|
|
|
template_content = template.read()
|
2018-07-18 17:21:11 +02:00
|
|
|
|
|
|
|
# remove previous event files in directory to allow the removal of events in calendar
|
|
|
|
files = glob.glob(event_collection_path + '*.markdown')
|
|
|
|
for file in files:
|
|
|
|
os.remove(file)
|
|
|
|
|
2018-07-18 14:29:27 +02:00
|
|
|
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)
|
2018-10-10 18:01:48 +02:00
|
|
|
location_info = event.location.split(sep=':')
|
|
|
|
event_content = event_content.replace('<location>', location_info[0])
|
|
|
|
if len(location_info) == 2:
|
|
|
|
event_content = event_content.replace('<address>', location_info[1])
|
2018-11-18 17:22:32 +01:00
|
|
|
else:
|
2018-11-19 12:56:35 +01:00
|
|
|
event_content = event_content.replace('<address>', location_info[0])
|
2018-07-18 14:49:02 +02:00
|
|
|
|
2018-07-18 14:29:27 +02:00
|
|
|
event_filename = begin.date().isoformat() + '-' + event.name.replace(' ', '_') + '.markdown'
|
2018-07-18 14:49:02 +02:00
|
|
|
with open(event_collection_path + event_filename, 'w', encoding='utf-8', newline='\n') as event_file:
|
2018-07-18 14:29:27 +02:00
|
|
|
event_file.write(event_content)
|
2018-07-18 15:01:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def validate_url(url: str) -> str:
|
|
|
|
"""
|
|
|
|
Validates a URL and returns it on success.
|
|
|
|
|
|
|
|
:param url: URL to verify
|
|
|
|
:return: verified URL
|
|
|
|
:raises: argparse.ArgumentTypeError if URL is invalid
|
|
|
|
"""
|
|
|
|
parsed_url: ParseResult = urlparse(url)
|
|
|
|
if parsed_url.netloc == '' or parsed_url.scheme == '':
|
|
|
|
raise argparse.ArgumentTypeError(f"'{url}' is not a valid URL.")
|
|
|
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
def validate_dir(directory: str) -> str:
|
|
|
|
"""
|
|
|
|
Validates a directory path and returns it on success.
|
|
|
|
|
|
|
|
:param directory: path to directory to verify
|
|
|
|
:return: verified directory path
|
|
|
|
:raises: argparse.ArgumentTypeError if directory does not exist
|
|
|
|
"""
|
|
|
|
if not isdir(directory):
|
|
|
|
raise argparse.ArgumentTypeError(f"'{directory}' is not an existing directory.")
|
|
|
|
return directory
|