Twitter Weather Station

Create your own weather station on Twitter.

Installation

With a few simple steps and the following code, you can have weather information for where you live automatically updated to your twitter account. The following steps and python program are for a Unix/Linux/Mac environment. I am sure they can easily be adapted to Windows.

Save the following program on you computer under the name of your choice. You can also choose to download this program from here. Don't forget to set the execute mode for this file with:

chmod a+x
/usr/local/bin/twitter_weather.py

Update the variable USERNAME and PASSWORD with the username and password of your twitter account.

Update the variable URL with the XML file for your city. A list of supported cities can be found at http://www.weather.gov/xml/current_obs/. Be careful to use the XML and not the RSS link

Create a crontab entry in you system to update the weather every few hours. A reasonable value will be every 6 hours.

1 */6 * * * /usr/local/bin/twitter_weather.py

Now you have weather information updated 4 times a day.

The source code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from xml.etree import ElementTree as ET
import urllib2
import twitter

# information that need to be modified
# Source for the city codes at http://www.weather.gov/xml/current_obs/
URL = 'http://www.weather.gov/xml/current_obs/KSFO.xml'
USERNAME='weathertest'
PASSWORD='*******'
# end of modifiable informtion

class Weather:
    def __init__(self, url):
        fd = urllib2.urlopen(url)
        tree = ET.parse(fd)
        self.root = tree.getroot()
        fd.close()

    def get(self, key):
        return self.root.find(key).text

    def temp_f(self):
        return self.get('temp_f')

    def weather(self):
        return self.get('weather')

    def wind_dir(self):
        return self.get('wind_dir')

    def humidity(self):
        return self.get('relative_humidity')

    def wind_degrees(self):
        return self.get('wind_degrees')

    def wind_mph(self):
        return self.get('wind_mph')

    def pressure(self):
        return self.get('pressure_mb')

    def twit(self):
        weather = "%s %sF" % (self.weather(), self.temp_f())
        pressure = "Pressure: %s mb" % (self.pressure())
        humidity = "Humidity: %s" % (self.humidity())
        wind = "Wind: %s %s\" %s MPH" % (self.wind_dir(),
                                         self.wind_degrees(),
                                         self.wind_mph())
        return ' - '.join((weather, pressure, humidity, wind))

def main():
    try:
        weather = Weather(URL)
        tw = twitter.Api(username=USERNAME, password=PASSWORD)
        tw.PostUpdate(weather.twit().decode('utf-8'))
    except Exception, why:
        print str(why)

if __name__ == "__main__":
    main()

If you are using this program you can post a comment here or send me an email and I will follow your weather station from the ones I am already running on twitter.

Some examples

Twitter Weather Bot

Here is the Sockeris machine based on a 100 Mhz 486 class processor. That I use to run all the twitter weather stations I manage. It's run OpenBSD embedded in a 64Mb flash card and Python 2.7 and easily manage a dozen of twitter accounts.


Comments !