Files
mlbstats-mastodon-scripts/plays/mlbplaysave.sh
T
2024-08-08 22:17:11 -05:00

41 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
set -e
set -o pipefail
while getopts 'd:' opt
do
case $opt in
d)
db=$OPTARG
;;
?)
exit 1
;;
esac
done
if [[ -z $db ]]
then
echo "$0:" '-d is required' >&2
exit 1
fi
# grab the game pk of a single live game, if multiple (in case of spring
# training)
gamePk=$(sqlite3 $db "select json ->> 'gamePk' from games where json ->> 'status' ->> 'detailedState' in ('In Progress', 'Warmup') limit 1")
if [[ -z "$gamePk" ]]
then
echo "$0:" 'no live games found' >&2
exit 1
fi
jqFilter='{gamePk} + (.gameData.teams | {awayTeam: .away.teamName, homeTeam: .home.teamName}) + (.liveData.plays.allPlays[] | (.result + (.about | {atBatIndex, halfInning, inning, isComplete, isScoringPlay, hasReview})))'
fmt='OFS=""; print $0, 0'
save="\"sqlite3 $db '.mode ascii' '.separator ' '.import /dev/stdin plays'\""
# grab select data from each response with jq, add 0 to the end of each line
# (to go in the 'posted' db column), then write each line as they come in to db
mlblive subscribe -g $gamePk | jq -Sc "$jqFilter" | awk "{$fmt | $save; close($save)}"