2024-07-24 21:17:36 -05:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
2024-07-25 22:34:35 -05:00
|
|
|
|
set -e
|
2024-08-01 20:04:58 -05:00
|
|
|
|
set -o pipefail
|
2024-07-25 22:34:35 -05:00
|
|
|
|
|
2024-08-05 22:44:18 -05:00
|
|
|
|
while getopts 'd:' opt
|
2024-07-25 22:27:25 -05:00
|
|
|
|
do
|
|
|
|
|
|
case $opt in
|
2024-07-26 19:14:39 -05:00
|
|
|
|
d)
|
|
|
|
|
|
db=$OPTARG
|
|
|
|
|
|
;;
|
2024-07-25 22:27:25 -05:00
|
|
|
|
?)
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
;;
|
|
|
|
|
|
esac
|
|
|
|
|
|
done
|
|
|
|
|
|
|
2024-07-26 19:14:39 -05:00
|
|
|
|
if [[ -z $db ]]
|
2024-07-24 21:17:36 -05:00
|
|
|
|
then
|
2024-07-26 19:14:39 -05:00
|
|
|
|
echo "$0:" '-d is required' >&2
|
2024-07-24 21:17:36 -05:00
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
2024-08-03 15:54:35 -05:00
|
|
|
|
# grab the game pk of a single live game, if multiple (in case of spring
|
|
|
|
|
|
# training)
|
2024-08-08 22:17:11 -05:00
|
|
|
|
gamePk=$(sqlite3 $db "select json ->> 'gamePk' from games where json ->> 'status' ->> 'detailedState' in ('In Progress', 'Warmup') limit 1")
|
2024-08-03 15:54:35 -05:00
|
|
|
|
if [[ -z "$gamePk" ]]
|
2024-07-25 22:48:57 -05:00
|
|
|
|
then
|
2024-07-30 21:36:11 -05:00
|
|
|
|
echo "$0:" 'no live games found' >&2
|
2024-07-25 22:48:57 -05:00
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
2024-08-02 22:00:42 -05:00
|
|
|
|
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'
|
2024-08-03 17:35:08 -05:00
|
|
|
|
save="\"sqlite3 $db '.mode ascii' '.separator ' '.import /dev/stdin plays'\""
|
2024-08-02 22:00:42 -05:00
|
|
|
|
|
2024-07-31 21:42:56 -05:00
|
|
|
|
# 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
|
2024-08-02 22:52:58 -05:00
|
|
|
|
mlblive subscribe -g $gamePk | jq -Sc "$jqFilter" | awk "{$fmt | $save; close($save)}"
|