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