DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Twitter Bot Weatherlisbon

06.18.2007
| 7039 views |
  • submit to reddit
        This little bash script shows how to use curl, grep, tail, sed and perl one-liners in order to compose a bleeding-edge twitter bot.
This one returns daily weather forecasts for Lisbon city based on the BBC weather forecast rss feed.

#! /bin/sh

#Goto here
here=/home/guillaume/Personal
cd $here

#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
file="weather${city}"

#Weather twitter bot
twitbot=weatherlisbon:*******

#Timestamp the log file
echo .>> $file.log
date >> $file.log

#Read the RSS feed and filter it
curl $feed | grep 'title' | tail -n 1 | perl -wlne'm/title>(.*)<\/title/i && print $1' | sed -e "s/°//g" > $file.txt

#Read the forecast into a weather variable
read weather < $file.txt

#Twit the weather variable away
curl --basic --user $twitbot --data status="$weather" http://twitter.com/statuses/update.xml >> $file.log