From c31b17bb8e8406f9c939725b18ef78d54fbaaa4f Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Tue, 11 Oct 2022 02:53:15 +0200 Subject: [PATCH] Add api client --- .envrc | 3 ++ api.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ pull.sh | 27 +++++++++++++++++ shell.nix | 12 ++++++++ 4 files changed, 129 insertions(+) create mode 100644 .envrc create mode 100755 api.py create mode 100755 pull.sh create mode 100644 shell.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..8eec315 --- /dev/null +++ b/.envrc @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +use nix +export CARDS_ACCESS_TOKEN="hunter2" diff --git a/api.py b/api.py new file mode 100755 index 0000000..f01e898 --- /dev/null +++ b/api.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python +import typer +import os +import httpx +import rich +#from rich.syntax import Syntax +import pygments.lexers +import pygments.formatters + + +def post(cmd: str, **data) -> httpx.Response: + return httpx.post("https://www.pvv.ntnu.no/~andreasd/cards/command.php", data=dict( + access_token = ACCESS_TOKEN, + cmd = cmd, + **data + )) + +def print_code(html: str, lexer: str): + #rich.print(Syntax(html, "html")) # is shit + if not os.isatty(1): + print( html ) + else: + print( + pygments.highlight( + html, + pygments.lexers.get_lexer_by_name(lexer), + pygments.formatters.TerminalFormatter(), + #pygments.formatters.Terminal256Formatter(), + #pygments.formatters.TerminalTrueColorFormatter(), + ) + ) + + +app = typer.Typer( + no_args_is_help = True, +) + + +@app.command() +def get_card_groups(): + rich.print_json(data=post("get_my_cardgroups").json()) + +@app.command() +def get_card_group(group_id: int): + rich.print_json(data=post("get_cards_in_group", **locals()).json()) + +@app.command() +def get_card_rendered(card_id: int): + print_code(post("get_card_rendered", **locals()).text, "html") + +@app.command() +def get_card(card_id: int): + print_code(post("get_card_xml", **locals()).text, "xml") + +@app.command() +def set_card(card_id: int, file: typer.FileText): + value = file.read() + del file + print_code(post("set_card_xml", **locals()).text, "html") + +@app.command() +def get_styles(): + # this one fails due to trailing comma + #rich.print_json(data=post("get_my_styles").json()) + rich.print(post("get_my_styles").text) + +@app.command() +def get_style(style_id: int): + print_code(post("get_style_data", **locals()).text, "html") + +@app.command() +def get_style_dimensions(style_id: int): + rich.print_json(data=post("get_style_dimensions", **locals()).json()) + +@app.command() +def set_style(style_id: int, file: typer.FileText): + value = file.read() + del file + print_code(post("set_style_data", **locals()).text, "html") + + +if __name__ == "__main__": + if "CARDS_ACCESS_TOKEN" not in os.environ: + print("ERROR: environment variable 'CARDS_ACCESS_TOKEN' not set") + exit(1) + ACCESS_TOKEN = os.environ["CARDS_ACCESS_TOKEN"] + app() diff --git a/pull.sh b/pull.sh new file mode 100755 index 0000000..06a4e5e --- /dev/null +++ b/pull.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +test -d pvv && rm -rfv pvv +test -e pvv && { + >&2 echo "'pvv' is not a directory" + exit 1 +} +mkdir -p pvv/{cards,styles} + +./api.py get-styles | hjson -c | jq '.[].id' --raw-output | +while read style_id; do + (set -x + ./api.py get-style $style_id > pvv/styles/$style_id.xml + ) & +done + +./api.py get-card-groups | jq '.[] | select(has("share")) | .id' --raw-output | +while read group_id; do + ./api.py get-card-group $group_id | jq '.[].id' --raw-output | + while read card_id; do + (set -x + ./api.py get-card $card_id > pvv/cards/$card_id.xml + ) & + done +done + +wait diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..508e29a --- /dev/null +++ b/shell.nix @@ -0,0 +1,12 @@ +with import {}; +pkgs.mkShell { + packages = with pkgs; [ + jq + hjson # to deal with broken json + j2cli + sass + python3Packages.rich + python3Packages.httpx + python3Packages.typer + ]; +}