
Twitter’s feature to allow updates from terminal allows for a lot of creative ways to use twitter. I used some already written scripts and commands and put it all together to receive weather forecasts for my area at 9am every day on my phone. Example of SMS message sent to my phone “Forecast for: DES MOINES, IA Today Mainly clear and frigid – Low 10“ Here is how I did it:
First I used a written shell script and made output modifications so length would be under Twitter’s length limitations. The modified script is listed below:
Note: I take no credit for the original script. It was available online, and I modified it based on my needs. Dave Taylor is the original author, and offers a book full of shell scripts I recommend checking out! Page Here
#!/bin/sh
# weather – report weather forecast, including lat/long, for zip
# Dave Taylor – found at http://www.intuitive.com/wicked/showscript.cgi?063-weather.sh
# Edited for twitter use by MadHatter (maddhat.com)
llurl=”http://www.census.gov/cgi-bin/gazetteer?city=&state=&zip=”
wxurl=”http://wwwa.accuweather.com“
wxurl=”$wxurl/adcbin/public/local_index_print.asp?zipcode=”
if [ "$1" = "-a" ] ; then
size=999; shift
else
size=5
fi
if [ $# -eq 0 ] ; then
echo “Usage: $0 [-a] zipcode” >&2
exit 1
fi
if [ $size -eq 5 ] ; then
echo “”
# get some information on the zipcode from the Census Bureau
lynx -source “${llurl}$1″ | \
sed -n ‘/^<li><strong>/,/^Location:/p’ | \
sed ’s/<[^>]*>//g;s/^ //g’
fi
# the weather forecast itself at accuweather.com
lynx -source “${wxurl}$1″ | \
sed -n ‘/Start – Forecast Cell/,/End – Forecast Cell/p’ | \
sed ’s/<[^>]*>//g;s/^ [ ]*//g’ | \
sed ’s/10-Day AccuWeather.com//’ | \
sed ’s/Today/Today:/’ | \
uniq | \
head -$size
exit 0
Save this as weather.sh in your home folder. Then open a text editor and create “tw.sh” saved to your home directory with the following contents:
Note: You need to replace the USER:PASSWORD with the username and password for the secondary twitter account.
#!/bin/sh
#tw.sh
curl -u USER:PASSWORD -d status=”$*” http://twitter.com/statuses/update.xml
For this script to run correctly curl must be installed. For ubuntu the command is “sudo apt-get install curl”
Now we have the necessary scripts created. To receive updates you will need to create a second twitter account of which to send updates. Make sure you follow this new account with your primary twitter account and turn on device updates if you are looking for update to your mobile device.
All that is left to get things up and running is to add the crontab entry. I added to my sudo crontab by the following “sudo crontab -e” and adding the following entry:
Note: This will run at 9am every day. You can edit yours however you’d like. For testing it may be a good idea to start with “* * * * *” to run every minute to ensure updates are working as planned. Replace “user” with your user name, as well as ZIPCODE with the zip code of the area you would like weather forecasts for.
0 9 * * * /home/user/tw.sh $(/home/user/weather.sh ZIPCODE)
Save the crontab entry. Check that it was added by typing “sudo crontab -l”. That’s all there is to it.
Some useful links for help and customization:
Twitter API, Cron.