2022-02-23 10:11:56 +00:00
|
|
|
from typing import Dict, Any, Union
|
2021-04-30 08:46:10 +00:00
|
|
|
|
2020-02-19 20:26:28 +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
|
2020-02-19 20:26:28 +00:00
|
|
|
|
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:
|
2020-02-19 20:26:28 +00:00
|
|
|
result = yaml.safe_load(f)
|
2022-06-04 15:47:54 +00:00
|
|
|
return result # type: ignore
|
2020-02-19 20:26:28 +00:00
|
|
|
|
|
|
|
|
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:
|
2020-02-19 20:26:28 +00:00
|
|
|
result = datetime.datetime.utcnow()
|
|
|
|
else:
|
2022-02-23 10:11:56 +00:00
|
|
|
result = dateutil.parser.parse(date)
|
2020-02-19 20:26:28 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2022-06-04 14:12:21 +00:00
|
|
|
def main() -> None:
|
2020-02-19 20:26:28 +00:00
|
|
|
config = load_config()
|
|
|
|
|
2022-03-08 15:46:21 +00:00
|
|
|
if "logging" in config:
|
|
|
|
logging.config.dictConfig(config["logging"])
|
2020-02-19 20:26:28 +00:00
|
|
|
|
2022-03-08 15:46:21 +00:00
|
|
|
calendar_id: str = config["calendar"]["google_id"]
|
|
|
|
ics_filepath: str = config["calendar"]["source"]
|
2020-02-19 20:26:28 +00:00
|
|
|
|
2022-03-08 15:46:21 +00:00
|
|
|
start = get_start_date(config["start_from"])
|
2020-02-19 20:26:28 +00:00
|
|
|
|
|
|
|
converter = CalendarConverter()
|
|
|
|
converter.load(ics_filepath)
|
|
|
|
|
2020-02-20 14:37:31 +00:00
|
|
|
service = GoogleCalendarService.from_config(config)
|
2022-02-24 09:20:07 +00:00
|
|
|
gcalendar = GoogleCalendar(service, calendar_id)
|
2020-02-19 20:26:28 +00:00
|
|
|
|
|
|
|
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__":
|
2020-02-19 20:26:28 +00:00
|
|
|
main()
|