Twitter / weatherlisbon
Twitter is a fairly recent social short messaging system. It allows to send short messages to a network of friends via instant messaging (IM) services, sms-es, and the twitter web-site itself.
Twitter begs for folks to do inventive mashups like twittervision.
Thus I wrote a small script in bash that checks for the weather in Lisbon from a BBC weather forecast feed and sends it to twitter. I called this script a twitter-bot-agent. The agent's name is weatherlisbon. If you're registered in Twitter then you can add "him" to your friends and every morning at 6am Lisbon time you'll be twitted with the lisbon weather for the day.
Comment: In fact I was scooped, and other folks already had this idea. But as far as I can see this is the only bot tweeting the daily weather for lisbon. Go check out more bots at http://www.retweet.com/.
You can check out the script explained in this wiki or here below:
#! /bin/sh
#BBC Lisbon weather id
id=0048
#BBC weather RSS feed address
feed="http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/${id}.xml"
#City
city=lisbon
#temporary file
tmpfile="weather${city}.txt"
#Weather twitter bot
twitbot=username:******
#Read the RSS feed and filter it
#Careful with this long one-liner. Blogger may have corrupted it. Thanks to LordRich for pointing that out.
curl $feed 2>&1 | grep 'title' | tail -n 1 | perl -wlne'm/title>(.*)<\/title/i && print $1' | sed -e "s/°//g" > $tmpfile
#Read the forecast into a weather variable
read weather < $tmpfile
#Twit the weather variable away
curl --basic --user $twitbot --data status="$weather" http://twitter.com/statuses/update.xml
UPDATE: the script is better viewed at snippets.dzone.

4 coments:
Blogger has messed up that code quite badly, it took me a while to work out what it should be. Also, a couple of things worth pointing out:
1, the structure of the feed address is slightly different for uk locations. And the id for a uk location is a valid world-wide location, but not the correct location.
2, I realise it's harder to do, but if you read in the description then you could show more info (the uv index and pollution index).
3, I added in a sed script to remove the country name. This makes it easier to read and would allow for the extra info in #2.
Thanks for your suggestions LordRich.
Indeed, you will need to tinker a bit with the script that is exposed in blogger.
I'm referring to the long sequence of pipes - the 'º' character in the 'sed' command in fact must be replaced by its correct html character codification. And there's no line-feed. But you probably already figured that out.
As you might have noticed, I'm very keen to long one-liners :)
Not a good habit I reckon.
Anyway, feel free to improve the script and use it ad lib. It's why I posted it in the first place: so others could pick it up and make it better.
I won't have much time to improve it myself. I did it more like an exercise.
Ok, I added the current version of the script at snippets.dzone.com/griflet
Check it at
http://snippets.dzone.com/posts/show/4159
Love this idea! Another useful tool to add to the BBC Breaking and BBC Tech twitter feeds :)
Is there any chance you'd be able to crate a bot for the weather in Newcastle (id 0010) please?
Guy
Post a Comment