From 0c7c0153aae68ae117f6539e38aec710ba257381 Mon Sep 17 00:00:00 2001
From: atoav <dh@atoav.com>
Date: Thu, 7 May 2020 12:09:26 +0200
Subject: [PATCH] Add json command

---
 bbbmon/bbbmon.py   | 19 +++++++++++++++++++
 bbbmon/meetings.py | 16 ++++++++++++++++
 bbbmon/printing.py |  1 +
 pyproject.toml     |  2 +-
 4 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/bbbmon/bbbmon.py b/bbbmon/bbbmon.py
index ff8fd62..6667bd4 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 66340b8..0dc7413 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 3abf455..0c8d4fb 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 c23cef6..8f431a3 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>"]
-- 
GitLab