summaryrefslogtreecommitdiff
path: root/envmon/weather.py
diff options
context:
space:
mode:
authorChristian Franke <nobody@nowhere.ws>2013-06-20 19:25:15 +0200
committerChristian Franke <nobody@nowhere.ws>2013-06-20 19:25:31 +0200
commit9191312aac429e745388029ba57a29f015378541 (patch)
tree43fbbf740d0e2a0a231accb2db8e5f9df3af6c8c /envmon/weather.py
parent0ab0cbbda438fd30f24273f1db99d76746108a75 (diff)
Add envmon code
Diffstat (limited to 'envmon/weather.py')
-rw-r--r--envmon/weather.py24
1 files changed, 24 insertions, 0 deletions
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