diff --git a/bbbmon/bbbmon.py b/bbbmon/bbbmon.py index ff8fd62bb8be9bef66d34896748d4cc9ad932647..6667bd42c51508ef25f6b274f16f9bf1d0ad4eba 100755 --- a/bbbmon/bbbmon.py +++ b/bbbmon/bbbmon.py @@ -141,6 +141,25 @@ def config(ctx, userconfig, watch, new, edit, path, print_): click.echo(ctx.get_help()) +@main.command(context_settings=CONTEXT_SETTINGS) +@click.pass_context +@click.option('--watch', '-w', help="Run repeatedly with the given interval in seconds", type=click.IntRange(2, 2147483647, clamp=True)) +@click.option('--compact/--pretty', '-c/-p', default=False, show_default=True, help="Print pretty and indented json or compact") +@click.option('--userconfig', '-u', is_flag=True, help="Use user config even if on server") +@click.option('--endpoint', '-e', multiple=True, help="Filter by one or more endpoints as named in the user configuration (e.g. [servername]). Order is respected.") +def json(ctx, userconfig, watch, endpoint, compact): + """Print json""" + config = init_config(userconfig) + config.filter_endpoints(endpoint) + if not config.endpoints: + exit() + if watch is not None: + while watch is not None: + print(format_json(config, watch, compact)) + time.sleep(watch) + else: + print(format_json(config, watch, compact)) + diff --git a/bbbmon/meetings.py b/bbbmon/meetings.py index 66340b86909074927208ce62ef585a0cbc83ba8b..0dc7413e90908141ecfc44a3a2378f30fde3a408 100644 --- a/bbbmon/meetings.py +++ b/bbbmon/meetings.py @@ -4,6 +4,7 @@ import time import subprocess import hashlib +import json from datetime import datetime, timedelta import requests from xml.etree import cElementTree as ElementTree @@ -269,3 +270,18 @@ def meetings_twolines(config: Config, watch: int, fancy: bool): lines = ["{:^60}".format(l[:61]) for l in lines] lines = "\n".join(lines) print(lines) + + +def format_json(config: Config, watch: bool, compact: bool) -> str: + meetings = [get_meetings(e.secret, e.url, config.path) for e in config.endpoints] + + # Clear screen after request is done, and before printing new data to keep + # blinking to a minimum + if watch is not None: + click.clear() + + + if compact: + return str(json.dumps(meetings)) + else: + return str(json.dumps(meetings, indent=4)) diff --git a/bbbmon/printing.py b/bbbmon/printing.py index 3abf455673128cb06f32763cc59ceaac7770ace7..0c8d4fb6a101e6d161e95998c5a4df101fcdf034 100644 --- a/bbbmon/printing.py +++ b/bbbmon/printing.py @@ -7,6 +7,7 @@ from typing import NewType, Optional, Tuple, Iterable, List from bbbmon.xmldict import XmlListConfig, XmlDictConfig import bbbmon.meetings +from bbbmon.configuration import Config diff --git a/pyproject.toml b/pyproject.toml index c23cef61d883f58ca7235eee41b90b624a30e4ed..8f431a3c593916a282bc7e941c11d9db4072eacf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "bbbmon" -version = "0.1.22" +version = "0.1.23" description = "A small CLI utility to monitor bbb usage" authors = ["David Huss <david.huss@hfbk-hamburg.de>"] maintainers = ["David Huss <david.huss@hfbk-hamburg.de>"]