239 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			239 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
#!/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
 |