2013-07-09 01:19:36 +12:00
from util import hook , http , web
2012-09-11 10:17:30 +12:00
2013-07-09 01:35:53 +12:00
base_url = " http://api.wunderground.com/api/ {} / {} /q/ {} .json "
2013-07-09 01:19:36 +12:00
@hook.command ( autohelp = None )
2013-08-02 12:05:44 +12:00
def weather ( inp , reply = None , db = None , nick = None , bot = None , notice = None ) :
2013-09-04 18:30:04 +08:00
""" weather <location> [dontsave] -- Gets weather data
for < location > from Wunderground . """
2013-07-09 01:19:36 +12:00
api_key = bot . config . get ( " api_keys " , { } ) . get ( " wunderground " )
if not api_key :
return " Error: No wunderground API details. "
2011-11-20 22:23:31 +13:00
2014-02-13 15:02:44 +13:00
# initialise weather DB
2012-09-12 00:16:06 +12:00
db . execute ( " create table if not exists weather(nick primary key, loc) " )
2011-11-20 22:23:31 +13:00
2012-09-10 22:55:34 +12:00
# if there is no input, try getting the users last location from the DB
if not inp :
2012-09-12 00:16:06 +12:00
location = db . execute ( " select loc from weather where nick=lower(?) " ,
2013-09-04 18:30:04 +08:00
[ nick ] ) . fetchone ( )
2012-09-12 00:16:06 +12:00
if not location :
2012-09-10 22:55:34 +12:00
# no location saved in the database, send the user help text
2011-11-20 22:23:31 +13:00
notice ( weather . __doc__ )
return
2013-07-09 01:19:36 +12:00
loc = location [ 0 ]
2012-09-10 22:55:34 +12:00
# no need to save a location, we already have it
dontsave = True
else :
# see if the input ends with "dontsave"
dontsave = inp . endswith ( " dontsave " )
# remove "dontsave" from the input string after checking for it
if dontsave :
2013-07-09 01:19:36 +12:00
loc = inp [ : - 9 ] . strip ( ) . lower ( )
2012-09-10 22:55:34 +12:00
else :
2013-07-09 01:19:36 +12:00
loc = inp
location = http . quote_plus ( loc )
2013-07-09 01:35:53 +12:00
request_url = base_url . format ( api_key , " geolookup/forecast/conditions " , location )
response = http . get_json ( request_url )
2012-09-10 22:55:34 +12:00
2013-07-09 01:35:53 +12:00
if ' location ' not in response :
2013-07-09 01:19:36 +12:00
try :
2013-07-09 01:35:53 +12:00
location_id = response [ ' response ' ] [ ' results ' ] [ 0 ] [ ' zmw ' ]
2013-07-09 01:19:36 +12:00
except KeyError :
return " Could not get weather for that location. "
2013-07-09 01:35:53 +12:00
# get the weather again, using the closest match
request_url = base_url . format ( api_key , " geolookup/forecast/conditions " , " zmw: " + location_id )
response = http . get_json ( request_url )
2013-07-09 01:19:36 +12:00
2013-07-09 01:35:53 +12:00
if response [ ' location ' ] [ ' state ' ] :
place_name = " \x02 {} \x02 , \x02 {} \x02 ( \x02 {} \x02 ) " . format ( response [ ' location ' ] [ ' city ' ] ,
2013-09-04 18:30:04 +08:00
response [ ' location ' ] [ ' state ' ] ,
response [ ' location ' ] [ ' country ' ] )
2013-07-09 01:19:36 +12:00
else :
2013-07-09 01:35:53 +12:00
place_name = " \x02 {} \x02 ( \x02 {} \x02 ) " . format ( response [ ' location ' ] [ ' city ' ] ,
2013-09-04 18:30:04 +08:00
response [ ' location ' ] [ ' country ' ] )
2013-07-09 01:19:36 +12:00
2013-07-09 01:35:53 +12:00
forecast_today = response [ " forecast " ] [ " simpleforecast " ] [ " forecastday " ] [ 0 ]
forecast_tomorrow = response [ " forecast " ] [ " simpleforecast " ] [ " forecastday " ] [ 1 ]
2012-09-10 22:55:34 +12:00
2012-09-12 07:18:06 +12:00
# put all the stuff we want to use in a dictionary for easy formatting of the output
2012-09-12 07:39:15 +12:00
weather_data = {
2013-07-09 01:19:36 +12:00
" place " : place_name ,
2013-07-09 01:35:53 +12:00
" conditions " : response [ ' current_observation ' ] [ ' weather ' ] ,
" temp_f " : response [ ' current_observation ' ] [ ' temp_f ' ] ,
" temp_c " : response [ ' current_observation ' ] [ ' temp_c ' ] ,
" humidity " : response [ ' current_observation ' ] [ ' relative_humidity ' ] ,
" wind_kph " : response [ ' current_observation ' ] [ ' wind_kph ' ] ,
" wind_mph " : response [ ' current_observation ' ] [ ' wind_mph ' ] ,
" wind_direction " : response [ ' current_observation ' ] [ ' wind_dir ' ] ,
2013-07-09 01:19:36 +12:00
" today_conditions " : forecast_today [ ' conditions ' ] ,
" today_high_f " : forecast_today [ ' high ' ] [ ' fahrenheit ' ] ,
" today_high_c " : forecast_today [ ' high ' ] [ ' celsius ' ] ,
" today_low_f " : forecast_today [ ' low ' ] [ ' fahrenheit ' ] ,
" today_low_c " : forecast_today [ ' low ' ] [ ' celsius ' ] ,
" tomorrow_conditions " : forecast_tomorrow [ ' conditions ' ] ,
" tomorrow_high_f " : forecast_tomorrow [ ' high ' ] [ ' fahrenheit ' ] ,
" tomorrow_high_c " : forecast_tomorrow [ ' high ' ] [ ' celsius ' ] ,
" tomorrow_low_f " : forecast_tomorrow [ ' low ' ] [ ' fahrenheit ' ] ,
" tomorrow_low_c " : forecast_tomorrow [ ' low ' ] [ ' celsius ' ] ,
2013-07-09 01:35:53 +12:00
" url " : web . isgd ( response [ " current_observation " ] [ ' forecast_url ' ] + " ?apiref=e535207ff4757b18 " )
2012-09-12 17:04:04 +12:00
}
2013-07-09 01:19:36 +12:00
reply ( " {place} - \x02 Current: \x02 {conditions} , {temp_f} F/ {temp_c} C, {humidity} , "
" Wind: {wind_kph} KPH/ {wind_mph} MPH {wind_direction} , \x02 Today: \x02 {today_conditions} , "
" High: {today_high_f} F/ {today_high_c} C, Low: {today_low_f} F/ {today_low_c} C. "
" \x02 Tomorrow: \x02 {tomorrow_conditions} , High: {tomorrow_high_f} F/ {tomorrow_high_c} C, "
" Low: {tomorrow_low_f} F/ {tomorrow_low_c} C - {url} " . format ( * * weather_data ) )
2012-09-12 17:04:04 +12:00
if location and not dontsave :
db . execute ( " insert or replace into weather(nick, loc) values (?,?) " ,
2013-09-04 18:30:04 +08:00
( nick . lower ( ) , location ) )
2013-02-07 23:26:10 +13:00
db . commit ( )