diff --git a/README.md b/README.md index 9991b7f..1ab2380 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ A bash script to get the weather and forecast from OpenWeatherMap and output to the terminal, Openbox, or HTML +![Output example][output] + +[output]: https://i.postimg.cc/T1WsDkJs/2019-05-26-1558885180-667x428-scrot.png "Example output" ## Contents 1. [About](#1-about) @@ -71,7 +74,7 @@ chill, it is not displayed. Copy (and edit, as appropriate) the `weather_sh.rc` file to `$HOME\.config`. The first line is the OpenWeatherMap API key -The second line is your default location +The second line is your default location. (See note below) The third line is your default degree character (either `c` or `f`) The fourth line is True or False depending on whether or not you want icons displayed for the weather. @@ -82,7 +85,7 @@ icons displayed for the weather. options: * `-k` Specifies OpenWeatherMap API key from the command-line. - * `-l city_name` Sets the city for manual weather lookup. + * `-l city_name` Sets the city for manual weather lookup. (see note below) * `-t` Output to the terminal/stdout (default if no output is specified) * `-h` Output HTML formatted text * `-o` Output OpenBox output @@ -94,8 +97,13 @@ options: _Note: If the OpenWeatherMap API key is specified from the command-line, it will override the API key set in the file._ +_Note: It is **STRONGLY** recommended to use the City ID from OpenWeatherMap +instead of a city name. Instructions on finding your city's City ID +[here](https://www.dmopress.com/openweathermap-howto/) ._ + ## 5. Todo * Add in sunrise/sunset * HTML colored output - * Current location instead of hardcoded + * Current location instead of hardcoded + - this is problematic due to the way the API looks up city names. diff --git a/example_output.png b/example_output.png new file mode 100644 index 0000000..fc0d9f1 Binary files /dev/null and b/example_output.png differ diff --git a/forecast.sh b/forecast.sh index 2c8dd3e..8c46bf5 100755 --- a/forecast.sh +++ b/forecast.sh @@ -29,7 +29,17 @@ if [ -f "$HOME/.config/weather_sh.rc" ];then defaultLocation=${line[1]} degreeCharacter=${line[2]} UseIcons=${line[3]} - colors=${line[4]} + temp=${line[4]} + if [ "$temp" = "True" ];then + if [ -f "$HOME/.bashcolors" ];then + source "$HOME/.bashcolors" + colors="True" + else + colors="" + fi + else + colors="" + fi fi ######################################################################## @@ -70,6 +80,13 @@ if [ -z $apiKey ];then exit fi +#Is it City ID or a string? +case $defaultLocation in + ''|*[!0-9]*) CityID="False" ;; + *) CityID="True" ;; +esac + + ######################################################################## # Do we need a new datafile? If so, get it. ######################################################################## @@ -77,7 +94,12 @@ fi dataPath="/tmp/fore-$defaultLocation.json" if [ ! -e $dataPath ];then touch $dataPath - data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?q=$defaultLocation&units=metric&appid=$apiKey") + if [ "$CityID" = "True" ];then + data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?id=$defaultLocation&units=metric&appid=$apiKey") + else + data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?q=$defaultLocation&units=metric&appid=$apiKey") + fi + echo $data > $dataPath else data=$(cat $dataPath) @@ -88,7 +110,11 @@ lastUpdateTime=$(($(date +%s) -600)) while true; do lastfileupdate=$(date -r $dataPath +%s) if [ $(($(date +%s)-$lastfileupdate)) -ge 600 ];then - data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?q=$defaultLocation&units=metric&appid=$apiKey") + if [ "$CityID" = "True" ];then + data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?id=$defaultLocation&units=metric&appid=$apiKey") + else + data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?q=$defaultLocation&units=metric&appid=$apiKey") + fi echo $data > $dataPath fi if [ $(($(date +%s)-$lastUpdateTime)) -ge 600 ]; then diff --git a/getlocation.sh b/getlocation.sh new file mode 100644 index 0000000..8f68fb0 --- /dev/null +++ b/getlocation.sh @@ -0,0 +1 @@ +curl http://api.db-ip.com/v2/free/184.59.91.149/city diff --git a/weather.sh b/weather.sh index 9bc9f34..27b4e7d 100755 --- a/weather.sh +++ b/weather.sh @@ -13,8 +13,8 @@ lastUpdateTime=0 FeelsLike=0 dynamicUpdates=0 UseIcons="True" -colors=0 - +colors="False" +CityID="True" if [ -f "$HOME/.config/weather_sh.rc" ];then readarray -t line < "$HOME/.config/weather_sh.rc" @@ -22,7 +22,17 @@ if [ -f "$HOME/.config/weather_sh.rc" ];then defaultLocation=${line[1]} degreeCharacter=${line[2]} UseIcons=${line[3]} - colors=${line[4]} + temp=${line[4]} + if [ "$temp" = "True" ];then + if [ -f "$HOME/.bashcolors" ];then + source "$HOME/.bashcolors" + colors="True" + else + colors="" + fi + else + colors="" + fi fi while [ $# -gt 0 ]; do @@ -59,11 +69,23 @@ if [ -z $apiKey ];then exit fi +#Is it City ID or a string? +case $defaultLocation in + ''|*[!0-9]*) CityID="False" ;; + *) CityID="True" ;; +esac + dataPath="/tmp/wth-$defaultLocation.json" + if [ ! -e $dataPath ]; then touch $dataPath - data=$(curl "http://api.openweathermap.org/data/2.5/weather?q=$defaultLocation&units=metric&appid=$apiKey") + #The API call is different if city ID is used instead of string lookup + if [ "$CityID" = "True" ];then + data=$(curl "http://api.openweathermap.org/data/2.5/weather?id=$defaultLocation&units=metric&appid=$apiKey") + else + data=$(curl "http://api.openweathermap.org/data/2.5/weather?q=$defaultLocation&units=metric&appid=$apiKey") + fi echo $data > $dataPath else data=$(cat $dataPath) @@ -73,7 +95,11 @@ lastUpdateTime=$(($(date +%s) -600)) while true; do lastfileupdate=$(date -r $dataPath +%s) if [ $(($(date +%s)-$lastfileupdate)) -ge 600 ];then - data=$(curl -s "http://api.openweathermap.org/data/2.5/weather?q=$defaultLocation&units=metric&appid=$apiKey") + if [ "$CityID" = "True" ];then + data=$(curl "http://api.openweathermap.org/data/2.5/weather?id=$defaultLocation&units=metric&appid=$apiKey") + else + data=$(curl "http://api.openweathermap.org/data/2.5/weather?q=$defaultLocation&units=metric&appid=$apiKey") + fi echo $data > $dataPath fi if [ $(($(date +%s)-$lastUpdateTime)) -ge 600 ]; then diff --git a/weather_sh.rc b/weather_sh.rc index 4c7bc9c..9d62f17 100644 --- a/weather_sh.rc +++ b/weather_sh.rc @@ -1,5 +1,5 @@ NotARealAPIKey -Dayton +4509884 f False True