-
-
Save Der-Schubi/df6d5413267264ef64019d39949342d2 to your computer and use it in GitHub Desktop.
Simple alternative to wttr.in using OpenWeatherMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| help() { | |
| echo "Usage: wttr.sh -l [Location]" | |
| } | |
| if [ "$#" == 0 ]; then | |
| help | |
| echo -e "\nwttr.sh requires at least a Location as Option. Try 'wttr.sh -l London'" | |
| exit 1 | |
| fi | |
| OPTS=$(getopt -o hdl:jpw --long help,debug,location:,json,prettyjson,waybar -n 'wttr.sh' -- "$@") | |
| if [[ $? -ne 0 ]]; then | |
| echo "ERR: Error when analyzing options!" >&2 | |
| exit 1 | |
| fi | |
| OPT_DEBUG=0 | |
| OPT_JSON=0 | |
| OPT_WAYBAR=0 | |
| ARG_LOCATION="" | |
| eval set -- "$OPTS" | |
| while [ : ]; do | |
| case "$1" in | |
| -h | --help) | |
| help | |
| shift | |
| ;; | |
| -d | --debug) | |
| OPT_DEBUG=1 | |
| shift | |
| ;; | |
| -l | --location) | |
| ARG_LOCATION="$2" | |
| shift 2 | |
| ;; | |
| -j | --json) | |
| OPT_JSON=1 | |
| shift | |
| ;; | |
| -p | --prettyjson) | |
| OPT_JSON=2 | |
| shift | |
| ;; | |
| -w | --waybar) | |
| OPT_WAYBAR=1 | |
| shift | |
| ;; | |
| --) | |
| shift | |
| break 2 | |
| ;; | |
| *) | |
| echo "ERR: Internal Error!" | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| [ $OPT_DEBUG -eq 1 ] && echo "DEBUG: $OPT_DEBUG | JSON: $OPT_JSON | LOCATION: $ARG_LOCATION" | |
| API_KEY="14785236987456321" | |
| LANG="en" | |
| # Function to get Emoji from icon id (e.g. 04d) | |
| iconid_to_emoji() { | |
| i='' | |
| case $1 in | |
| 01d) | |
| icon=$'' #clear day | |
| ;; | |
| 01n) | |
| icon=$'' #clear night | |
| ;; | |
| 02d) | |
| icon=$'' #few clouds day | |
| ;; | |
| 02n) | |
| icon=$'' #few clouds night | |
| ;; | |
| 03* | 04*) | |
| icon=$'' #scattered/broken clouds day/night | |
| ;; | |
| 09*) | |
| icon=$'' #shower rain day/night | |
| ;; | |
| 10d) | |
| icon=$'' #rain day | |
| ;; | |
| 10n) | |
| icon=$'' #rain night | |
| ;; | |
| "10d 11d") | |
| icon=$'' #thunderstorm day | |
| ;; | |
| "10n 11n") | |
| icon=$'' #thunderstorm night | |
| ;; | |
| 11*) | |
| icon=$'' #thunderstorm generic | |
| ;; | |
| 13*) | |
| icon=$'' #snow day/night | |
| ;; | |
| 50*) | |
| icon=$'' #fog day/night | |
| ;; | |
| *) | |
| icon=$'' | |
| ;; | |
| esac | |
| echo -e $icon | |
| } | |
| # Function to map wind direction in degrees to arrows | |
| wind_to_arrow() { | |
| local deg=$1 | |
| if [ $deg -ge 0 -a $deg -lt 23 ] || [ $deg -ge 338 ]; then | |
| echo "→" | |
| elif [ $deg -ge 23 -a $deg -lt 68 ]; then | |
| echo "↗️" | |
| elif [ $deg -ge 68 -a $deg -lt 113 ]; then | |
| echo "↑" | |
| elif [ $deg -ge 113 -a $deg -lt 158 ]; then | |
| echo "↖️" | |
| elif [ $deg -ge 158 -a $deg -lt 203 ]; then | |
| echo "←" | |
| elif [ $deg -ge 203 -a $deg -lt 248 ]; then | |
| echo "↙️" | |
| elif [ $deg -ge 248 -a $deg -lt 293 ]; then | |
| echo "↓" | |
| else | |
| echo "↘️" | |
| fi | |
| } | |
| # Get latitude and longitude of the location | |
| location=$(echo "$ARG_LOCATION" | sed 's/ /%20/g') # URL-encode spaces | |
| geo_data=$(curl -s "http://api.openweathermap.org/geo/1.0/direct?q=$location&appid=$API_KEY") | |
| [ $OPT_DEBUG -eq 1 ] && echo "Geo Data: $geo_data" | |
| lat=$(echo $geo_data | jq -r '.[0].lat') | |
| lon=$(echo $geo_data | jq -r '.[0].lon') | |
| # Get current weather data | |
| weather_data=$(curl -s "http://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$lon&appid=$API_KEY&units=metric&lang=$LANG") | |
| [ $OPT_DEBUG -eq 1 ] && echo "Weather Data: $weather_data" | |
| # Print to JSON-File | |
| if [ $OPT_JSON -gt 0 ]; then | |
| if [ $OPT_JSON -eq 1 ]; then | |
| echo "$weather_data" | |
| else | |
| echo "$weather_data" | jq . | |
| fi | |
| exit 0 | |
| fi | |
| location_name=$(echo $weather_data | jq -r '.name') | |
| country=$(echo $weather_data | jq -r '.sys.country') | |
| weather_main=$(echo $weather_data | jq -r '.weather[0].main') | |
| weather_description=$(echo $weather_data | jq -r '.weather[0].description') | |
| iconid=$(echo $weather_data | jq -r '.weather[0].icon') | |
| temp=$(echo $weather_data | jq -r '.main.temp' | awk '{printf "%.0f", $1}') | |
| wind_speed=$(echo $weather_data | jq -r '.wind.speed' | awk '{ printf "%.0f\n", $1 * 3.6}') | |
| wind_deg=$(echo $weather_data | jq -r '.wind.deg') | |
| # Get Emoji from Icon ID | |
| weather_emoji=$(iconid_to_emoji $iconid) | |
| # Convert wind direction to arrow | |
| wind_arrow=$(wind_to_arrow "$wind_deg") | |
| # Prepare Output | |
| if [ $OPT_WAYBAR -eq 1 ]; then | |
| if (( $(echo "$temp > 0" | bc -l) )); then | |
| CLASS="temp_above_zero" | |
| else | |
| CLASS="temp_below_zero" | |
| fi | |
| TEXT=$"${weather_emoji} ${temp}°C" | |
| TOOLTIP=$"$location_name, $country: $weather_main $weather_emoji ($weather_description) ${temp}°C ${wind_arrow}${wind_speed}km/h" | |
| jq -nc \ | |
| --arg "class" "$CLASS" \ | |
| --arg "text" "$TEXT" \ | |
| --arg "tooltip" "$TOOLTIP" \ | |
| '$ARGS.named' | |
| else | |
| # Print the one-liner | |
| echo -e $"$location_name, $country: $weather_emoji ${temp}°C ${wind_arrow}${wind_speed}km/h" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment