moved from dotfiles

This commit is contained in:
KeeganForelight 2023-01-13 13:39:40 -05:00
commit 27504e712b
13761 changed files with 27759 additions and 0 deletions

238
bb Executable file
View File

@ -0,0 +1,238 @@
#!/usr/bin/env bash
#set -e
usage() {
cat <<EOF
usage: $0 [-c][-f][-s][-h][-r [option]]
Queries Bluebikes API to get recent bike status
Options:
-c, --colorize enables tmux color options
-f, --force force refreshes bb data
-s, --search pulls up a searchable fzf menu to update
displayed station
-r, --rename [NAME] rename the current station
stored in data/alias.json
if called without an arguement resets to default
-h, --help display this message
EOF
}
# setting up vars
TIMEOUT=60 # interval we request bike information at (s)
WORKINGDIR="$HOME/.dotfiles/bin/bluebikes"
# api
BB_API="https://gbfs.bluebikes.com/gbfs/en/station_status.json"
# allows customizing search window or dropping in dmenu
SEARCH="fzf-tmux --layout=reverse -p 50%,50% --border"
# create directory on fresh installs
if [[ ! -d "$WORKINGDIR/data/status" ]] ; then
mkdir -p "$WORKINGDIR/data/status"
fi
# creating station alias list on fresh installs
if [[ ! -e "$WORKINGDIR/data/alias.json" ]] ; then
starter_json=$(printf '{ "stations": [] }' | gojq '.')
echo "$starter_json" > "$WORKINGDIR/data/alias.json"
fi
# getting station
if [[ -e "$WORKINGDIR/data/.station" ]] ; then
# if file exists
source "$WORKINGDIR/data/.station"
fi
get_station_info() {
# sets $BIKES and $DOCKS based on $STATIONID
# checks that $STATIONID is set
if [[ -z "$STATIONID" ]] ; then
printf 'Please set a station with bb -s' >&2
exit 1
fi
# uses $UPDATE set based on update check
if [ "$UPDATE" = true ] ; then
# getting new station info
STATION_INFO=$(curl --silent -fL $BB_API | gojq --arg id $STATIONID '.data.stations[] | select( .station_id == $id)' 2>/dev/null)
if [[ -z "$STATION_INFO" ]] ; then
printf 'Error retrieving station info! Check your connection' >&2
exit 1
fi
# set vars
BIKES=$(echo "$STATION_INFO" | gojq '.num_bikes_available')
DOCKS=$(echo "$STATION_INFO" | gojq '.num_docks_available')
EXPIRATION=$(($(date +%s) + $TIMEOUT))
# creating info file
printf 'BIKES=%d\nDOCKS=%d\n' $BIKES $DOCKS > "$WORKINGDIR/data/status/$EXPIRATION"
else
# grab existing data
source $WORKINGDIR/data/status/*
fi
}
check_update() {
# sets $UPDATE if needed
file_expr=$(ls "$WORKINGDIR/data/status" | grep .json | awk -F . '{print $1}')
if [[ $(date +%s) -gt $file_expr ]] ; then
# out of date
UPDATE=true
# cleaning old info
rm ${WORKINGDIR}/data/status/*.json 2> /dev/null
fi
}
set_station_alias() {
# sets $ALIAS for $STATION_ID
if [[ -z "$STATIONID" ]] ; then
# no station id
printf 'No Station set to change name for!\nPlease run bb -s to set a station' >&2
exit 1
fi
#aliases=$(cat "$WORKINGDIR/data/alias.json") # alias json
ALIAS_FILE="$WORKINGDIR/data/alias.json"
# fmtting
FMT_ALIAS=$(printf '"%s"' $(echo $ALIAS | tr -d '"'))
FMT_STATIONID=$(printf '"%d"' $STATIONID)
OUT=$(cat "$ALIAS_FILE" | gojq ".$FMT_STATIONID |= $FMT_ALIAS")
echo "$OUT" > "$ALIAS_FILE"
# printing change
if [[ -z "$ALIAS" ]] ; then
printf 'Removing alias\n'
else
printf 'Updated alias to %s\n' "$ALIAS"
fi
}
update_station() {
# provides a gui to update the station to watch
STATIONS=$(curl --silent -fL https://gbfs.bluebikes.com/gbfs/en/station_information.json | gojq '.data.stations[]')
if [[ -z "$STATIONS" ]] ; then
printf 'Error retrieving station info! Check your connection' >&2
exit 1
fi
# prompt user to search
NEW_STATION=$(echo "$STATIONS" | gojq '.name' | tr -d '"' | eval "$SEARCH")
if [[ -z "$NEW_STATION" ]] ; then
exit 0
fi
# number correlating to selected name
STATIONID=$(echo "$STATIONS"| gojq --arg name "$NEW_STATION" '. | select( .name == $name) | .station_id' | tr -d '"') # trim quotes
# setting data/.station file
output=$(printf 'STATIONID=%s\nSTATION_NAME="%s"\n' "$STATIONID" "$NEW_STATION")
echo "$output" > "$WORKINGDIR/data/.station"
}
colorize() {
# colorizes $BIKES and $DOCKS for tmux
case $1 in
[0-2])
clr='#[fg=color1]' # red
;;
[3-6])
clr='#[fg=color184]' # yellow
;;
*)
clr='#[fg=color34]' # green
;;
esac
printf '%s%d' "$clr" $1
}
print_status() {
# prints
get_station_info # sets $DOCKS $BIKES and $STATIONID
if [[ -e "$WORKINGDIR/data/alias.json" ]] ; then
FMT_STATIONID=$(printf '"%d"' $STATIONID)
ALIAS=$(cat "$WORKINGDIR/data/alias.json" | gojq ".$FMT_STATIONID" | tr -d '"')
if [[ ! -z "$ALIAS" ]] ; then
STATION_NAME="$ALIAS"
fi
fi
DEFAULT=""
if [[ "$COLORIZE" = true ]] ; then
# adding tmux colors
DEFAULT="#[default]"
BIKES=$(colorize $BIKES)
DOCKS=$(colorize $DOCKS)
fi
printf '%s  %s  %s%s\n' $BIKES $DOCKS "$DEFAULT" "$STATION_NAME"
}
# always check/clean status
check_update
# change long form
for arg in "$@"; do
shift
case "$arg" in
'--help') set -- "$@" "-h" ;;
'--search') set -- "$@" "-s" ;;
'--force') set -- "$@" "-f" ;;
'--rename') set -- "$@" "-r" ;;
'--colorize') set -- "$@" "-c" ;;
*) set -- "$@" "$arg" ;;
esac
done
# parsing args
while getopts "hsfcr" opt ; do
case "$opt" in
'h' )
usage
exit 0
;;
's' )
update_station
;;
'f' )
UPDATE=true
;;
'c' )
COLORIZE=true
;;
'r' )
nextopt=${!OPTIND}
if [[ -n "$nextopt" && "$nextopt" != -* ]] ; then
ALIAS="$nextopt"
fi
set_station_alias
;;
'?' )
usage
exit 1
;;
esac
done
# finish by printing
print_status

2
data/.station Normal file
View File

@ -0,0 +1,2 @@
STATIONID=413
STATION_NAME="Kennedy-Longfellow School 158 Spring St"

3
data/alias.json Normal file
View File

@ -0,0 +1,3 @@
{
"413": "Longfellow"
}

2
data/status/1673462169 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462172 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462173 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462174 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462175 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462184 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462185 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462194 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462195 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462204 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462205 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462214 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462215 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462224 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462225 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462234 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462235 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462244 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462245 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462254 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462255 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462264 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462265 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462271 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462272 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462274 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462275 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462278 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462279 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462280 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462281 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462283 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462284 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462294 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462295 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462302 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462303 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462304 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462305 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462314 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462315 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462324 Normal file
View File

@ -0,0 +1,2 @@
BIKES=8
DOCKS=11

2
data/status/1673462325 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462327 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462328 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462334 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462335 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462344 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462345 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462354 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462355 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462364 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462365 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462374 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462375 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462384 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462385 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462386 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462387 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462394 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462395 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462404 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462405 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462414 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462415 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462424 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462425 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462426 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462430 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462431 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462434 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462435 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462436 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462437 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462444 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462445 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462448 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462449 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462454 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462455 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462461 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462462 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462464 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462465 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462469 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462470 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462471 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462472 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462474 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462475 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462484 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462485 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462494 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462495 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462504 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462505 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

2
data/status/1673462514 Normal file
View File

@ -0,0 +1,2 @@
BIKES=9
DOCKS=10

Some files were not shown because too many files have changed in this diff Show More