#!/bin/bash # server version, calls server for actual API info WORKING_DIR="$HOME/.local/share/weather" KEY_FILE="$WORKING_DIR/.env" # set $API_KEY if [[ ! -e "$KEY_FILE" ]] ; then echo "No API key found!" exit 1 fi source "$KEY_FILE" lookup_location() { # search for a location url="http://api.openweathermap.org/geo/1.0/direct?q=$SEARCH&appid=$API_KEY&limit=5" res=$(curl --silent -fL "$url") echo "$res" } lookup_zipcode() { # lookup based on zipcode url="http://api.openweathermap.org/geo/1.0/zip?zip=$ZIPCODE&appid=$API_KEY&limit=5" res=$(curl --silent -fL "$url") echo "$res" } get_weather() { # calls server for weather based on $COORDS LAT=$(echo "$COORDS" | awk -F , '{print $1}') LON=$(echo "$COORDS" | awk -F , '{print $2}') url="https://api.openweathermap.org/data/2.5/weather?lat=$LAT&lon=$LON&appid=$API_KEY&units=$UNITS" res=$(curl --silent -fL "$url") echo "$res" } while getopts "c:s:z:u:" opt; do case "$opt" in 'c' ) COORDS="$OPTARG" ;; 's' ) SEARCH="$OPTARG" ;; 'z' ) ZIPCODE="$OPTARG" ;; 'u' ) UNITS="$OPTARG" ;; '?' ) echo "ERROR: UNRECOGNZIED ARGUMENT" exit 1 ;; esac done if [[ -n "$COORDS" ]] ; then get_weather elif [[ -n "$SEARCH" ]] ; then lookup_location elif [[ -n "$ZIPCODE" ]] ; then lookup_zipcode fi exit 0