summaryrefslogtreecommitdiff
path: root/envmon/weather.py
blob: c5396c608c7c21692027553b065a8deeb4b11e25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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