No Gravatar

I was searching around this morning for a method of getting sunrise/sunset times into a set of python and bash scripts I am writing to control mains (240V) powered items. Imagine my joy therefore when a quick search on Google found me this: python-weather-api. It’s very simple to use as you shall see (I have pretty much reproduced the example from the sufficiently brief python wrapper documentation!)

Just to note that I ran this example from within the terminal, not a script. I am using the Yahoo Weather API with the Hull, UK short code (UKXX0075).

>>> import pywapi
>>> import pprint
>>> pp = pprint.PrettyPrinter(indent=4)
>>> result = pywapi.get_weather_from_yahoo('UKXX0075', 'metric')
>>> pp.pprint(result)
{   'astronomy': {   'sunrise': u'5:32 am', 'sunset': u'8:39 pm'},
'atmosphere': {   'humidity': u'68',
'pressure': u'1015.92',
'rising': u'0',
'visibility': u'9.99'},
'condition': {   'code': u'30',
'date': u'Tue, 11 Aug 2009 9:50 am BST',
'temp': u'19',
'text': u'Partly Cloudy',
'title': u'Conditions for Kingston upon Hull, UK at 9:50 am BST'},
'forecasts': [   {   'code': u'20',
'date': u'11 Aug 2009',
'high': u'22',
'low': u'15',
'text': u'AM Fog/PM Sun'},
{   'code': u'11',
'date': u'12 Aug 2009',
'high': u'21',
'low': u'11',
'text': u'AM Light Rain'}],
'geo': {   'lat': u'53.76', 'long': u'-.3'},
'html_description': u'\n<img src="http://l.yimg.com/a/i/us/we/52/30.gif" alt="" />
\n<strong>Current Conditions:</strong>
\nPartly Cloudy, 19 C
\n
<strong>Forecast:</strong>
\nTue - AM Fog/PM Sun. High: 22 Low: 15
\nWed - AM Light Rain. High: 21 Low: 11
\n
\n<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Kingston_upon_Hull__UK/*http://weather.yahoo.com/forecast/UKXX0075_c.html">Full Forecast at Yahoo! Weather</a></code>

\n(provided by <a href="http://www.weather.com">The Weather Channel</a>)
\n',
'link': u'http://us.rd.yahoo.com/dailynews/rss/weather/Kingston_upon_Hull__UK/*http://weather.yahoo.com/forecast/UKXX0075_c.html',
'location': {   'city': u'Kingston upon Hull',
'country': u'UK',
'region': u''},
'title': u'Yahoo! Weather - Kingston upon Hull, UK',
'units': {   'distance': u'km',
'pressure': u'mb',
'speed': u'kph',
'temperature': u'C'},
'wind': {   'chill': u'19', 'direction': u'290', 'speed': u'11.27'}}
>>>

That’s it! Thanks goes to qetzal for his work!

  • Share/Bookmark

2 Comments on “Getting Weather Information with Python”

You can track this conversation through its atom feed.

  1. Nick Coleman says:

    Another alternative is the pyephem module. If you are familiar with xephem, then you already know the API as it uses xephem’s syntax. It provides astronomy calculations, of which sunrise and sunset are part, as well as astronomical sunrise and sunset which could be more suited to controlling light switches. Astronomical sunrise and sunset aim at showing the amount of brightness in the sky, or how dark it is.

    One possible benefit of xephem is that you don’t need an internet connection.

    On a side note, if you are also using the weather forecast of yahoo, is it ok in the UK? Its forecast here in Australia is so inaccurate as to be useless. It regularly differs from the official forecast by 5C to 10c, or 9F to 18F.

  2. Chris Ramsay says:

    Thanks for that info Nick – I’d not thought of using that before. I’ll check it out.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>