2024-07-25 02:17:36 +00:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
2024-07-26 03:34:35 +00:00
|
|
|
|
set -e
|
2024-08-02 01:04:58 +00:00
|
|
|
|
set -o pipefail
|
2024-07-26 03:34:35 +00:00
|
|
|
|
|
2024-07-27 00:14:39 +00:00
|
|
|
|
while getopts 'd:t:' opt
|
2024-07-26 03:27:25 +00:00
|
|
|
|
do
|
|
|
|
|
case $opt in
|
2024-07-27 00:14:39 +00:00
|
|
|
|
d)
|
|
|
|
|
db=$OPTARG
|
|
|
|
|
;;
|
2024-07-26 03:27:25 +00:00
|
|
|
|
t)
|
|
|
|
|
team=$OPTARG
|
|
|
|
|
;;
|
|
|
|
|
?)
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
2024-07-27 00:14:39 +00:00
|
|
|
|
if [[ -z $db ]]
|
2024-07-25 02:17:36 +00:00
|
|
|
|
then
|
2024-07-27 00:14:39 +00:00
|
|
|
|
echo "$0:" '-d is required' >&2
|
2024-07-25 02:17:36 +00:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2024-07-27 00:43:45 +00:00
|
|
|
|
if [[ -z $team ]]
|
|
|
|
|
then
|
|
|
|
|
echo "$0:" '-t is required' >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2024-08-01 01:20:17 +00:00
|
|
|
|
# grab most recently started live pk, if multiple
|
|
|
|
|
gamePk=$(mlblive schedule -t $team | jq '.dates[].games | map(select(.status.abstractGameState == "Live"))[-1].gamePk')
|
|
|
|
|
if [[ "$gamePk" = 'null' ]]
|
2024-07-26 03:48:57 +00:00
|
|
|
|
then
|
2024-07-31 02:36:11 +00:00
|
|
|
|
echo "$0:" 'no live games found' >&2
|
2024-07-26 03:48:57 +00:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2024-08-03 03:00:42 +00: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'
|
|
|
|
|
save="\"sqlite3 $db '.mode ascii' '.separator ' '.import /dev/stdin mlbdata'\""
|
|
|
|
|
|
2024-08-01 02:42:56 +00: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-03 03:00:42 +00:00
|
|
|
|
mlblive subscribe -g $gamePk | jq -Sc --unbuffered "$jqFilter" | awk "{$fmt | $save; close($save)}"
|