Skip to content

Instantly share code, notes, and snippets.

@nicholascw
Last active April 30, 2024 02:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicholascw/0fbfa123c5ec59b981e4233b029239d4 to your computer and use it in GitHub Desktop.
Save nicholascw/0fbfa123c5ec59b981e4233b029239d4 to your computer and use it in GitHub Desktop.
i3bar METAR information fetched from FAA AviationWeather.gov API
# For base16 colors, visit https://github.com/tinted-theming/base16-shell/tree/main
metar_bar () {
echo '{"version":1, "click_events":true}'
echo '['
echo "[{\"name\":\"refresh\",\"full_text\":\"Refreshing...\"}]"
while true
do
METAR_JSON=$(curl -s -X 'GET' "https://aviationweather.gov/api/data/metar?ids=$1&format=json&taf=false" -H "accept: text/plain")
METAR_GEOJSON=$(curl -s -X 'GET' "https://aviationweather.gov/api/data/metar?ids=$1&format=geojson&taf=false" -H "accept: text/plain")
METAR_FLTCAT=$(echo $METAR_GEOJSON | jq -r '.features[1].properties.fltcat')
METAR_TYPE=$(echo $METAR_JSON | jq -r '.[0].metarType')
METAR_TEXT=$(echo $METAR_JSON | jq -r '.[0].rawOb')
METAR_OBSTIME=$(echo $METAR_JSON | jq -r '.[0].obsTime')
COLOR="#$BASE16_COLOR_03_HEX"
if [[ $METAR_FLTCAT == "VFR" ]]
then
COLOR="#$BASE16_COLOR_0B_HEX"
fi
if [[ $METAR_FLTCAT == "MVFR" ]]
then
COLOR="#$BASE16_COLOR_0D_HEX"
fi
if [[ $METAR_FLTCAT == "IFR" ]]
then
COLOR="#$BASE16_COLOR_08_HEX"
fi
if [[ $METAR_FLTCAT == "LIFR" ]]
then
COLOR="#$BASE16_COLOR_0E_HEX"
fi
if [[ $METAR_TYPE == "METAR" ]]
then
TYPE_COLOR="#$BASE16_COLOR_04_HEX"
else
TYPE_COLOR="#$BASE16_COLOR_09_HEX"
fi
print_metar_statusline () {
AGE=$[($(date +%s)-$METAR_OBSTIME)/60]
AGE_COLOR="#$BASE16_COLOR_0B_HEX"
if [[ $METAR_TYPE == "METAR" ]]
then
if [[ $AGE -ge 40 ]]
then
AGE_COLOR="#$BASE16_COLOR_0A_HEX"
fi
else
if [[ $AGE -ge 20 ]]
then
AGE_COLOR="#$BASE16_COLOR_0A_HEX"
fi
fi
if [[ $AGE -ge 60 ]]
then
AGE_COLOR="#$BASE16_COLOR_08_HEX"
fi
LINE=",[{\"name\":\"FLTCAT\",\"separator\":true,\"color\":\"#$BASE16_COLOR_00_HEX\",\"background\":\"$COLOR\",\"separator_block_width\":1,\"full_text\":\" $METAR_FLTCAT \"}"
LINE="$LINE,{\"name\":\"AGE\",\"separator\":false,\"separator_block_width\":0,\"color\":\"#$BASE16_COLOR_00_HEX\",\"background\":\"$AGE_COLOR\",\"full_text\":\" $AGE min old \"}"
LINE="$LINE,{\"name\":\"SPECI\",\"separator\":false,\"separator_block_width\":0,\"color\":\"$TYPE_COLOR\",\"full_text\":\" $METAR_TYPE \"}"
LINE="$LINE,{\"name\":\"METAR\",\"separator\":false,\"separator_block_width\":0,\"color\":\"#$BASE16_COLOR_04_HEX\",\"full_text\":\"$METAR_TEXT \"}]"
echo $LINE
}
print_metar_statusline
while [[ ! -n $clickevent ]] && [[ $AGE -lt 65 ]]
do
read -t30 clickevent
BTN_CLICKED=$(echo $clickevent | grep -Eo '"button":[0-9]')
if [ "$BTN_CLICKED" = '"button":3' ]
then
unset clickevent
echo "[ $METAR_FLTCAT | $AGE min old ] $METAR_TYPE $METAR_TEXT" | xclip -selection c
else
print_metar_statusline
fi
done
unset clickevent
echo ",[{\"name\":\"refresh\",\"full_text\":\"Refreshing...\"}]"
sleep 1
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment