A very simple weather application for the terminal


There are many ways to check the weather on my computer but all are slightly inconvenient. I’d rather not open a browser when I don’t need to, and the very popular wttr.in includes tons of information and ASCII art that I don’t need.

The following script can be called from the command line to pull data from pirate-weather.apiable.io, a free service fully compatible with the once popular and now defunct Dark Sky Forecast.io API.

This script simply lists (see screenshot) the current temperature and conditions, as well as an hourly forecast with the same information over the next 24 hours.

This was thrown together unpolished for personal use, and so for example usage of Fahrenheit is assumed. This can be easily modified as needed.

#!/usr/bin/env bash
#~rts/oarion7

# Takes 1 optional argument N number of hours to forecast (max 48)

myapikey='' # Get one from: https://pirate-weather.apiable.io/
coordinates='40.713914010140385,-73.98905208987489' # A good place to get slapped hard in the face
custom_number='24' # No. of hours if not specified in arg. Comment to default to API (48).

if [[ -n "$1" ]] && [[ ! -z "${1##*[!0-9]*}" ]] && [[ "$1" -le 48 ]] ; then
    custom_number="$1"
fi

data="$(curl -LSs "https://api.pirateweather.net/forecast/${myapikey}/${coordinates}")"

[[ -z "$data" ]] && { echo "Error fetching API data."; exit 1; }

date="$(date -d "@$( jq '.currently.time' <<< "$data" )" +'%A, %d %B %Y %H:%M' )"

printf "\n"

printf '%s\n%s \n\nCurrently %s F and %s' \
"Weather Report by Pirate Weather API" "$date" \
"$(jq '.currently.apparentTemperature' <<< "$data")" \
"$(jq '.currently.summary' <<< "$data" | sed 's/\"//g' )" 

printf "\n\n"

forecast="$(jq '.hourly[]' <<< "$data" | egrep -v '^\"' )"

if [[ -n "$custom_number" ]] ; then
    readarray -t times < <( echo "$forecast" | jq ".[:$custom_number] | .[] | .time" )
else
    readarray -t times < <( echo "$forecast" | jq '.[] | .time' )
fi

readarray -t summaries < <( echo "$forecast" | jq '.[] | .summary' | sed 's/\"//g' )

readarray -t temps < <( echo "$forecast"  | jq '.[] | .apparentTemperature' )

tabs 3

for i in $(seq 0 $(( ${#times[@]} - 1 )) ) ; do 

    printf '%s\t\t%s\t\t%s\n' "$(date -d "@${times[$i]}" +'%H:%M')" "${temps[$i]} F" "${summaries[$i]}"

done 

printf "\n"

At a glance, I know before heading out for the day if I’m properly dressed and if I need to bring an umbrella, and that’s exactly all I need.