wp-cal-integration/main.py
2022-09-28 15:52:52 +02:00

66 lines
2 KiB
Python
Executable file

#!/usr/bin/env python3
import sys
import yaml
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
from adapters import *
class Config():
"""
The default configuration.
Keys:
.google.calendar_id: the id of the calendar to sync
.google.credentials: the json of the obtained credentials file
.google.token_file: where to save the login token
.wordpress.url: the base wordpress url
.wordpress.calendar.id: the id of the (wp-booking-system) calendar
.wordpress.calendar.name: the name of the calendar
.wordpress.calendar.translations: a dictionary of language <-> translation pairs (example: {"en": "Reservations"})
.wordpress.credentials.user: the user as which to log into wordpress
.wordpress.credentials.password: the users password
"""
def __init__(self, file):
self.file = file
self.config: dict | None = None
def load(self):
if self.file == '-':
config = yaml.load(sys.stdin, Loader=Loader)
else:
with open(self.file) as fp:
config = yaml.load(fp, Loader=Loader)
self.config = config
def __getitem__(self, name):
assert self.config is not None
return self.config[name]
if __name__ == '__main__':
config = Config('-')
config.load()
g = Google(
config['google']['calendar_id'],
credentials=config['google']['credentials'],
token_file=config['google'].get('token_file', '~/.wp-cal-integration-google-token.json')
)
w = Wordpress(
config['wordpress']['url'],
calendar_metadata=CalendarMetadata(
id=config['wordpress']['calendar']['id'],
name=config['wordpress']['calendar']['name'],
translations=config['wordpress']['calendar']['translations'],
),
credentials=config['wordpress']['credentials'],
)
g.login()
events = g.get_events()
w.login()
w.post_events(events)