Skip to content

Instantly share code, notes, and snippets.

@skydrome
Created May 27, 2018 16:30
Show Gist options
  • Save skydrome/d56f0a098b27466e4654f1b5d0765ebe to your computer and use it in GitHub Desktop.
Save skydrome/d56f0a098b27466e4654f1b5d0765ebe to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#HOST=192.168.1.64:8080
HOST=192.168.1.70:8080
msg() {
MESSAGE="Starting in 5 minutes"
case "$1" in
redsox)
TITLE="Boston Redsox $2"
MESSAGE+=" $(ansiweather -u imperial -a false -l 'Boston, MA' \
|sed -e 's:Weather in Boston :Temp :' -e 's: % - Pressure.*:%:' \
-e 's: °F:°F:' -e 's: mph:mph:')"
IMAGE="https://seeklogo.com/images/B/boston-red-sox-logo-21F446678F-seeklogo.com.png"
;;
news)
TITLE="WRAL $2 News"
IMAGE="https://vignette.wikia.nocookie.net/logopedia/images/1/13/WRAL_2003.jpg"
;;
alert)
TITLE="Weather Alert"
MESSAGE="$2"
IMAGE="https://pbs.twimg.com/profile_images/2382643128/nws_logo1_400x400.png"
;;
esac
send "$TITLE" "$MESSAGE" "$IMAGE"
}
send() {
curl -s -X POST -H 'Content-Type: application/json' -d \
'{
"id":"1",
"jsonrpc":"2.0",
"method":"GUI.ShowNotification",
"params":{
"title":"'"$1"'",
"message":"'"$2"'",
"image":"'"$3"'",
"displaytime":6000
}
}' http://${HOST}/jsonrpc 1>/dev/null
echo "$?:sent message $1 $(date +%a\ %H%M)"
sleep 5
}
weather_alert() {
file='/tmp/kodi.alert'
touch $file
cmp=$(<$file)
res=$(curl -s 'https://alerts.weather.gov/cap/wwaatmget.php?x=NCC183&y=0' \
|grep -m2 '<title>' |tail -n1 |sed -e "s:<title>::" -e "s:</title>::" -e "s:\t::")
if [[ "$res" != "$cmp" ]]; then
echo "$res" >$file
msg alert "$res"
fi
}
main_loop() {
while true; do
echo
date=($(date +%a\ %H%M))
day=${date[0]}
time=${date[1]}
if [[ $day = @(Mon|Tue|Wed|Thur|Fri) ]]; then
case $time in
0455) msg news "Early Morning" ;;
0555) msg news "Morning" ;;
1555) msg news "Afternoon" ;;
1655|1755) msg news "Evening" ;;
2255) msg news "Late" ;;
esac
fi
if [[ $day = @(Sat|Sun) ]]; then
case $time in
0555) msg news "Early Morning" ;;
0655) msg news "Morning" ;;
1755) msg news "Evening" ;;
esac
fi
if [[ $day = 'Mon' ]]; then
case $time in
1255) msg redsox "vs Toronto" ;;
esac
elif [[ $day = 'Tue' ]]; then
case $time in
1905) msg redsox "vs Toronto" ;;
esac
elif [[ $day = 'Wed' ]]; then
case $time in
1255) msg redsox "vs Toronto" ;;
esac
elif [[ $day = 'Thur' ]]; then
case $time in
2005) msg redsox "@ Houston" ;;
esac
elif [[ $day = 'Fri' ]]; then
case $time in
2005) msg redsox "@ Houston" ;;
esac
elif [[ $day = 'Sat' ]]; then
case $time in
2155) msg news "Late" ;;
1905) msg redsox "@ Houston" ;;
esac
elif [[ $day = 'Sun' ]]; then
case $time in
0855) msg news "Morning" ;;
2255) msg news "Late" ;;
1255) msg redsox "vs Atlanta" ;;
esac
fi
weather_alert
# loop every 5 minutes
sleep 300
done
}
echo ' :syncronizing with time'
declare -a minute=($(date +%M |fold -w1))
until [[ ${minute[1]} = @(0|5) ]]; do
minute=($(date +%M |fold -w1))
sleep 5
done
echo ' :starting main loop'
main_loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment