|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | # setting the locale, some users have issues with different locales, this forces the correct one |
3 | | -export LC_ALL=C.utf8 |
| 3 | +export LC_ALL=en_US.UTF-8 |
4 | 4 |
|
5 | | -fahrenheit=$1 |
6 | | -location=$2 |
7 | | -fixedlocation=$3 |
| 5 | +API_URL="https://wttr.in" |
| 6 | +DELIM=":" |
8 | 7 |
|
9 | 8 | # emulate timeout command from bash - timeout is not available by default on OSX |
10 | 9 | if [ "$(uname)" == "Darwin" ]; then |
11 | | - timeout() { |
12 | | - perl -e 'alarm shift; exec @ARGV' "$duration" "$@" |
| 10 | + function timeout() { |
| 11 | + local _duration |
| 12 | + _duration="${1:-1}" |
| 13 | + command -p perl -e 'alarm shift; exec @ARGV' "$_duration" "$@" |
13 | 14 | } |
14 | 15 | fi |
15 | 16 |
|
16 | | -display_location() |
17 | | -{ |
18 | | - if $location && [[ ! -z "$fixedlocation" ]]; then |
19 | | - echo " $fixedlocation" |
20 | | - elif $location; then |
21 | | - city=$(curl -s https://ipinfo.io/city 2> /dev/null) |
22 | | - region=$(curl -s https://ipinfo.io/region 2> /dev/null) |
23 | | - echo " $city, $region" |
24 | | - else |
25 | | - echo '' |
26 | | - fi |
27 | | -} |
| 17 | +# Fetch weather information from remote API |
| 18 | +# Globals: |
| 19 | +# API_URL |
| 20 | +# DELIM |
| 21 | +# Arguments: |
| 22 | +# show fahrenheit, either "true" or "false" |
| 23 | +# optional fixed location to query weather data about |
| 24 | +function fetch_weather_information() { |
| 25 | + local _show_fahrenheit _location _unit |
| 26 | + _show_fahrenheit="$1" |
| 27 | + _location="$2" |
28 | 28 |
|
29 | | -fetch_weather_information() |
30 | | -{ |
31 | | - display_weather=$1 |
32 | | - # it gets the weather condition textual name (%C), and the temperature (%t) |
33 | | - api_response=$(curl -sL wttr.in/${fixedlocation// /%20}\?format="%C+%t$display_weather") |
34 | | - |
35 | | - if [[ $api_response = "Unknown location;"* ]]; then |
36 | | - echo "Unknown location error" |
| 29 | + if "$_show_fahrenheit"; then |
| 30 | + _unit="u" |
37 | 31 | else |
38 | | - echo $api_response |
| 32 | + _unit="m" |
39 | 33 | fi |
| 34 | + |
| 35 | + # If the user provies a "fixed location", `@dracula-fixed-location`, that the |
| 36 | + # API does not recognize, the API may suggest a users actual geoip GPS |
| 37 | + # location in the response body. This can lead to user PI leak. |
| 38 | + # Drop response body when status code >= 400 and return nonzero by passing the |
| 39 | + # `--fail` flag. Execute curl last to allow the consumer to leverage the |
| 40 | + # return code. Pass `--show-error` and redirect stderr for the consumer. |
| 41 | + command -p curl -L --silent --fail --show-error \ |
| 42 | + "${API_URL}/${_location// /%20}?format=%C${DELIM}%t${DELIM}%l&${_unit}" 2>&1 |
40 | 43 | } |
41 | 44 |
|
42 | | -#get weather display |
43 | | -display_weather() |
44 | | -{ |
45 | | - if $fahrenheit; then |
46 | | - display_weather='&u' # for USA system |
47 | | - else |
48 | | - display_weather='&m' # for metric system |
49 | | - fi |
50 | | - weather_information=$(fetch_weather_information $display_weather) |
| 45 | +# Format raw weather information from API |
| 46 | +# Globals: |
| 47 | +# DELIM |
| 48 | +# Arguments: |
| 49 | +# The raw weather data as returned by "fetch_weather_information()" |
| 50 | +# show location, either "true" or "false" |
| 51 | +function format_weather_info() { |
| 52 | + local _raw _show_location |
| 53 | + _raw="$1" # e.g. "Rain:+63°F:Houston, Texas, United States" |
| 54 | + _show_location="$2" |
51 | 55 |
|
52 | | - weather_condition=$(echo "$weather_information" | awk -F' -?[0-9]' '{print $1}' | xargs) # Extract condition before temperature, e.g. Sunny, Snow, etc |
53 | | - temperature=$(echo "$weather_information" | grep -oE '[-+]?[0-9]+°[CF]') # Extract temperature, e.g. +31°C, -3°F, etc |
54 | | - unicode=$(forecast_unicode $weather_condition) |
| 56 | + local _weather _temp _location |
| 57 | + _weather="${_raw%%"${DELIM}"*}" # slice temp and location to get weather |
| 58 | + _weather=$(printf '%s' "$_weather" | tr '[:upper:]' '[:lower:]') # lowercase weather, OSX’s bash3.2 does not support ${v,,} |
| 59 | + _temp="${_raw#*"${DELIM}"}" # slice weather to get temp and location |
| 60 | + _temp="${_temp%%"${DELIM}"*}" # slice location to get temp |
| 61 | + _temp="${_temp/+/}" # slice "+" from "+74°F" |
| 62 | + _location="${_raw##*"${DELIM}"}" # slice weather and temp to get location |
| 63 | + [ "${_location//[^,]/}" == ",," ] && _location="${_location%,*}" # slice country if it exists |
55 | 64 |
|
56 | | - # Mac Only variant should be transparent on Linux |
57 | | - if [[ "${temperature/+/}" == *"===="* ]]; then |
58 | | - temperature="error" |
59 | | - fi |
| 65 | + case "$_weather" in |
| 66 | + 'snow') |
| 67 | + _weather='❄' |
| 68 | + ;; |
| 69 | + 'rain' | 'shower') |
| 70 | + _weather='☂' |
| 71 | + ;; |
| 72 | + 'overcast' | 'cloud') |
| 73 | + _weather='☁' |
| 74 | + ;; |
| 75 | + 'na') |
| 76 | + _weather='' |
| 77 | + ;; |
| 78 | + *) |
| 79 | + _weather='☀' |
| 80 | + ;; |
| 81 | + esac |
60 | 82 |
|
61 | | - if [[ "${temperature/+/}" == "error" ]]; then |
62 | | - # Propagate Error |
63 | | - echo "error" |
| 83 | + if "$_show_location"; then |
| 84 | + printf '%s %s %s' "$_weather" "$_temp" "$_location" |
64 | 85 | else |
65 | | - echo "$unicode ${temperature/+/}" # remove the plus sign to the temperature |
| 86 | + printf '%s %s' "$_weather" "$_temp" |
66 | 87 | fi |
67 | 88 | } |
68 | 89 |
|
69 | | -forecast_unicode() |
70 | | -{ |
71 | | - weather_condition=$(echo $weather_condition | awk '{print tolower($0)}') |
72 | | - |
73 | | - if [[ $weather_condition =~ 'snow' ]]; then |
74 | | - echo '❄ ' |
75 | | - elif [[ (($weather_condition =~ 'rain') || ($weather_condition =~ 'shower')) ]]; then |
76 | | - echo '☂ ' |
77 | | - elif [[ (($weather_condition =~ 'overcast') || ($weather_condition =~ 'cloud')) ]]; then |
78 | | - echo '☁ ' |
79 | | - elif [[ $weather_condition = 'NA' ]]; then |
80 | | - echo '' |
81 | | - else |
82 | | - echo '☀ ' |
83 | | - fi |
84 | | -} |
| 90 | +# Display weather, temperature, and location |
| 91 | +# Globals |
| 92 | +# none |
| 93 | +# Arguments |
| 94 | +# show fahrenheit, either "true" (default) or "false" |
| 95 | +# show location, either "true" (default) or "false" |
| 96 | +# optional fixed location to query data about, e.g. "Houston, Texas" |
| 97 | +function main() { |
| 98 | + local _show_fahrenheit _show_location _location |
| 99 | + _show_fahrenheit="${1:-true}" |
| 100 | + _show_location="${2:-true}" |
| 101 | + _location="$3" |
85 | 102 |
|
86 | | -main() |
87 | | -{ |
88 | 103 | # process should be cancelled when session is killed |
89 | | - if timeout 1 bash -c "</dev/tcp/ipinfo.io/443" && timeout 1 bash -c "</dev/tcp/wttr.in/443" && [[ "$(display_weather)" != "error" ]]; then |
90 | | - echo "$(display_weather)$(display_location)" |
91 | | - else |
92 | | - echo "Weather Unavailable" |
| 104 | + if ! timeout 1 bash -c "</dev/tcp/wttr.in/443"; then |
| 105 | + printf 'Weather Unavailable\n' |
| 106 | + return |
| 107 | + fi |
| 108 | + |
| 109 | + # BashFAQ/002: assignment of substitution does not effect status code. |
| 110 | + local _resp |
| 111 | + if ! _resp=$(fetch_weather_information "$_show_fahrenheit" "$_location"); then |
| 112 | + |
| 113 | + # e.g. "curl: (22) The requested URL returned error: 404" |
| 114 | + case "${_resp##* }" in |
| 115 | + 404) |
| 116 | + printf 'Unknown Location\n' |
| 117 | + ;; |
| 118 | + *) |
| 119 | + printf 'Weather Unavailable\n' |
| 120 | + ;; |
| 121 | + esac |
| 122 | + |
| 123 | + return |
93 | 124 | fi |
| 125 | + |
| 126 | + format_weather_info "$_resp" "$_show_location" |
94 | 127 | } |
95 | 128 |
|
96 | | -#run main driver program |
97 | | -main |
| 129 | +main "$@" |
0 commit comments