#!/usr/bin/env bash # # Generates sparklines for the next hour of precipitation in your current # location. You can specify the number of minutes using the `-m` option. This # relies on `whereami` (a Mac OS X utility), `jq`, and `spark`. Unfortunately, # the proper version of Spark does not have support for the min/max flags that # this utility needs, so you'll need to install a fork. Install everything using # homebrew: # # brew tap davidcelis/spark && brew install whereami jq davidcelis/spark/spark # # Additionally, you'll need to register a forecast.io API Key by visiting # https://developer.forecast.io/ - once you've done that: # # export FORECAST_API_KEY=my_api_key # forecast # ▅▇█▆▄▃▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ # # forecast -m 15 # ▅▇█▆▄▃▃▂▂▂▁▁▁▁▁ # # The sparklines generated will be saved to /tmp/forecast.spark if you wish to # easily access it from other utilities without repeating calls. Additionally, # this utility will use that file's mtime to make sure that you do not go over # the number of API requests that forecast.io gives you for free. As such, this # utility will only attempt to call the forecast.io API if two minutes have # passed since the last call. sparkfile="/tmp/forecast.spark" jsonfile="/tmp/forecast.json" minutes=60 usage() { echo "Usage: forecast [-m <1...60>]" 1>&2; exit 1; } while getopts ":m:" flag; do case "${flag}" in m) minutes=${OPTARG} ;; *) usage ;; esac done shift $((OPTIND-1)) if [ ! -f "$jsonfile" ]; then touch $jsonfile touch $sparkfile fi last=$(stat -f %m $jsonfile) now=$(date +%s) if (( ("$now" - "$last") > 120 )); then curl -Ss https://api.forecast.io/forecast/$FORECAST_API_KEY/$(whereami) > $jsonfile temperature=$(cat $jsonfile | jq ".currently.temperature" | xargs printf "%.0f") sparklines=$(cat $jsonfile | jq ".minutely.data[:$minutes] | .[].precipIntensity * 10000" | spark -m 0 -M 1500) echo $temperature"°F $sparklines" | tee $sparkfile else cat $sparkfile fi