Add api client

This commit is contained in:
Peder Bergebakken Sundt 2022-10-11 02:53:15 +02:00
commit c31b17bb8e
4 changed files with 129 additions and 0 deletions

3
.envrc Normal file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
use nix
export CARDS_ACCESS_TOKEN="hunter2"

87
api.py Executable file
View File

@ -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()

27
pull.sh Executable file
View File

@ -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

12
shell.nix Normal file
View File

@ -0,0 +1,12 @@
with import <nixpkgs> {};
pkgs.mkShell {
packages = with pkgs; [
jq
hjson # to deal with broken json
j2cli
sass
python3Packages.rich
python3Packages.httpx
python3Packages.typer
];
}