askcompu

Untitled

May 24th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.33 KB | None | 0 0
  1. def alerts(phenny,input):
  2.     """Checks for weather alerts."""
  3.     query = input.group(2)
  4.     if not query: return phenny.reply('.wa what?')
  5.     alertstext, alertsnumber = alerts_search(query, phenny)
  6.     if alertstext:
  7.         if alertstext.startswith('Error Code'):
  8.             phenny.say("Sorry, " + input.nick +", I got an error. Here's the error i got, " + alertstext)
  9.         else:
  10.             phenny.say("Here's what I got, " + input.nick + ": " + alertstext)
  11.     else: phenny.say("Sorry " + input.nick + ', try something more specific than "' + query + '"')
  12.    
  13. alerts.commands = ['wa', 'alert', 'alerts']
  14. alerts.example = '.wa San Francisco, CA'
  15.  
  16. def alerts_search(query, phenny, jsonstring=None):
  17.     if hasattr(phenny.config, 'wunderground_api_key'):
  18.         if not jsonstring:
  19.             query = query.replace('!', '')
  20.             query = query.replace(' ', '_')
  21.             query = web.quote(query)
  22.             uri = 'https://api.wunderground.com/api/' + phenny.config.wunderground_api_key + '/alerts/q/' + query + '.json'
  23.             rec_bytes = web.get(uri)
  24.             jsonstring = json.loads(rec_bytes)
  25.         werror = 0
  26.         try:
  27.             werrorexist = jsonstring['response']['error']['type']
  28.             werror = 1
  29.         except:
  30.             werror = 0
  31.         if werror is 1:
  32.             werrortype = jsonstring['response']['error']['type']
  33.             werrordesc = jsonstring['response']['error']['description']
  34.             werrorfull = 'Error Code: ' + werrortype + ' - ' + werrordesc
  35.             return werrorfull, 0
  36.         try:
  37.             alertsnumber = len(jsonstring['alerts'])
  38.             if alertsnumber > 0:
  39.                 alertstext = "You have " + str(alertsnumber)
  40.                 if alertsnumber > 1:
  41.                     alertstext = alertstext + " alerts: "
  42.                 else:
  43.                     alertstext = alertstext + " alert: "
  44.                 for currentnum, alertsdict in enumerate(jsonstring['alerts'], start=1):
  45.                     alertstype = alertsdict['description']
  46.                     alertsend = alertsdict['expires']
  47.                     alertsdetails = alertsdict['message']
  48.                     alertsdetails = alertsdetails.replace('\u000A', ' ')
  49.                     if alertsdict['type'] == "SPE":
  50.                         alertsdetails = re.search(r'\.\.\.(.*?)\.\.\.', alertsdetails).group(1)
  51.                         alertstext = alertstext + "Alert " + str(currentnum) + " is a " + alertstype + " (" + alertsdetails + ") and expires at " + alertsend
  52.                     else:
  53.                         alertstext = alertstext + "Alert " + str(currentnum) + " is a " + alertstype + " and expires at " + alertsend
  54.                     if not currentnum == alertsnumber:
  55.                         alertstext = alertstext + ", "
  56.                 return alertstext, alertsnumber
  57.             else:
  58.                 return "You have no alerts", 0
  59.         except KeyError:
  60.             return None, 0
  61.     else:
  62.         return 'Sorry but you need to set your wunderground_api_key in the config file.', 0
  63.            
  64.            
  65.        
  66.  
  67. def weather_search(query, phenny):
  68.     if hasattr(phenny.config, 'wunderground_api_key'):
  69.         query = query.replace('!', '')
  70.         query = query.replace(' ', '_')
  71.         query = web.quote(query)
  72.         uri = 'https://api.wunderground.com/api/' + phenny.config.wunderground_api_key + '/conditions/alerts/q/' + query + '.json'
  73.         rec_bytes = web.get(uri)
  74.         jsonstring = json.loads(rec_bytes)
  75.         werror = 0
  76.         try:
  77.             werrorexist = jsonstring['response']['error']['type']
  78.             werror = 1
  79.         except:
  80.             werror = 0
  81.            
  82.         if werror is 1:
  83.             werrortype = jsonstring['response']['error']['type']
  84.             werrordesc = jsonstring['response']['error']['description']
  85.             werrorfull = 'Error Code: ' + werrortype + ' - ' + werrordesc
  86.             return werrorfull, None
  87.         else:
  88.             try:
  89.                 wcity = jsonstring['current_observation']['display_location']['full']
  90.                 wwinddir = str(jsonstring['current_observation']['wind_dir'])
  91.                 wwindspd = str(jsonstring['current_observation']['wind_mph'])
  92.                 wwindgust = str(jsonstring['current_observation']['wind_gust_mph'])
  93.                 wtemp = jsonstring['current_observation']['temperature_string']
  94.                 wfeels = jsonstring['current_observation']['feelslike_string']
  95.                 wuv = str(jsonstring['current_observation']['UV'])
  96.                 wcondition = jsonstring['current_observation']['weather']
  97.                 degree_sign = u'\N{DEGREE SIGN}'
  98.                 wurl = 'http://www.wunderground.com/?apiref=5284b9a94c2a6666'
  99.                 wtemp = wtemp.replace(' F', degree_sign + 'F')
  100.                 wtemp = wtemp.replace(' C', degree_sign + 'C')
  101.                 wfeels = wfeels.replace(' F', degree_sign + 'F')
  102.                 wfeels = wfeels.replace(' C', degree_sign + 'C')
  103.                
  104.                 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
  105.             except KeyError:
  106.                 return None, None
  107.     else:
  108.         return 'Sorry but you need to set your wunderground_api_key in the config file.', None
  109.  
  110. def weather(phenny, input):
  111.     """Queries Wunderground for the weather."""
  112.     query = input.group(2)
  113.     if not query: return phenny.reply('.w what?')
  114.  
  115.     uri, jsonstring = weather_search(query, phenny)
  116.     if uri:
  117.         if uri.startswith('Error Code'):
  118.             phenny.say("Sorry, " + input.nick +", I got an error. Here's the error i got, " + uri)
  119.         else:
  120.             phenny.say("Here's what I got, " + input.nick + ": " + uri)
  121.             if not hasattr(phenny.bot, 'last_seen_uri'):
  122.                 phenny.bot.last_seen_uri = {}
  123.             phenny.bot.last_seen_uri[input.sender] = uri
  124.             if jsonstring:
  125.                 alertstext, alertsnumber = alerts_search(query, phenny, jsonstring)
  126.                 if alertsnumber > 0:
  127.                     alertstext = "You have " + str(alertsnumber)
  128.                     if alertsnumber > 1:
  129.                         alertstext = alertstext + " weather alerts"
  130.                     else:
  131.                         alertstext = alertstext + " weather alert"
  132.                     alertstext = alertstext + ", please use .wa " + query + " to see "
  133.                     if alertsnumber > 1:
  134.                         alertstext = alertstext + "them."
  135.                     else:
  136.                         alertstext = alertstext + "it."
  137.                     phenny.say(alertstext)
  138.     else: phenny.say("Sorry " + input.nick + ', try something more specific than "' + query + '"')
  139. weather.commands = ['w', 'weather']
  140. weather.example = '.w San Francisco, CA'
  141.  
  142. def forecast_search(query, phenny):
  143.     if hasattr(phenny.config, 'wunderground_api_key'):
  144.         query = query.replace('!', '')
  145.         query = query.replace(' ', '_')
  146.         query = web.quote(query)
  147.         uri = 'https://api.wunderground.com/api/' + phenny.config.wunderground_api_key + '/conditions/forecast/alerts/q/' + query + '.json'
  148.         rec_bytes = web.get(uri)
  149.         jsonstring = json.loads(rec_bytes)
  150.         wferror = 0
  151.         try:
  152.             wferrorexist = jsonstring['response']['error']['type']
  153.             wferror = 1
  154.         except:
  155.             wferror = 0
  156.            
  157.         if wferror is 1:
  158.             wferrortype = jsonstring['response']['error']['type']
  159.             wferrordesc = jsonstring['response']['error']['description']
  160.             wferrorfull = 'Error Code: ' + wferrortype + ' - ' + wferrordesc
  161.             return wferrorfull, None
  162.         else:
  163.             try:
  164.                 wfcity = jsonstring['current_observation']['display_location']['full']
  165.                 wfdate1 = jsonstring['forecast']['simpleforecast']['forecastday'][0]['date']['weekday']
  166.                 wfdate2 = jsonstring['forecast']['simpleforecast']['forecastday'][1]['date']['weekday']
  167.                 wfdate3 = jsonstring['forecast']['simpleforecast']['forecastday'][2]['date']['weekday']
  168.                 wfdate4 = jsonstring['forecast']['simpleforecast']['forecastday'][3]['date']['weekday']
  169.                 wfcond1 = jsonstring['forecast']['simpleforecast']['forecastday'][0]['conditions']
  170.                 wfcond2 = jsonstring['forecast']['simpleforecast']['forecastday'][1]['conditions']
  171.                 wfcond3 = jsonstring['forecast']['simpleforecast']['forecastday'][2]['conditions']
  172.                 wfcond4 = jsonstring['forecast']['simpleforecast']['forecastday'][3]['conditions']
  173.                 wfhigh1f = str(jsonstring['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'])
  174.                 wfhigh2f = str(jsonstring['forecast']['simpleforecast']['forecastday'][1]['high']['fahrenheit'])
  175.                 wfhigh3f = str(jsonstring['forecast']['simpleforecast']['forecastday'][2]['high']['fahrenheit'])
  176.                 wfhigh4f = str(jsonstring['forecast']['simpleforecast']['forecastday'][3]['high']['fahrenheit'])
  177.                 wfhigh1c = str(jsonstring['forecast']['simpleforecast']['forecastday'][0]['high']['celsius'])
  178.                 wfhigh2c = str(jsonstring['forecast']['simpleforecast']['forecastday'][1]['high']['celsius'])
  179.                 wfhigh3c = str(jsonstring['forecast']['simpleforecast']['forecastday'][2]['high']['celsius'])
  180.                 wfhigh4c = str(jsonstring['forecast']['simpleforecast']['forecastday'][3]['high']['celsius'])
  181.                 wflow1f = str(jsonstring['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'])
  182.                 wflow2f = str(jsonstring['forecast']['simpleforecast']['forecastday'][1]['low']['fahrenheit'])
  183.                 wflow3f = str(jsonstring['forecast']['simpleforecast']['forecastday'][2]['low']['fahrenheit'])
  184.                 wflow4f = str(jsonstring['forecast']['simpleforecast']['forecastday'][3]['low']['fahrenheit'])
  185.                 wflow1c = str(jsonstring['forecast']['simpleforecast']['forecastday'][0]['low']['celsius'])
  186.                 wflow2c = str(jsonstring['forecast']['simpleforecast']['forecastday'][1]['low']['celsius'])
  187.                 wflow3c = str(jsonstring['forecast']['simpleforecast']['forecastday'][2]['low']['celsius'])
  188.                 wflow4c = str(jsonstring['forecast']['simpleforecast']['forecastday'][3]['low']['celsius'])
  189.                
  190.                 degree_sign = u'\N{DEGREE SIGN}'
  191.                
  192.                 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
  193.             except KeyError:
  194.                 return None, None
  195.     else:
  196.         return 'Sorry but you need to set your wunderground_api_key in the config file.', None
  197.  
  198. def forecast(phenny, input):
  199.     """Queries Wunderground for the weather forecast."""
  200.     query = input.group(2)
  201.     if not query: return phenny.reply('.wf what?')
  202.  
  203.     uri, jsonstring = forecast_search(query, phenny)
  204.     if uri:
  205.         if uri.startswith('Error Code'):
  206.             phenny.say("Sorry, " + input.nick +", I got an error. Here's the error i got, " + uri)
  207.         else:
  208.             wfurl = 'http://www.wunderground.com/?apiref=5284b9a94c2a6666'
  209.             phenny.say("Here's what I got, " + input.nick + ": " + uri)
  210.             phenny.say("Weather from " + wfurl)
  211.             if not hasattr(phenny.bot, 'last_seen_uri'):
  212.                 phenny.bot.last_seen_uri = {}
  213.             phenny.bot.last_seen_uri[input.sender] = uri
  214.             alertstext, alertsnumber = alerts_search(query, phenny)
  215.             if jsonstring:
  216.                 alertstext, alertsnumber = alerts_search(query, phenny, jsonstring)
  217.                 if alertsnumber > 0:
  218.                     alertstext = "You have " + str(alertsnumber)
  219.                     if alertsnumber > 1:
  220.                         alertstext = alertstext + " weather alerts"
  221.                     else:
  222.                         alertstext = alertstext + " weather alert"
  223.                     alertstext = alertstext + ", please use .wa " + query + " to see "
  224.                     if alertsnumber > 1:
  225.                         alertstext = alertstext + "them."
  226.                     else:
  227.                         alertstext = alertstext + "it."
  228.                     phenny.say(alertstext)
  229.     else: phenny.say("Sorry " + input.nick + ', try something more specific than "' + query + '"')
  230. forecast.commands = ['wf', 'forecast']
  231. forecast.example = '.wf San Francisco, CA'
Advertisement
Add Comment
Please, Sign In to add comment