Example #1
0
    def test_fetch_xml1(self):

        url = 'http://weather.yahooapis.com/forecastrss?w=2502265'
        rss = utils.fetch_xml(url)

        expected_root = 'rss'
        actual_root = rss.tag
        self.assertEqual(expected_root, actual_root)

        weather_ns = 'http://xml.weather.yahoo.com/ns/rss/1.0'
        location_tag = '{%s}location' % weather_ns
        units_tag = '{%s}units' % weather_ns
        wind_tag = '{%s}wind' % weather_ns
        atmosphere_tag = '{%s}atmosphere' % weather_ns
        astronomy_tag = '{%s}astronomy' % weather_ns
        expected_tags = ['title', 'link', 'description', 'language', 'lastBuildDate', 'ttl', location_tag,
                         units_tag, wind_tag, atmosphere_tag, astronomy_tag]

        valid_xml = True
        for tag in expected_tags:
            element = rss.find('channel/%s' %tag)

            if element is None:
                valid_xml = False

        self.assertTrue(valid_xml)
Example #2
0
def yahoo_conditions(location, units='f'):
    """
    Gets the current weather conditions from Yahoo weather. For more information, see
    https://developer.yahoo.com/weather/.

    :param location: a location in 'city, state, country' format (e.g. Salt Lake City, Utah, United States)
    :param units: fahrenheit by default (f). You may also choose celsius by entering c instead of f.
    :return: The current weather conditions for the given location. None if the location is invalid.
    """

    weather_url = 'http://weather.yahooapis.com/forecastrss?w=%s&u=%s'
    weather_ns = 'http://xml.weather.yahoo.com/ns/rss/1.0'
    woeid = utils.fetch_woeid(location)

    if woeid is None:
        return None

    url = weather_url % (woeid, units)

    # Try to parse the RSS feed at the given URL.
    try:
        rss = utils.fetch_xml(url)
        conditions = rss.find('channel/item/{%s}condition' % weather_ns)

        return {
            'title': rss.findtext('channel/title'),
            'current_condition': conditions.get('text'),
            'current_temp': conditions.get('temp'),
            'date': conditions.get('date'),
            'code': conditions.get('code')
        }
    except:
        raise