openweathermap_simple 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/sh
  2. get_icon() {
  3. case $1 in
  4. # Icons for weather-icons
  5. 01d) icon="";;
  6. 01n) icon="";;
  7. 02d) icon="";;
  8. 02n) icon="";;
  9. 03*) icon="";;
  10. 04*) icon="";;
  11. 09d) icon="";;
  12. 09n) icon="";;
  13. 10d) icon="";;
  14. 10n) icon="";;
  15. 11d) icon="";;
  16. 11n) icon="";;
  17. 13d) icon="";;
  18. 13n) icon="";;
  19. 50d) icon="";;
  20. 50n) icon="";;
  21. *) icon="";
  22. # Icons for Font Awesome 5 Pro
  23. #01d) icon="";;
  24. #01n) icon="";;
  25. #02d) icon="";;
  26. #02n) icon="";;
  27. #03d) icon="";;
  28. #03n) icon="";;
  29. #04*) icon="";;
  30. #09*) icon="";;
  31. #10d) icon="";;
  32. #10n) icon="";;
  33. #11*) icon="";;
  34. #13*) icon="";;
  35. #50*) icon="";;
  36. #*) icon="";
  37. esac
  38. echo $icon
  39. }
  40. KEY="6cc6bc11b81b8fbc306d48dc50bbb88a"
  41. CITY="Gomel"
  42. UNITS="metric"
  43. SYMBOL="°"
  44. API="https://api.openweathermap.org/data/2.5"
  45. if [ -n "$CITY" ]; then
  46. if [ "$CITY" -eq "$CITY" ] 2>/dev/null; then
  47. CITY_PARAM="id=$CITY"
  48. else
  49. CITY_PARAM="q=$CITY"
  50. fi
  51. weather=$(curl -sf "$API/weather?appid=$KEY&$CITY_PARAM&units=$UNITS")
  52. else
  53. location=$(curl -sf https://location.services.mozilla.com/v1/geolocate?key=geoclue)
  54. if [ -n "$location" ]; then
  55. location_lat="$(echo "$location" | jq '.location.lat')"
  56. location_lon="$(echo "$location" | jq '.location.lng')"
  57. weather=$(curl -sf "$API/weather?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS")
  58. fi
  59. fi
  60. if [ -n "$weather" ]; then
  61. weather_temp=$(echo "$weather" | jq ".main.temp" | cut -d "." -f 1)
  62. weather_icon=$(echo "$weather" | jq -r ".weather[0].icon")
  63. echo "$(get_icon "$weather_icon")" " $weather_temp$SYMBOL"
  64. fi