Twitter Weather station

Create your own weather station on Twitter.

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
    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
    
    1 */6 * * * /usr/local/bin/twitter_weather.py


Now you have weather information updated 6 times a day.
http://fred.velvnet.com/SFOWeather.png


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 %sºF" % (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()

#!/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 %sºF" % (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

 

Comments

Posted by: dafuser Mar 15, 2009 @ 08:39

When I run the script it throws the following error:

slash2:Downloads dafuser$ ./mini_weather.py Traceback (most recent call last): File "./mini_weather.py", line 5, in [HTML_REMOVED] import twitter ImportError: No module named twitter

I get the same error if I download the script from the download page or if I copy the script from the displayed source file.

I'm running the script on a Mac running Leopard 10.5.6 with

Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin

Any idea what's causing the error?

Thanks

Posted by: film izle Dec 23, 2009 @ 17:03

twitter is great for me i like it

Posted by: flowers san jose Dec 27, 2009 @ 01:44

no doubt twitter has changed our life so much that we are now considering it as an authentic news channel :)

Posted by: Rolex watches Jan 04, 2010 @ 23:33

Welcome to our website,and you will enjoy the sunshine service. Let's

abandon all the worry,and enjoy the happy shopping travel.All the

replica watches paypal and replica watches are most in fashion

boutique this year.rolex replica and rolex replica paypal and the

replica rolex paypal show your chaming temperament. In addtin, the

fake watches paypal and the other fake rolex paypal make you mold

your unique glamour this winter.The swiss rolex replica have the high

quality but low price. replica watch paypal and replica rolex have

all the style and cheapest price replica rolex watches. Enjoying the

fun of net purchase! replica rolex We will supply you the perfect

service and after sale support system. Our honest will bring you

absolute satisfaction.

Posted by: ugg boots Jan 07, 2010 @ 04:07

twitter is great for me i like it

Posted by: replica watches Jan 07, 2010 @ 21:57

The tag heuer rubber strap rolex doesn’t stray from rolex watches the strap pattern. Even replica watches though breitling watches the strap patek philippe patterns of audemars piguet watches from this brand chanel watches are familiar to us, it approaches a very sport and innovative montblanc watches appearance. This brand fake watches are always trying new replica watches watches with rolex watches new designs. This New replica rolex Concord C1 Chronograph tag heuer Watch can best embody this feature.This time Concord breitling watches offers us

Posted by: evening dresses Jan 09, 2010 @ 04:18

It must've taken you a bit of time, so thanks for taking the time to do so, I appreciate it, This post is just great, I'll read the rest of your blog...

Posted by: personalized basketball gifts Jan 13, 2010 @ 12:56

replica watches, evening dresses.. why not personalized basketball Gifts..

Posted by: wholesale china Jan 14, 2010 @ 00:19

Here is the richest in the world and you can find your needs bring you harvest yes there is no other better place than here here you are with my natural beauty but also naturally pure green world our homes a share in the bar.God bless you! www.trademic.com

Posted by: film izle Jan 17, 2010 @ 04:37

Thanks for your good website and for sharing your experiences.

Posted by: divx film izle Jan 17, 2010 @ 04:38

Thank you for sharing your friends. Hope to see you another day.

Posted by: Wholesale Jan 17, 2010 @ 21:58

in fact,to say nothing more on this subject, but still would like to thank for sharing

Posted by: links of london Jan 19, 2010 @ 00:10

Here is the richest in the world and you can find your needs bring you harvest yes there is no other better place than here here you are with my natural beauty but also naturally

Posted by: links of london Jan 19, 2010 @ 00:10

ere is the richest in the world and you can find your needs bring you harvest yes there is no other better place than here here you are with my natural beauty but also naturally pure green world our homes a share in the bar.God bless you! www.trademic.com

Posted by: Soulwhisper Jan 22, 2010 @ 00:02

Thank you for sharing your friends. Hope to see you another day. http://www.airmax-shox.com/en

Posted by: revizyon ile organize matbaacılık brnckvvtmllttrhaberi Jan 24, 2010 @ 14:07

Thank you [HTML_REMOVED]revizyon ile organize matbaacılık brnckvvtmllttrhaberi[HTML_REMOVED] [HTML_REMOVED]revizyon ile organize matbaacılık brnckvvtmllttrhaberi[HTML_REMOVED] [HTML_REMOVED]revizyon ile organize matbaacılık brnckvvtmllttrhaberi[HTML_REMOVED]

Leave a message

(Required)
(Required and not displayed)
(Optional)
obfuscated letters Enter the text shown in the image