Esempio n. 1
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
Esempio n. 2
0
    def test_fetch_woeid2(self):
        """
        Lookup the WOEID for an unknown location.
        """

        location = "unknown_location"
        expected_woeid = None
        actual_woeid = utils.fetch_woeid(location)
        self.assertEqual(expected_woeid, actual_woeid)
Esempio n. 3
0
    def test_fetch_woeid1(self):
        """
        Lookup the WOEID for various locations and assert they are all correct based on Ross Elliot's WOEID lookup. For
        more information, see http://woeid.rosselliot.co.nz/
        """

        # Test Salt Lake City
        location1 = "Salt Lake City, UT, United States"
        expected_woeid1 = "2487610"
        actual_woeid1 = utils.fetch_woeid(location1)
        self.assertEqual(expected_woeid1, actual_woeid1)

        # Test San Francisco
        location2 = "San Francisco, CA, United States"
        expected_woeid2 = "2487956"
        actual_woeid2 = utils.fetch_woeid(location2)
        self.assertEqual(expected_woeid2, actual_woeid2)

        # Test New York City
        location3 = "New York City, NY, United States"
        expected_woeid3 = "2459115"
        actual_woeid3 = utils.fetch_woeid(location3)
        self.assertEqual(expected_woeid3, actual_woeid3)