From 7117042fd019f2622267b3e405fac094ccd9c734 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Mon, 13 Jan 2025 15:58:45 +0100 Subject: [PATCH] Create script to create NC app token --- .../calendar_synchronizer/__init__.py | 32 ++++++++++ .../calendar_synchronizer/__main__.py | 3 + .../nc-cal-sync/calendar_synchronizer/cli.py | 16 +++++ .../calendar_synchronizer/login.py | 61 +++++++++++++++++++ .../nc-cal-sync/calendar_synchronizer/sync.py | 22 +++++++ 5 files changed, 134 insertions(+) create mode 100644 scripts/nc-cal-sync/calendar_synchronizer/__init__.py create mode 100644 scripts/nc-cal-sync/calendar_synchronizer/__main__.py create mode 100644 scripts/nc-cal-sync/calendar_synchronizer/cli.py create mode 100644 scripts/nc-cal-sync/calendar_synchronizer/login.py create mode 100644 scripts/nc-cal-sync/calendar_synchronizer/sync.py diff --git a/scripts/nc-cal-sync/calendar_synchronizer/__init__.py b/scripts/nc-cal-sync/calendar_synchronizer/__init__.py new file mode 100644 index 0000000..bcedc8a --- /dev/null +++ b/scripts/nc-cal-sync/calendar_synchronizer/__init__.py @@ -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) + diff --git a/scripts/nc-cal-sync/calendar_synchronizer/__main__.py b/scripts/nc-cal-sync/calendar_synchronizer/__main__.py new file mode 100644 index 0000000..4b443c9 --- /dev/null +++ b/scripts/nc-cal-sync/calendar_synchronizer/__main__.py @@ -0,0 +1,3 @@ +import calendar_synchronizer + +calendar_synchronizer.main() diff --git a/scripts/nc-cal-sync/calendar_synchronizer/cli.py b/scripts/nc-cal-sync/calendar_synchronizer/cli.py new file mode 100644 index 0000000..27e1cba --- /dev/null +++ b/scripts/nc-cal-sync/calendar_synchronizer/cli.py @@ -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() diff --git a/scripts/nc-cal-sync/calendar_synchronizer/login.py b/scripts/nc-cal-sync/calendar_synchronizer/login.py new file mode 100644 index 0000000..6ee8720 --- /dev/null +++ b/scripts/nc-cal-sync/calendar_synchronizer/login.py @@ -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']) + \ No newline at end of file diff --git a/scripts/nc-cal-sync/calendar_synchronizer/sync.py b/scripts/nc-cal-sync/calendar_synchronizer/sync.py new file mode 100644 index 0000000..809fc1c --- /dev/null +++ b/scripts/nc-cal-sync/calendar_synchronizer/sync.py @@ -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') +