diff options
| -rwxr-xr-x | wetter | 41 |
1 files changed, 39 insertions, 2 deletions
@@ -18,7 +18,8 @@ # How many days of weather to show. # Default: 4 # -# WETTER_LOCATION +# WETTER_DEFAULT_LOCATION +# WETTER_LOCATIONS_FILE # # SEE ALSO # API documentation: https://api.weather.mg/ @@ -34,6 +35,26 @@ main() { location_spec=$1 fi + if are_valid_coords "$location_spec"; then + location=$location_spec + debug "location: $location" + else + locations_file=${WETTER_LOCATIONS_FILE-} + if test -z "$locations_file"; then + error "location file not specified to resolve \`$location_spec'" + fi + if ! test -e "$locations_file"; then + error "locations file not found: $locations_file" + fi + location=$( + awk -v name="$location_spec" '$1==name{print$2}' "$locations_file" + ) + if test -z "location"; then + error "unknown location: $location_spec (see $locations_file)" + fi + debug "location: $location ($location_spec)" + fi + token_url=${WETTER_TOKEN_URL-https://api.weatherpro.com/v1/token/weather} api_root_url=${WETTER_API_ROOT_URL-https://point-forecast-weatherpro.meteogroup.com} @@ -62,7 +83,7 @@ main() { join(",") ') - locatedAt=$(echo "$location_spec" | awk -F, '{print$2","$1}') + locatedAt=$(echo "$location" | awk -F, '{print$2","$1}') days=${WETTER_DAYS-4} @@ -304,7 +325,23 @@ EOF EOF } +# usage: are_valid_coords STR +# Predicate to check whether a given string resembles valid coordinates. +are_valid_coords() { + printf '%s\n' "$1" | + awk -F, ' + NF != 2 { exit 1 } + $1 !~ /^[-+]?[0-9]+([.][0-9]*)?$/ && + $1 !~ /^[-+]?[.][0-9]+$/ { exit 1 } + $2 !~ /^[-+]?[0-9]+([.][0-9]*)?$/ && + $2 !~ /^[-+]?[.][0-9]+$/ { exit 1 } + + ($1 < -90 || $1 > 90) { exit 1 } + ($2 < -180 || $2 > 180) { exit 1 } + { exit 0 } + ' +} debug() { echo "$*" >&2 |
