Actually learned how the API works, cut out a bunch of stuff
This commit is contained in:
parent
98f9e814b7
commit
6564338b94
|
@ -1,6 +1,6 @@
|
||||||
from util import hook, http, web
|
from util import hook, http, web
|
||||||
|
|
||||||
base_url = "http://api.wunderground.com/api/{}/geolookup/{}/q/{}.json"
|
base_url = "http://api.wunderground.com/api/{}/{}/q/{}.json"
|
||||||
|
|
||||||
|
|
||||||
@hook.command(autohelp=None)
|
@hook.command(autohelp=None)
|
||||||
|
@ -40,48 +40,39 @@ def weather(inp, reply=None, db=None, nick=None, bot=None):
|
||||||
|
|
||||||
location = http.quote_plus(loc)
|
location = http.quote_plus(loc)
|
||||||
|
|
||||||
conditions_url = base_url.format(api_key, "conditions", location)
|
request_url = base_url.format(api_key, "geolookup/forecast/conditions", location)
|
||||||
conditions = http.get_json(conditions_url)
|
response = http.get_json(request_url)
|
||||||
|
|
||||||
try:
|
if 'location' not in response:
|
||||||
conditions['location']['city']
|
|
||||||
|
|
||||||
# found result, lets get the rest of the data
|
|
||||||
forecast_url = base_url.format(api_key, "forecast", location)
|
|
||||||
forecast = http.get_json(forecast_url)
|
|
||||||
except KeyError:
|
|
||||||
try:
|
try:
|
||||||
# try and get the closest match
|
location_id = response['response']['results'][0]['zmw']
|
||||||
location_id = conditions['response']['results'][0]['zmw']
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return "Could not get weather for that location."
|
return "Could not get weather for that location."
|
||||||
|
|
||||||
# get the conditions again, using the closest match
|
# get the weather again, using the closest match
|
||||||
conditions_url = base_url.format(api_key, "conditions", "zmw:" + location_id)
|
request_url = base_url.format(api_key, "geolookup/forecast/conditions", "zmw:" + location_id)
|
||||||
conditions = http.get_json(conditions_url)
|
response = http.get_json(request_url)
|
||||||
forecast_url = base_url.format(api_key, "forecast", "zmw:" + location_id)
|
|
||||||
forecast = http.get_json(forecast_url)
|
|
||||||
|
|
||||||
if conditions['location']['state']:
|
if response['location']['state']:
|
||||||
place_name = "\x02{}\x02, \x02{}\x02 (\x02{}\x02)".format(conditions['location']['city'],
|
place_name = "\x02{}\x02, \x02{}\x02 (\x02{}\x02)".format(response['location']['city'],
|
||||||
conditions['location']['state'], conditions['location']['country'])
|
response['location']['state'], response['location']['country'])
|
||||||
else:
|
else:
|
||||||
place_name = "\x02{}\x02 (\x02{}\x02)".format(conditions['location']['city'],
|
place_name = "\x02{}\x02 (\x02{}\x02)".format(response['location']['city'],
|
||||||
conditions['location']['country'])
|
response['location']['country'])
|
||||||
|
|
||||||
forecast_today = forecast["forecast"]["simpleforecast"]["forecastday"][0]
|
forecast_today = response["forecast"]["simpleforecast"]["forecastday"][0]
|
||||||
forecast_tomorrow = forecast["forecast"]["simpleforecast"]["forecastday"][1]
|
forecast_tomorrow = response["forecast"]["simpleforecast"]["forecastday"][1]
|
||||||
|
|
||||||
# put all the stuff we want to use in a dictionary for easy formatting of the output
|
# put all the stuff we want to use in a dictionary for easy formatting of the output
|
||||||
weather_data = {
|
weather_data = {
|
||||||
"place": place_name,
|
"place": place_name,
|
||||||
"conditions": conditions['current_observation']['weather'],
|
"conditions": response['current_observation']['weather'],
|
||||||
"temp_f": conditions['current_observation']['temp_f'],
|
"temp_f": response['current_observation']['temp_f'],
|
||||||
"temp_c": conditions['current_observation']['temp_c'],
|
"temp_c": response['current_observation']['temp_c'],
|
||||||
"humidity": conditions['current_observation']['relative_humidity'],
|
"humidity": response['current_observation']['relative_humidity'],
|
||||||
"wind_kph": conditions['current_observation']['wind_kph'],
|
"wind_kph": response['current_observation']['wind_kph'],
|
||||||
"wind_mph": conditions['current_observation']['wind_mph'],
|
"wind_mph": response['current_observation']['wind_mph'],
|
||||||
"wind_direction": conditions['current_observation']['wind_dir'],
|
"wind_direction": response['current_observation']['wind_dir'],
|
||||||
"today_conditions": forecast_today['conditions'],
|
"today_conditions": forecast_today['conditions'],
|
||||||
"today_high_f": forecast_today['high']['fahrenheit'],
|
"today_high_f": forecast_today['high']['fahrenheit'],
|
||||||
"today_high_c": forecast_today['high']['celsius'],
|
"today_high_c": forecast_today['high']['celsius'],
|
||||||
|
@ -92,7 +83,7 @@ def weather(inp, reply=None, db=None, nick=None, bot=None):
|
||||||
"tomorrow_high_c": forecast_tomorrow['high']['celsius'],
|
"tomorrow_high_c": forecast_tomorrow['high']['celsius'],
|
||||||
"tomorrow_low_f": forecast_tomorrow['low']['fahrenheit'],
|
"tomorrow_low_f": forecast_tomorrow['low']['fahrenheit'],
|
||||||
"tomorrow_low_c": forecast_tomorrow['low']['celsius'],
|
"tomorrow_low_c": forecast_tomorrow['low']['celsius'],
|
||||||
"url": web.isgd(conditions["current_observation"]['forecast_url'] + "?apiref=e535207ff4757b18")
|
"url": web.isgd(response["current_observation"]['forecast_url'] + "?apiref=e535207ff4757b18")
|
||||||
}
|
}
|
||||||
|
|
||||||
reply("{place} - \x02Current:\x02 {conditions}, {temp_f}F/{temp_c}C, {humidity}, "
|
reply("{place} - \x02Current:\x02 {conditions}, {temp_f}F/{temp_c}C, {humidity}, "
|
||||||
|
|
Reference in a new issue