stream_lichess.sh (2186B)
1 #!/bin/sh 2 # Stream a lichess game, by game id. 3 # Requires permissions to the board / game. 4 # NOTE that it updates after a move. 5 # Dependencies: curl, json2tsv (could be replaced by jq). 6 7 if [ "$1" = "" ]; then 8 echo "Usage: $0 <gameid>" >&2 9 exit 1 10 fi 11 12 gameid="$1" 13 token="${LICHESS_TOKEN}" # API token here. 14 15 url="https://lichess.org/api/board/game/stream/$gameid" 16 17 # start position of classical chess. 18 fen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" 19 20 firstline=1 21 header="" 22 23 # -N disables cURL buffering, each line is streamed as JSON. 24 curl \ 25 -f \ 26 -s \ 27 -N \ 28 -H "Authorization: Bearer $token" \ 29 -H 'Accept: application/x-ndjson' "$url" | \ 30 while read -r json; do 31 moveplayed="0" 32 if [ "$firstline" = "1" ]; then 33 firstline="0" 34 35 str=$(printf '%s' "$json" | jaq '$1 == ".initialFen" { print $3; }') 36 test "$str" != "" && fen="$str" # override 37 38 header=$(printf '%s' "$json" | jaq ' 39 BEGIN { 40 white_name = "Anonymous"; 41 black_name = "Anonymous"; 42 } 43 $2 == "?" || $3 == "" { next; } 44 $1 == ".white.name" { white_name = $3; } 45 $1 == ".white.rating" { white_rating = $3; } 46 $1 == ".white.provisional" && $3 == "true" { white_provisional = "?"; } 47 $1 == ".black.name" { black_name = $3; } 48 $1 == ".black.rating" { black_rating = $3; } 49 $1 == ".black.provisional" && $3 == "true" { black_provisional = "?"; } 50 $1 == ".white.aiLevel" { white_name = "AI level " $3; } 51 $1 == ".black.aiLevel" { black_name = "AI level " $3; } 52 END { 53 white = white_name; 54 if (white_rating != "") 55 white = white " (" white_rating white_provisional ")"; 56 black = black_name; 57 if (black_rating != "") 58 black = black " (" black_rating black_provisional ")"; 59 print white " vs " black; 60 }') 61 fi 62 63 str=$(printf '%s' "$json" | jaq '$1 == ".moves" { print $3; }') 64 if [ "$str" != "" ]; then 65 moves="$str" # override 66 moveplayed="1" 67 fi 68 69 clear 70 printf '%s\n\n' "$header" 71 ./fen -o tty "$fen" "$moves" 72 73 if [ "$moveplayed" = "1" ]; then 74 speaktext="$(./fen -l -o speak "$fen" "$moves")" 75 fi 76 printf '\n%s\n' "$speaktext" 77 78 printf '\nMoves:\n' 79 printf '%s\n' "$moves" 80 81 printf '\nPGN:\n' 82 ./fen -o pgn "$fen" "$moves" 83 84 # audio 85 if [ "$moveplayed" = "1" ]; then 86 (printf '%s\n' "$speaktext" | espeak) & 87 fi 88 done