Migrate to central appointment database #45

Merged
christian merged 34 commits from sync-calendar into develop 2025-01-20 13:11:20 +00:00
5 changed files with 134 additions and 0 deletions
Showing only changes of commit 7117042fd0 - Show all commits

View File

@ -0,0 +1,32 @@
from . import cli
from . import login, sync
import logging
_logMap = {
0: logging.WARNING,
1: logging.INFO,
2: logging.DEBUG
}
_runMap = {
'login': login.run,
'sync': sync.run,
}
def main():
args = cli.getArgs(
loginSpCb=login.buildSubparser,
syncSpCb=sync.buildSubparser
)
logging.basicConfig()
_l = logging.getLogger(__name__)
_l.setLevel(_logMap.get(args.verbose, logging.DEBUG))
_l.debug('Parameters %s', args)
if args.mode in _runMap:
_runMap[args.mode](args)
else:
_l.error('Unknown mode %s', args.mode)

View File

@ -0,0 +1,3 @@
import calendar_synchronizer
calendar_synchronizer.main()

View File

@ -0,0 +1,16 @@
import argparse
def getArgs(loginSpCb, syncSpCb):
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='count', default=0, help='Increase the verbosity')
subparsers = parser.add_subparsers(dest='mode')
loginSubparser = subparsers.add_parser('login')
loginSpCb(loginSubparser)
syncSubparser = subparsers.add_parser('sync')
syncSpCb(syncSubparser)
# parser.add_argument("url", help="The URL to try and cache")
return parser.parse_args()

View File

@ -0,0 +1,61 @@
import os
import requests
import logging
import json
def buildSubparser(subparser):
subparser.add_argument('--url', default='https://cloud.tsc-vfl.de')
def run(args):
l = logging.getLogger(__name__)
l.debug('Login to %s', args.url)
url = f'{args.url}/index.php/login/v2'
l.debug('Using login url %s', url)
startRequest = requests.post(url)
startRequest.raise_for_status()
data = startRequest.json()
print('You need to login in the browser now to validate the login token.')
print('Please visit the following url:')
print(data['login'])
print()
print('After you have logged in, press enter to continue.')
input()
l.debug('Logging in')
intermediateToken = data['poll']['token']
pollUrl = data['poll']['endpoint']
loginRequest = requests.post(pollUrl, data={'token': intermediateToken})
loginRequest.raise_for_status()
data = loginRequest.json()
loginName = data['loginName']
appPassword = data['appPassword']
data = {
'loginName': loginName,
'appPassword': appPassword,
'base': data['server']
}
with open('login.json', 'w') as f:
json.dump(data, f)
l.info('Login successful')
class LoginData:
def __init__(self, loginName, appPassword):
self.loginName = loginName
self.appPassword = appPassword
def loadLoginData():
with open('login.json', 'r') as f:
data = json.load(f)
return LoginData(data['loginName'], data['appPassword'])

View File

@ -0,0 +1,22 @@
import logging, json
from . import login
_l = logging.getLogger(__name__)
def buildSubparser(subparser):
subparser.add_argument('--schedule', default='../../data/schedule.json')
subparser.add_argument('--holidays', default='../../data/holidays.json')
def run(args):
_l.info('Loading data from hard disc')
loginData = login.loadLoginData()
with open(args.schedule, 'r') as f:
schedule = json.load(f)
with open(args.holidays, 'r') as f:
holidays = json.load(f)
_l.info('Data was read from hard disc')