2020-03-15 00:57:01 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
#author: Dane Williams
|
|
|
|
#script for gathering current location and weather at location
|
|
|
|
#script is then called in the dracula.tmux program
|
|
|
|
|
|
|
|
city=$(curl -s https://ipinfo.io/city 2> /dev/null)
|
2020-03-15 03:21:31 +01:00
|
|
|
region=$(curl -s https://ipinfo.io/region 2> /dev/null)
|
|
|
|
zip=$(curl -s https://ipinfo.io/postal 2> /dev/null | tail -1)
|
|
|
|
country=$(curl -s https://ipinfo.io/country 2> /dev/null)
|
2020-03-15 00:57:01 +01:00
|
|
|
|
2020-03-15 03:21:31 +01:00
|
|
|
region_code_url=http://www.ip2country.net/ip2country/region_code.html
|
|
|
|
weather_url=https://forecast.weather.gov/zipcity.php
|
|
|
|
|
|
|
|
#substitute region code for regions in north america
|
|
|
|
get_region_code()
|
|
|
|
{
|
|
|
|
curl -s $region_code_url | grep $region &> /dev/null && region=$(curl -s $region_code_url | grep $region | cut -d ',' -f 2)
|
|
|
|
echo $region
|
|
|
|
}
|
|
|
|
|
|
|
|
weather_information()
|
|
|
|
{
|
|
|
|
curl -sL $weather_url?inputstring=$zip | grep myforecast-current | grep -Eo '>.*<' | sed -E 's/>(.*)</\1/'
|
|
|
|
}
|
|
|
|
get_temp()
|
|
|
|
{
|
|
|
|
weather_information | grep 'deg;F' | cut -d '&' -f 1
|
|
|
|
}
|
|
|
|
forecast_unicode()
|
|
|
|
{
|
|
|
|
forecast=$(weather_information | head -n 1)
|
|
|
|
|
|
|
|
if [[ $forecast =~ 'Snow' ]]; then
|
2020-03-15 20:41:55 +01:00
|
|
|
echo '❄ '
|
2020-03-15 03:21:31 +01:00
|
|
|
elif [[ (($forecast =~ 'Rain') || ($forecast =~ 'Shower')) ]]; then
|
2020-03-15 20:41:55 +01:00
|
|
|
echo '☂ '
|
2020-03-15 03:21:31 +01:00
|
|
|
elif [[ (($forecast =~ 'Overcast') || ($forecast =~ 'Cloud')) ]]; then
|
2020-03-15 20:41:55 +01:00
|
|
|
echo '☁ '
|
2020-03-15 20:21:58 +01:00
|
|
|
elif [[ $forecast = 'NA' ]]; then
|
|
|
|
echo ''
|
2020-03-15 03:21:31 +01:00
|
|
|
else
|
2020-03-15 20:41:55 +01:00
|
|
|
echo '☀ '
|
2020-03-15 03:21:31 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
#get weather display if in US
|
|
|
|
display_weather()
|
|
|
|
{
|
|
|
|
if [ $country = 'US' ]; then
|
2020-03-15 20:59:20 +01:00
|
|
|
echo "$(forecast_unicode)$(get_temp)°F"
|
2020-03-15 03:21:31 +01:00
|
|
|
else
|
|
|
|
echo ''
|
|
|
|
fi
|
|
|
|
}
|
2020-03-15 00:57:01 +01:00
|
|
|
|
|
|
|
main()
|
|
|
|
{
|
2020-03-15 05:13:46 +01:00
|
|
|
# process should be cancelled when session is killed
|
2020-03-15 03:21:31 +01:00
|
|
|
if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then
|
|
|
|
echo "$(display_weather) $city, $(get_region_code)"
|
|
|
|
else
|
|
|
|
echo "Location Unavailable"
|
|
|
|
fi
|
2020-03-15 00:57:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#run main driver program
|
|
|
|
main
|