owms

OpenWeatherMap based weather client
git clone git://git.vgx.fr/owms
Log | Files | Refs

owms.sh (2790B)


      1 #!/bin/sh
      2 # Basic openweathermap scrapper
      3 # Dependencies : curl, grep, sed, cut, tr, jq
      4 
      5 #set -x
      6 
      7 # Config:
      8 
      9 BASE_URL="https://openweathermap.org"
     10 
     11 # Set APPID there or it will be scrapped from main page
     12 #APPID=
     13 
     14 # old method
     15 
     16 # get_app_id () {
     17 #     WIDGET_SCRIPT_PREFIX="/themes/openweathermap/assets/vendor/owm/js/weather-widget-new"
     18 #     WIDGET_SCRIPT_URL="${BASE_URL}$(curl -s "$BASE_URL" | grep -o "$WIDGET_SCRIPT_PREFIX"'\.[^.]*\.js')"
     19 # 
     20 #     curl -s "$WIDGET_SCRIPT_URL" | grep -o 'appidWeatherLayer:"[^"]*' | sed 's/.*:"//''
     21 # }
     22 
     23 get_app_id () {
     24     curl -s "${BASE_URL}/find" | grep -o 'appid=[^"]*' | cut -d= -f2
     25 }
     26 
     27 usage () {
     28     echo "Usage: $0 COMMAND CITY"
     29     echo "COMMAND=now|hour|hours|week"
     30 }
     31 
     32 find_data () {
     33     curl -s "${BASE_URL}/data/2.5/find?q=$1&appid=${APPID}"
     34 }
     35 
     36 first_match_id () {
     37     find_data "$1" | jq -r '.list[0].id'
     38 }
     39 
     40 first_match_coord () {
     41     find_data "$1" | jq '.list[0].coord | .lat, .lon' | tr '\n' '\t'
     42 }
     43 
     44 find () {
     45     find_data "$1" | jq -r '.list[]|.name+", ID="+(.id|tostring)+" coords:"+([.coord.lat, .coord.lon]|tostring)'
     46 }
     47 
     48 onecall () {
     49     curl -s "${BASE_URL}/data/2.5/onecall?lat=$1&lon=$2&lang=${LANG:0:2}&units=metric&appid=${APPID}"
     50 }
     51 
     52 now () {
     53     #    COORDS="$(first_match_coord "$1")"
     54     #    LAT="$(echo $COORDS | cut -f1)"
     55     #    LON="$(echo $COORDS | cut -f2)"
     56     onecall $(first_match_coord "$1") |\
     57     jq -rj '.current | .temp, "°C (ressenti : ", .feels_like, "°C) ",'\
     58 '   .pressure, "hPa ", .humidity, "% 💧, ",'\
     59 '   .clouds, "% ☁,, 🍃 :", .wind_speed, "m/s, ", .weather[0].description'
     60     echo # add missing newline
     61 }
     62 
     63 hour () {
     64     onecall $(first_match_coord "$1") |\
     65     jq -r ".minutely[].precipitation" |\
     66     awk -v 'ORS=' '{if($1 == 0)print" ";else if($1<0.2)print".";else if($1<1)print"1";else if($1<2)print"2";else print"x"}'
     67     echo
     68     echo "|'''''''''''''^''''''''''''''|''''''''''''''^''''''''''''''|"
     69 }
     70 
     71 hours () {
     72     onecall $(first_match_coord "$1") |\
     73     jq -r '.hourly[]|(.dt | localtime | strftime("%HH: "))+(.temp|tostring)+"°C, "'\
     74 '   +(.pop|tostring)+" proba. de ☔, "+(.clouds|tostring)+"% ☁,, "+.weather[0].description'
     75 }
     76 
     77 week () {
     78     onecall $(first_match_coord "$1") |\
     79     jq -r '.daily[]|(.dt | localtime | strflocaltime("%a %e %b: "))+(.temp.min|tostring)+"  "+(.temp.max|tostring)+"°C, "'\
     80 '   +(.pop|tostring)+" proba. de ☔, "+.weather[0].description'
     81 }
     82 
     83 if [[ $# -le 0 ]] ; then
     84     usage
     85     exit 1
     86 fi
     87 
     88 APPID=${APPID:-$(get_app_id)}
     89 
     90 #echo "$APPID"
     91 
     92 COMMAND=$1
     93 shift
     94 
     95 case "$COMMAND" in
     96     find)
     97         find "$@"
     98         ;;
     99     now)
    100         now "$@"
    101         ;;
    102     hour)
    103         hour "$@"
    104         ;;
    105     hours)
    106         hours "$@"
    107         ;;
    108     week)
    109         week "$@"
    110         ;;
    111     *)
    112         echo "Unknown command" >&2
    113         usage
    114         ;;
    115 esac
    116