import urllib
import datetime
from xml.etree.ElementTree import parse

# See: http://developer.yahoo.com/weather
WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss?p=%s&u=c'
WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'

def GetWeather(location):
        url = WEATHER_URL % location
        rss = parse(urllib.urlopen(url)).getroot()
        c = {}

        c['Title'] = rss.findtext('channel/title')
        c['Link'] = rss.findtext('channel/link')
        c['Language'] = rss.findtext('channel/language')
        c['Description'] = rss.findtext('channel/description')
        c['LastBuildDate'] = rss.findtext('channel/lastBuildDate')
        c['TTL'] = rss.findtext('channel/ttl')

        element = rss.find('channel/{%s}location' % WEATHER_NS)
        c['LocationCity'] = element.get('city')
        c['LocationRegion'] = element.get('region')
        c['LocationCountry'] = element.get('country')
    
        element = rss.find('channel/{%s}units' % WEATHER_NS)
        c['UnitsTemperature'] = element.get('temperature')
        c['UnitsDistance'] = element.get('distance')
        c['UnitsPressure'] = element.get('pressure')
        c['UnitsSpeed'] = element.get('speed')

        element = rss.find('channel/{%s}wind' % WEATHER_NS)
        c['WindChill'] = element.get('chill')
        c['WindDirection'] = element.get('direction')
        c['WindSpeed'] = element.get('speed')

        element = rss.find('channel/{%s}atmosphere' % WEATHER_NS)
        c['AtmosphereHumidity'] = element.get('humidity')
        c['AtmospherePisibility'] = element.get('visibility')
        c['AtmospherePressure'] = element.get('pressure')
        c['AtmosphereRising'] = element.get('rising')

        element = rss.find('channel/{%s}astronomy' % WEATHER_NS)
        c['AstronomySunrise'] = element.get('sunrise')
        c['AstronomySunset'] = element.get('sunset')
    
        c['ItemTitle'] =  rss.findtext('channel/item/title')
        c['ItemLink'] = rss.findtext('channel/item/link')
        c['ItemDescription'] = rss.findtext('channel/item/description')
        c['ItemGuid'] = rss.findtext('channel/item/guid') 
        c['ItemPubdate'] = rss.findtext('channel/item/pubDate')
    
        element = rss.find('channel/item/{%s}condition' % WEATHER_NS)
        c['ConditionText'] = element.get('text')
        c['ConditionCode'] = element.get('code')
        c['ConditionTemp'] = element.get('temp')
        c['ConditionDate'] = element.get('date')

        element = rss.find('channel/item/{%s}forecast' % WEATHER_NS)
        c['ForecastDay'] = element.get('day')
        c['ForecastDate'] = element.get('date')
        c['ForecastLow'] = element.get('low')
        c['ForecastHigh'] = element.get('high')
        c['ForecastCode'] = element.get('code')

        return c