cleaning up directory references

This commit is contained in:
KeeganForelight 2023-01-13 14:00:34 -05:00
parent a5d729c066
commit 938e869045

39
bb
View File

@ -22,7 +22,10 @@ EOF
# setting up vars
TIMEOUT=60 # interval we request bike information at (s)
WORKINGDIR="$HOME/.dotfiles/bin/bluebikes"
WORKING_DIR="$HOME/.local/bluebikes"
DATA_DIR="$WORKING_DIR/.data"
ALIAS_FILE="$WORKING_DIR/aliases.json"
STATION_FILE="$DATA_DIR/.station"
# api
BB_API="https://gbfs.bluebikes.com/gbfs/en/station_status.json"
@ -31,20 +34,20 @@ BB_API="https://gbfs.bluebikes.com/gbfs/en/station_status.json"
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"
if [[ ! -d "$DATA_DIR" ]] ; then
mkdir -p "$DATA_DIR"
fi
# creating station alias list on fresh installs
if [[ ! -e "$WORKINGDIR/data/alias.json" ]] ; then
if [[ ! -e "$ALIAS_FILE" ]] ; then
starter_json=$(printf '{ "stations": [] }' | gojq '.')
echo "$starter_json" > "$WORKINGDIR/data/alias.json"
echo "$starter_json" > "$ALIAS_FILE"
fi
# getting station
if [[ -e "$WORKINGDIR/data/.station" ]] ; then
if [[ -e "$STATION_FILE" ]] ; then
# if file exists
source "$WORKINGDIR/data/.station"
source "$STATION_FILE"
fi
get_station_info() {
@ -75,24 +78,23 @@ get_station_info() {
EXPIRATION=$(($(date +%s) + $TIMEOUT))
# creating info file
printf 'BIKES=%d\nDOCKS=%d\n' $BIKES $DOCKS > "$WORKINGDIR/data/status/$EXPIRATION"
printf 'BIKES=%d\nDOCKS=%d\n' $BIKES $DOCKS > "$DATA_DIR/$EXPIRATION.bb"
else
# grab existing data
source $WORKINGDIR/data/status/*
source $DATA_DIR/*.bb
fi
}
check_update() {
# sets $UPDATE if needed
file_expr=$(ls "$WORKINGDIR/data/status" | grep .json | awk -F . '{print $1}')
file_expr=$(ls "$DATA_DIR" | grep .bb | 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
# cleaning old station info
rm $DATA_DIR/*.bb 2> /dev/null
fi
}
@ -105,10 +107,7 @@ set_station_alias() {
exit 1
fi
#aliases=$(cat "$WORKINGDIR/data/alias.json") # alias json
ALIAS_FILE="$WORKINGDIR/data/alias.json"
# fmtting
# gojq hates qoutes
FMT_ALIAS=$(printf '"%s"' $(echo $ALIAS | tr -d '"'))
FMT_STATIONID=$(printf '"%d"' $STATIONID)
@ -143,7 +142,7 @@ update_station() {
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"
echo "$output" > "$STATION_FILE"
}
colorize() {
@ -167,9 +166,9 @@ print_status() {
# prints
get_station_info # sets $DOCKS $BIKES and $STATIONID
if [[ -e "$WORKINGDIR/data/alias.json" ]] ; then
if [[ -e "$WORKINGDIR/aliases.json" ]] ; then
FMT_STATIONID=$(printf '"%d"' $STATIONID)
ALIAS=$(cat "$WORKINGDIR/data/alias.json" | gojq ".$FMT_STATIONID" | tr -d '"')
ALIAS=$(cat "$WORKINGDIR/aliases.json" | gojq ".$FMT_STATIONID" | tr -d '"')
if [[ ! -z "$ALIAS" ]] ; then
STATION_NAME="$ALIAS"
fi