Skip to content

Instantly share code, notes, and snippets.

@KennyStier
Created March 1, 2017 13:24
Show Gist options
  • Save KennyStier/0529afe40a0744e3617d8272d4cda721 to your computer and use it in GitHub Desktop.
Save KennyStier/0529afe40a0744e3617d8272d4cda721 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Twitter PWS Status Bot
# Author: Kenny Stier <[email protected]>
#
# Gathers Personal Weather Station data from Weather Underground
# and sends tweets throughout the day
# Twitter Credentials
username="user"
password="password"
# Wunderground PWS ID
pws="KINMILLH2"
# Wunderground API Key
key="api-key here"
# What type of tweet is to be sent?
tweetType=$1
# If tweeting current conditions...
if [ "$tweetType" = "current" ]; then
# Download PWS Data
wget --quiet -O /tmp/wu-api.json https://api.wunderground.com/api/$key/conditions/q/pws:$pws.json
# Calculate Age of PWS Data in Seconds
localEpoch=$(jshon -e current_observation -e local_epoch -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
currentEpoch=$(date +%s)
observationEpoch=$(jshon -e current_observation -e observation_epoch -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
observationAge=$(expr $currentEpoch - $observationEpoch)
# Determine if WU has recent PWS data
if [ "$observationAge" -le "3600" ]; then
temperature=$(jshon -e current_observation -e temp_f -F /tmp/wu-api.json)
weather=$(jshon -e current_observation -e weather -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
winds=$(jshon -e current_observation -e wind_string -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//' | sed -e 's/^./\L&\E/')
humidity=$(jshon -e current_observation -e relative_humidity -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
feelsLike=$(jshon -e current_observation -e feelslike_f -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
feelsLikeDiff=$(expr $(printf "%.0f" "$temperature") - $feelsLike)
rainTotal=$(jshon -e current_observation -e precip_today_in -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
visibilityMI=$(jshon -e current_observation -e visibility_mi -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
if [ "$feelsLikeDiff" -lt "0" ]; then
feelsLikeDiff=$(expr $feelsLikeDiff * -1)
fi
if [ "$rainTotal" != "0.00" ]; then
rain=$(echo We have gotten $rainTotal inches of rain so far today.)
fi
if [ $(printf "%.0f" "$visibilityMI") -le "3" ]; then
visibility=$(echo The visibility is currently $visibilityMI miles.)
fi
if [ "$feelsLikeDiff" -gt "5" ]; then
feelsLikeString=$(echo It feels like it\'s "$feelsLike" degrees outside.)
fi
tweet=$(echo It\'s currently $(printf "%.1f" "$temperature") degrees and "$weather" with the winds "$winds". The humidity is at "$humidity".)
tweet2=$(echo $feelsLikeString $rain $visibility)
fi
fi
# If tweeting today's forecast...
if [ "$tweetType" = "todayForecast" ]; then
wget --quiet -O /tmp/wu-api.json https://api.wunderground.com/api/$key/forecast/q/pws:$pws.json
daytime=$(jshon -e forecast -e txt_forecast -e forecastday -e 0 -e fcttext -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
evening=$(jshon -e forecast -e txt_forecast -e forecastday -e 1 -e fcttext -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
tweet=$(echo "Today: $daytime")
tweet2=$(echo "Tonight: $evening")
fi
# If tweeting tomorrow's forecast...
if [ "$tweetType" = "tomorrowForecast" ]; then
wget --quiet -O /tmp/wu-api.json https://api.wunderground.com/api/$key/forecast/q/pws:$pws.json
daytime=$(jshon -e forecast -e txt_forecast -e forecastday -e 2 -e fcttext -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
evening=$(jshon -e forecast -e txt_forecast -e forecastday -e 3 -e fcttext -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
tweet=$(echo "Tomorrow: $daytime")
tweet2=$(echo "Tomorrow Evening: $evening")
fi
# If tweeting daily summary...
if [ "$tweetType" = "dailySummary" ]; then
wget --quiet -O /tmp/wu-api.json https://api.wunderground.com/api/$key/conditions/q/pws:$pws.json
rainTotal=$(jshon -e current_observation -e precip_today_in -F /tmp/wu-api.json | sed -e 's/^"//' -e 's/"$//')
if [ "$rainTotal" != "0.00" ]; then
tweet=$(echo Today, we got a total of $rainTotal inches of rain.)
fi
fi
# upload to imgur - curl -s https://gist.githubusercontent.com/vivien/9768953/raw/imgur | sh -s
tweet () {
tweetlen=${#tweet}
if [ $tweetlen -eq 0 ] || [ $tweetlen -gt 140 ]; then
if [ $tweetlen -gt 140 ]; then
let EXTRA=$tweetlen-140
let EXTRAtail=$EXTRA+7
echo "(Tweet is $EXTRA characters too long and has been cut into two."
tweet1_1=$(echo "(1/1) $tweet" | cut -c1-140)
tweet1_2=$(echo "(1/2) $tweet" | tail -c "$EXTRAtail")
twidge update "$tweet1_2"
twidge update "$tweet1_1"
else
echo "Tweet was empty, nothing sent"
fi
else
twidge update "$tweet"
fi
}
tweet2 () {
tweetlen2=${#tweet2}
if [ $tweetlen2 -eq 0 ] || [ $tweetlen2 -gt 140 ]; then
if [ $tweetlen2 -gt 140 ]; then
let EXTRA2=$tweetlen2-140
let EXTRA2tail=$EXTRA2+7
echo "Tweet is $EXTRA2 characters too long and has been cut into two."
tweet2_1=$(echo "(1/1) $tweet2" | cut -c1-140)
tweet2_2=$(echo "$tweet2" | tail -c "$EXTRA2tail" | awk '$0="(1/2) "$0')
twidge update "$tweet2_2"
twidge update "$tweet2_1"
else
echo "Tweet was empty, nothing sent"
fi
else
twidge update "$tweet2"
fi
}
if [ "$tweet2" != "" ]; then
tweet2
echo $tweet2
fi
if [ "$tweet" != "" ]; then
tweet
echo $tweet
fi
rm "/tmp/wu-api.json"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment