From 9191312aac429e745388029ba57a29f015378541 Mon Sep 17 00:00:00 2001 From: Christian Franke Date: Thu, 20 Jun 2013 19:25:15 +0200 Subject: Add envmon code --- envmon/weather.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 envmon/weather.py (limited to 'envmon/weather.py') diff --git a/envmon/weather.py b/envmon/weather.py new file mode 100644 index 0000000..c5396c6 --- /dev/null +++ b/envmon/weather.py @@ -0,0 +1,24 @@ +import contextlib +import urllib2 +import xml.etree.ElementTree + +class WeatherData(object): + def __init__(self): + pass + +def weather(woeid): + """ + Takes a woeid as argument (see yahoo api docs) and returns a dictionary + containing weather data. + """ + + with contextlib.closing(urllib2.urlopen('http://weather.yahooapis.com/forecastrss?w=%s&u=c' % woeid, timeout=2.0)) as request: + tree = xml.etree.ElementTree.fromstring(request.read()) + + yweather = 'http://xml.weather.yahoo.com/ns/rss/1.0' + results = WeatherData() + + results.temperature = float(tree.find('.//{%s}condition' % yweather).get('temp')) + results.humidity = float(tree.find('.//{%s}atmosphere' % yweather).get('humidity')) + + return results -- cgit v1.2.1