In [1]:
import passwords
import base64

In [2]:
my_info = passwords.get_secure_info()

In [5]:
weather_key = base64.b64decode(my_info['weather_key'])
url = 'http://api.wunderground.com/api/%s/conditions/q/NY/New_York.json' % (weather_key)

In [6]:
def connection(url):
    '''
    connects to yelp to extract data
    '''
    import urllib
    import urllib2
    import json
    
    try:
        conn = urllib2.urlopen(url, None)
        try:
            data = json.loads(conn.read())
        finally:
            conn.close()
    except urllib2.HTTPError, error:
        data = json.loads(error.read())
    
    return data

In [7]:
data = connection(url)['current_observation']
data


Out[7]:
{u'UV': u'7',
 u'dewpoint_c': 17,
 u'dewpoint_f': 62,
 u'dewpoint_string': u'62 F (17 C)',
 u'display_location': {u'city': u'New York',
  u'country': u'US',
  u'country_iso3166': u'US',
  u'elevation': u'17.00000000',
  u'full': u'New York, NY',
  u'latitude': u'40.75013351',
  u'longitude': u'-73.99700928',
  u'magic': u'1',
  u'state': u'NY',
  u'state_name': u'New York',
  u'wmo': u'99999',
  u'zip': u'10001'},
 u'estimated': {},
 u'feelslike_c': u'28',
 u'feelslike_f': u'82',
 u'feelslike_string': u'82 F (28 C)',
 u'forecast_url': u'http://www.wunderground.com/US/NY/New_York.html',
 u'heat_index_c': 28,
 u'heat_index_f': 82,
 u'heat_index_string': u'82 F (28 C)',
 u'history_url': u'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KNYNEWYO71',
 u'icon': u'partlycloudy',
 u'icon_url': u'http://icons-ak.wxug.com/i/c/k/partlycloudy.gif',
 u'image': {u'link': u'http://www.wunderground.com',
  u'title': u'Weather Underground',
  u'url': u'http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png'},
 u'local_epoch': u'1374698008',
 u'local_time_rfc822': u'Wed, 24 Jul 2013 16:33:28 -0400',
 u'local_tz_long': u'America/New_York',
 u'local_tz_offset': u'-0400',
 u'local_tz_short': u'EDT',
 u'ob_url': u'http://www.wunderground.com/cgi-bin/findweather/getForecast?query=40.746399,-73.981598',
 u'observation_epoch': u'1374697986',
 u'observation_location': {u'city': u'Murray Hill, New York',
  u'country': u'US',
  u'country_iso3166': u'US',
  u'elevation': u'325 ft',
  u'full': u'Murray Hill, New York, New York',
  u'latitude': u'40.746399',
  u'longitude': u'-73.981598',
  u'state': u'New York'},
 u'observation_time': u'Last Updated on July 24, 4:33 PM EDT',
 u'observation_time_rfc822': u'Wed, 24 Jul 2013 16:33:06 -0400',
 u'precip_1hr_in': u'0.00',
 u'precip_1hr_metric': u' 0',
 u'precip_1hr_string': u'0.00 in ( 0 mm)',
 u'precip_today_in': u'0.00',
 u'precip_today_metric': u'0',
 u'precip_today_string': u'0.00 in (0 mm)',
 u'pressure_in': u'29.78',
 u'pressure_mb': u'1008',
 u'pressure_trend': u'0',
 u'relative_humidity': u'53%',
 u'solarradiation': u'',
 u'station_id': u'KNYNEWYO71',
 u'temp_c': 27.1,
 u'temp_f': 80.8,
 u'temperature_string': u'80.8 F (27.1 C)',
 u'visibility_km': u'16.1',
 u'visibility_mi': u'10.0',
 u'weather': u'Partly Cloudy',
 u'wind_degrees': 284,
 u'wind_dir': u'WNW',
 u'wind_gust_kph': u'22.5',
 u'wind_gust_mph': u'14.0',
 u'wind_kph': 11.3,
 u'wind_mph': 7.0,
 u'wind_string': u'From the WNW at 7.0 MPH Gusting to 14.0 MPH',
 u'windchill_c': u'NA',
 u'windchill_f': u'NA',
 u'windchill_string': u'NA'}

In [19]:
print data['feelslike_f'], data['precip_1hr_in'], data['icon_url']


82 0.00 http://icons-ak.wxug.com/i/c/k/partlycloudy.gif

In [23]:
def good_enough_to_walk(data):
    '''
    Is it a good day for a walk? Let's find out.
    '''
    outside_day = False
    if (45 < data['feelslike_f'] > 85) and data['precip_1hr_in'] == '0.00':
        outside_day = True
    return outside_day

In [24]:
good_enough_to_walk(data)


True
True
Out[24]:
True

In [ ]: