Add api client
This commit is contained in:
commit
c31b17bb8e
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/env bash
|
||||
use nix
|
||||
export CARDS_ACCESS_TOKEN="hunter2"
|
|
@ -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()
|
|
@ -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
|
Loading…
Reference in New Issue