sync_ics2gcal/sync_ics2gcal/sync_calendar.py

54 lines
1.3 KiB
Python
Raw Permalink Normal View History

2022-02-23 10:11:56 +00:00
from typing import Dict, Any, Union
2021-04-30 08:46:10 +00:00
import yaml
import dateutil.parser
import datetime
import logging
import logging.config
2022-03-08 15:46:21 +00:00
from . import CalendarConverter, GoogleCalendarService, GoogleCalendar, CalendarSync
2022-02-23 10:11:56 +00:00
ConfigDate = Union[str, datetime.datetime]
2020-03-08 10:10:42 +00:00
2021-04-30 08:46:10 +00:00
def load_config() -> Dict[str, Any]:
2022-03-08 15:46:21 +00:00
with open("config.yml", "r", encoding="utf-8") as f:
result = yaml.safe_load(f)
2022-06-04 15:47:54 +00:00
return result # type: ignore
2022-02-23 10:11:56 +00:00
def get_start_date(date: ConfigDate) -> datetime.datetime:
if isinstance(date, datetime.datetime):
return date
2022-03-08 15:46:21 +00:00
if "now" == date:
result = datetime.datetime.utcnow()
else:
2022-02-23 10:11:56 +00:00
result = dateutil.parser.parse(date)
return result
2022-06-04 14:12:21 +00:00
def main() -> None:
config = load_config()
2022-03-08 15:46:21 +00:00
if "logging" in config:
logging.config.dictConfig(config["logging"])
2022-03-08 15:46:21 +00:00
calendar_id: str = config["calendar"]["google_id"]
ics_filepath: str = config["calendar"]["source"]
2022-03-08 15:46:21 +00:00
start = get_start_date(config["start_from"])
converter = CalendarConverter()
converter.load(ics_filepath)
service = GoogleCalendarService.from_config(config)
2022-02-24 09:20:07 +00:00
gcalendar = GoogleCalendar(service, calendar_id)
sync = CalendarSync(gcalendar, converter)
sync.prepare_sync(start)
sync.apply()
2020-03-08 10:10:42 +00:00
2022-03-08 15:46:21 +00:00
if __name__ == "__main__":
main()