Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def alerts(phenny,input):
- """Checks for weather alerts."""
- query = input.group(2)
- if not query: return phenny.reply('.wa what?')
- alertstext, alertsnumber = alerts_search(query, phenny)
- if alertstext:
- if alertstext.startswith('Error Code'):
- phenny.say("Sorry, " + input.nick +", I got an error. Here's the error i got, " + alertstext)
- else:
- phenny.say("Here's what I got, " + input.nick + ": " + alertstext)
- else: phenny.say("Sorry " + input.nick + ', try something more specific than "' + query + '"')
- alerts.commands = ['wa', 'alert', 'alerts']
- alerts.example = '.wa San Francisco, CA'
- def alerts_search(query, phenny, jsonstring=None):
- if hasattr(phenny.config, 'wunderground_api_key'):
- if not jsonstring:
- query = query.replace('!', '')
- query = query.replace(' ', '_')
- query = web.quote(query)
- uri = 'https://api.wunderground.com/api/' + phenny.config.wunderground_api_key + '/alerts/q/' + query + '.json'
- rec_bytes = web.get(uri)
- jsonstring = json.loads(rec_bytes)
- werror = 0
- try:
- werrorexist = jsonstring['response']['error']['type']
- werror = 1
- except:
- werror = 0
- if werror is 1:
- werrortype = jsonstring['response']['error']['type']
- werrordesc = jsonstring['response']['error']['description']
- werrorfull = 'Error Code: ' + werrortype + ' - ' + werrordesc
- return werrorfull, 0
- try:
- alertsnumber = len(jsonstring['alerts'])
- if alertsnumber > 0:
- alertstext = "You have " + str(alertsnumber)
- if alertsnumber > 1:
- alertstext = alertstext + " alerts: "
- else:
- alertstext = alertstext + " alert: "
- for currentnum, alertsdict in enumerate(jsonstring['alerts'], start=1):
- alertstype = alertsdict['description']
- alertsend = alertsdict['expires']
- alertsdetails = alertsdict['message']
- alertsdetails = alertsdetails.replace('\u000A', ' ')
- if alertsdict['type'] == "SPE":
- alertsdetails = re.search(r'\.\.\.(.*?)\.\.\.', alertsdetails).group(1)
- alertstext = alertstext + "Alert " + str(currentnum) + " is a " + alertstype + " (" + alertsdetails + ") and expires at " + alertsend
- else:
- alertstext = alertstext + "Alert " + str(currentnum) + " is a " + alertstype + " and expires at " + alertsend
- if not currentnum == alertsnumber:
- alertstext = alertstext + ", "
- return alertstext, alertsnumber
- else:
- return "You have no alerts", 0
- except KeyError:
- return None, 0
- else:
- return 'Sorry but you need to set your wunderground_api_key in the config file.', 0
- def weather_search(query, phenny):
- if hasattr(phenny.config, 'wunderground_api_key'):
- query = query.replace('!', '')
- query = query.replace(' ', '_')
- query = web.quote(query)
- uri = 'https://api.wunderground.com/api/' + phenny.config.wunderground_api_key + '/conditions/alerts/q/' + query + '.json'
- rec_bytes = web.get(uri)
- jsonstring = json.loads(rec_bytes)
- werror = 0
- try:
- werrorexist = jsonstring['response']['error']['type']
- werror = 1
- except:
- werror = 0
- if werror is 1:
- werrortype = jsonstring['response']['error']['type']
- werrordesc = jsonstring['response']['error']['description']
- werrorfull = 'Error Code: ' + werrortype + ' - ' + werrordesc
- return werrorfull, None
- else:
- try:
- wcity = jsonstring['current_observation']['display_location']['full']
- wwinddir = str(jsonstring['current_observation']['wind_dir'])
- wwindspd = str(jsonstring['current_observation']['wind_mph'])
- wwindgust = str(jsonstring['current_observation']['wind_gust_mph'])
- wtemp = jsonstring['current_observation']['temperature_string']
- wfeels = jsonstring['current_observation']['feelslike_string']
- wuv = str(jsonstring['current_observation']['UV'])
- wcondition = jsonstring['current_observation']['weather']
- degree_sign = u'\N{DEGREE SIGN}'
- wurl = 'http://www.wunderground.com/?apiref=5284b9a94c2a6666'
- wtemp = wtemp.replace(' F', degree_sign + 'F')
- wtemp = wtemp.replace(' C', degree_sign + 'C')
- wfeels = wfeels.replace(' F', degree_sign + 'F')
- wfeels = wfeels.replace(' C', degree_sign + 'C')
- return ('In ' + wcity + ' it is currently ' + wcondition + ', the temperature is ' + wtemp + ' and it feels like ' + wfeels + '. The wind speed is ' + wwindspd + ' MPH ' + wwinddir + ' with gusts of up to ' + wwindgust + " MPH. The UV level is " + wuv + ". Weather from " + wurl), jsonstring
- except KeyError:
- return None, None
- else:
- return 'Sorry but you need to set your wunderground_api_key in the config file.', None
- def weather(phenny, input):
- """Queries Wunderground for the weather."""
- query = input.group(2)
- if not query: return phenny.reply('.w what?')
- uri, jsonstring = weather_search(query, phenny)
- if uri:
- if uri.startswith('Error Code'):
- phenny.say("Sorry, " + input.nick +", I got an error. Here's the error i got, " + uri)
- else:
- phenny.say("Here's what I got, " + input.nick + ": " + uri)
- if not hasattr(phenny.bot, 'last_seen_uri'):
- phenny.bot.last_seen_uri = {}
- phenny.bot.last_seen_uri[input.sender] = uri
- if jsonstring:
- alertstext, alertsnumber = alerts_search(query, phenny, jsonstring)
- if alertsnumber > 0:
- alertstext = "You have " + str(alertsnumber)
- if alertsnumber > 1:
- alertstext = alertstext + " weather alerts"
- else:
- alertstext = alertstext + " weather alert"
- alertstext = alertstext + ", please use .wa " + query + " to see "
- if alertsnumber > 1:
- alertstext = alertstext + "them."
- else:
- alertstext = alertstext + "it."
- phenny.say(alertstext)
- else: phenny.say("Sorry " + input.nick + ', try something more specific than "' + query + '"')
- weather.commands = ['w', 'weather']
- weather.example = '.w San Francisco, CA'
- def forecast_search(query, phenny):
- if hasattr(phenny.config, 'wunderground_api_key'):
- query = query.replace('!', '')
- query = query.replace(' ', '_')
- query = web.quote(query)
- uri = 'https://api.wunderground.com/api/' + phenny.config.wunderground_api_key + '/conditions/forecast/alerts/q/' + query + '.json'
- rec_bytes = web.get(uri)
- jsonstring = json.loads(rec_bytes)
- wferror = 0
- try:
- wferrorexist = jsonstring['response']['error']['type']
- wferror = 1
- except:
- wferror = 0
- if wferror is 1:
- wferrortype = jsonstring['response']['error']['type']
- wferrordesc = jsonstring['response']['error']['description']
- wferrorfull = 'Error Code: ' + wferrortype + ' - ' + wferrordesc
- return wferrorfull, None
- else:
- try:
- wfcity = jsonstring['current_observation']['display_location']['full']
- wfdate1 = jsonstring['forecast']['simpleforecast']['forecastday'][0]['date']['weekday']
- wfdate2 = jsonstring['forecast']['simpleforecast']['forecastday'][1]['date']['weekday']
- wfdate3 = jsonstring['forecast']['simpleforecast']['forecastday'][2]['date']['weekday']
- wfdate4 = jsonstring['forecast']['simpleforecast']['forecastday'][3]['date']['weekday']
- wfcond1 = jsonstring['forecast']['simpleforecast']['forecastday'][0]['conditions']
- wfcond2 = jsonstring['forecast']['simpleforecast']['forecastday'][1]['conditions']
- wfcond3 = jsonstring['forecast']['simpleforecast']['forecastday'][2]['conditions']
- wfcond4 = jsonstring['forecast']['simpleforecast']['forecastday'][3]['conditions']
- wfhigh1f = str(jsonstring['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'])
- wfhigh2f = str(jsonstring['forecast']['simpleforecast']['forecastday'][1]['high']['fahrenheit'])
- wfhigh3f = str(jsonstring['forecast']['simpleforecast']['forecastday'][2]['high']['fahrenheit'])
- wfhigh4f = str(jsonstring['forecast']['simpleforecast']['forecastday'][3]['high']['fahrenheit'])
- wfhigh1c = str(jsonstring['forecast']['simpleforecast']['forecastday'][0]['high']['celsius'])
- wfhigh2c = str(jsonstring['forecast']['simpleforecast']['forecastday'][1]['high']['celsius'])
- wfhigh3c = str(jsonstring['forecast']['simpleforecast']['forecastday'][2]['high']['celsius'])
- wfhigh4c = str(jsonstring['forecast']['simpleforecast']['forecastday'][3]['high']['celsius'])
- wflow1f = str(jsonstring['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'])
- wflow2f = str(jsonstring['forecast']['simpleforecast']['forecastday'][1]['low']['fahrenheit'])
- wflow3f = str(jsonstring['forecast']['simpleforecast']['forecastday'][2]['low']['fahrenheit'])
- wflow4f = str(jsonstring['forecast']['simpleforecast']['forecastday'][3]['low']['fahrenheit'])
- wflow1c = str(jsonstring['forecast']['simpleforecast']['forecastday'][0]['low']['celsius'])
- wflow2c = str(jsonstring['forecast']['simpleforecast']['forecastday'][1]['low']['celsius'])
- wflow3c = str(jsonstring['forecast']['simpleforecast']['forecastday'][2]['low']['celsius'])
- wflow4c = str(jsonstring['forecast']['simpleforecast']['forecastday'][3]['low']['celsius'])
- degree_sign = u'\N{DEGREE SIGN}'
- return ('The forecast for ' + wfdate1 + ' in ' + wfcity + ' is ' + wfcond1 + ' with a high of ' + wfhigh1f + degree_sign + 'F (' + wfhigh1c + degree_sign + 'C) and a low of ' + wflow1f + degree_sign + 'F (' + wflow1c + degree_sign + 'C). On ' + wfdate2 + ' it will be ' + wfcond2 + ' with a high of ' + wfhigh2f + degree_sign + 'F (' + wfhigh2c + degree_sign + 'C) and a low of ' + wflow2f + degree_sign + 'F (' + wflow2c + degree_sign + 'C). On ' + wfdate3 + ' it will be ' + wfcond3 + ' with a high of ' + wfhigh3f + degree_sign + 'F (' + wfhigh3c + degree_sign + 'C) and a low of ' + wflow3f + degree_sign + 'F (' + wflow3c + degree_sign + 'C). On ' + wfdate4 + ' it will be ' + wfcond4 + ' with a high of ' + wfhigh4f + degree_sign + 'F (' + wfhigh4c + degree_sign + 'C) and a low of ' + wflow4f + degree_sign + 'F (' + wflow4c + degree_sign + 'C).'), jsonstring
- except KeyError:
- return None, None
- else:
- return 'Sorry but you need to set your wunderground_api_key in the config file.', None
- def forecast(phenny, input):
- """Queries Wunderground for the weather forecast."""
- query = input.group(2)
- if not query: return phenny.reply('.wf what?')
- uri, jsonstring = forecast_search(query, phenny)
- if uri:
- if uri.startswith('Error Code'):
- phenny.say("Sorry, " + input.nick +", I got an error. Here's the error i got, " + uri)
- else:
- wfurl = 'http://www.wunderground.com/?apiref=5284b9a94c2a6666'
- phenny.say("Here's what I got, " + input.nick + ": " + uri)
- phenny.say("Weather from " + wfurl)
- if not hasattr(phenny.bot, 'last_seen_uri'):
- phenny.bot.last_seen_uri = {}
- phenny.bot.last_seen_uri[input.sender] = uri
- alertstext, alertsnumber = alerts_search(query, phenny)
- if jsonstring:
- alertstext, alertsnumber = alerts_search(query, phenny, jsonstring)
- if alertsnumber > 0:
- alertstext = "You have " + str(alertsnumber)
- if alertsnumber > 1:
- alertstext = alertstext + " weather alerts"
- else:
- alertstext = alertstext + " weather alert"
- alertstext = alertstext + ", please use .wa " + query + " to see "
- if alertsnumber > 1:
- alertstext = alertstext + "them."
- else:
- alertstext = alertstext + "it."
- phenny.say(alertstext)
- else: phenny.say("Sorry " + input.nick + ', try something more specific than "' + query + '"')
- forecast.commands = ['wf', 'forecast']
- forecast.example = '.wf San Francisco, CA'
Advertisement
Add Comment
Please, Sign In to add comment