weather.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. # Prints the current weather in Celsius, Fahrenheits or lord Kelvins. The forecast is cached and updated with a period of $update_period.
  3. # You location. Find a string that works for you by Googling on "weather in <location-string>"
  4. location='Melbourne, Australia'
  5. # Can be any of {c,f,k}.
  6. unit="c"
  7. tmp_file="/tmp/tmux-powerline_weather.txt"
  8. get_condition_symbol() {
  9. local conditions=$(echo "$1" | tr '[:upper:]' '[:lower:]')
  10. case "$conditions" in
  11. sunny | "partly sunny" | "mostly sunny")
  12. hour=$(date +%H)
  13. if [ "$hour" -ge "22" -o "$hour" -le "5" ]; then
  14. #echo "☽"
  15. echo "☾"
  16. else
  17. #echo "☀"
  18. echo "☼"
  19. fi
  20. ;;
  21. "rain and snow" | "chance of rain" | "light rain" | rain | "heavy rain" | "freezing drizzle" | flurries | showers | "scattered showers" | drizzle | "rain showers")
  22. #echo "☂"
  23. echo "☔"
  24. ;;
  25. snow | "light snow" | "scattered snow showers" | icy | ice/snow | "chance of snow" | "snow showers" | sleet)
  26. #echo "☃"
  27. echo "❅"
  28. ;;
  29. "partly cloudy" | "mostly cloudy" | cloudy | overcast)
  30. echo "☁"
  31. ;;
  32. "chance of storm" | thunderstorm | "chance of tstorm" | storm | "scattered thunderstorms")
  33. #echo "⚡"
  34. echo "☈"
  35. ;;
  36. dust | fog | smoke | haze | mist)
  37. echo "♨"
  38. ;;
  39. windy)
  40. echo "⚑"
  41. #echo "⚐"
  42. ;;
  43. clear)
  44. #echo "☐"
  45. echo "✈" # So clear you can see the aeroplanes! TODO what symbol does best represent a clear sky?
  46. ;;
  47. *)
  48. echo "?"
  49. ;;
  50. esac
  51. }
  52. read_tmp_file() {
  53. if [ ! -f "$tmp_file" ]; then
  54. return
  55. fi
  56. IFS_bak="$IFS"
  57. IFS=$'\n'
  58. lines=($(cat ${tmp_file}))
  59. IFS="$IFS_bak"
  60. degrees="${lines[0]}"
  61. conditions="${lines[1]}"
  62. }
  63. degrees=""
  64. if [ -f "$tmp_file" ]; then
  65. if [ "$PLATFORM" == "mac" ]; then
  66. last_update=$(stat -f "%m" ${tmp_file})
  67. else
  68. last_update=$(stat -c "%Y" ${tmp_file})
  69. fi
  70. time_now=$(date +%s)
  71. update_period=600
  72. up_to_date=$(echo "(${time_now}-${last_update}) < ${update_period}" | bc)
  73. if [ "$up_to_date" -eq 1 ]; then
  74. read_tmp_file
  75. fi
  76. fi
  77. if [ -z "$degrees" ]; then
  78. if [ "$unit" == "k" ]; then
  79. search_unit="c"
  80. else
  81. search_unit="$unit"
  82. fi
  83. # Convert spaces before using this in the URL.
  84. if [ "$PLATFORM" == "mac" ]; then
  85. search_location=$(echo "$location" | sed -e 's/[ ]/%20/g')
  86. else
  87. search_location=$(echo "$location" | sed -e 's/\s/%20/g')
  88. fi
  89. weather_data=$(curl --max-time 4 -s "http://www.google.com/ig/api?weather=${search_location}")
  90. if [ "$?" -eq "0" ]; then
  91. error=$(echo "$weather_data" | grep "problem_cause\|DOCTYPE");
  92. if [ -n "$error" ]; then
  93. echo "error"
  94. exit 1
  95. fi
  96. degrees=$(echo "$weather_data" | sed "s|.*<temp_${search_unit} data=\"\([^\"]*\)\"/>.*|\1|")
  97. if [ "$PLATFORM" == "mac" ]; then
  98. conditions=$(echo $weather_data | xpath //current_conditions/condition/@data 2> /dev/null | grep -oe '".*"' | sed "s/\"//g")
  99. else
  100. conditions=$(echo "$weather_data" | grep -PZo "<current_conditions>(\\n|.)*</current_conditions>" | grep -PZo "(?<=<condition\sdata=\")([^\"]*)")
  101. fi
  102. echo "$degrees" > $tmp_file
  103. echo "$conditions" >> $tmp_file
  104. elif [ -f "$tmp_file" ]; then
  105. read_tmp_file
  106. fi
  107. fi
  108. if [ -n "$degrees" ]; then
  109. if [ "$unit" == "k" ]; then
  110. degrees=$(echo "${degrees} + 273.15" | bc)
  111. fi
  112. unit_upper=$(echo "$unit" | tr '[cfk]' '[CFK]')
  113. condition_symbol=$(get_condition_symbol "$conditions")
  114. echo "${condition_symbol} ${degrees}°${unit_upper}"
  115. fi