def get_weather(self, e): #This callback handling code should be able to be reused in any other function if get_weather.waitfor_callback: return try: location = e.location except: location = e.input if location and user.get_location(location): location = user.get_location(location) #allow looking up by nickname if location == "" and user: location = user.get_location(e.nick) if location == "": get_weather.waitfor_callback = True user.get_geoIP_location(self, e, "", "", "", get_weather) return # Try weather functions in order forecast_io(self, e, location) if not e.output: get_wwo(self, location, e) if not e.output: return get_weather2(self, e) return e
def google_sunrise(self, e): #returns the next sunrise time and time from now of the place specified #This callback handling code should be able to be reused in any other function if google_sunrise.waitfor_callback: return try: location = e.location except: location = e.input if location == "" and user: location = user.get_location(e.nick) if location=="": google_sunrise.waitfor_callback=True user.get_geoIP_location(self, e, "", "", "", google_sunrise) return #End callback handling code e.output = google_sun(self, location, "Sunrise", e.nick) return e
def get_wolfram(self, e): #query 'input' on wolframalpha and get the plaintext result back if get_wolfram.waitfor_callback: return try: location = e.location except: location = "" if location == "" and user: location = user.get_location(e.nick) if location=="": get_wolfram.waitfor_callback=True user.get_geoIP_location(self, e, "", "", "", get_wolfram) return location = urllib.parse.quote(location) socket.setdefaulttimeout(30) url = "http://api.wolframalpha.com/v2/query?appid=%s&format=plaintext&input=%s&location=%s" % (self.botconfig["APIkeys"]["wolframAPIkey"], urllib.parse.quote(e.input), location) dom = xml.dom.minidom.parse(urllib.request.urlopen(url)) socket.setdefaulttimeout(10) if (dom.getElementsByTagName("queryresult")[0].getAttribute("success") == "false"): try: related = dom.getElementsByTagName("relatedexample")[0].getAttribute("input") e.input = related return get_wolfram(self, e) except Exception as inst: traceback.print_exc() print("!wolframrelated " + e.input + " : " + str(inst)) result = self.bangcommands["!error"](self, e).output e.output = result return e else: try: query = dom.getElementsByTagName("plaintext")[0].childNodes[0].data try: result = dom.getElementsByTagName("plaintext")[1].childNodes[0].data except: result = self.bangcommands["!error"](self, e).output output = query.replace("\n", " || ") + " :: " + result.replace("\n", " || ") e.output = output return e except Exception as inst: traceback.print_exc() print("!wolfram " + e.input + " : " + str(inst)) result = self.bangcommands["!error"](self, e).output e.output = result return e
def get_weather(self, e): # WWO weather of place specified in 'zip' # http://www.worldweatheronline.com/free-weather-feed.aspx #This callback handling code should be able to be reused in any other function if get_weather.waitfor_callback: return try: location = e.location except: location = e.input if location == "" and user: location = user.get_location(e.nick) if location == "": get_weather.waitfor_callback=True user.get_geoIP_location(self, e, "", "", "", get_weather) return location = urllib.parse.quote(location) #End callback handling code url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q={}&format=json&num_of_days=1&includeLocation=yes&key={}".format(location, self.botconfig["APIkeys"]["wwoAPIkey"]) response = urllib.request.urlopen(url).read().decode('utf-8') weather = json.loads(response) weatherdata = weather["data"] if 'error' not in weatherdata: country = weatherdata['nearest_area'][0]['country'][0]['value'] if country == "United States Of America" or country == "Canada" or country == "USA": country = "" elif country == "United Kingdom": country = ", UK" else: country = ", " + country try: region = ", " + weatherdata['nearest_area'][0]['region'][0]['value'] except: region = "" city = "%s%s%s" % (weatherdata['nearest_area'][0]['areaName'][0]['value'], region, country) desc = weatherdata['current_condition'][0]['weatherDesc'][0]['value'] temp = "{}°F {}°C".format(weatherdata['current_condition'][0]['temp_F'], weatherdata['current_condition'][0]['temp_C']) humidity = "%s%%" % (weatherdata['current_condition'][0]['humidity']) high = "{}°F {}°C".format(weatherdata['weather'][0]['tempMaxF'], weatherdata['weather'][0]['tempMaxC']) low = "{}°F {}°C".format(weatherdata['weather'][0]['tempMinF'], weatherdata['weather'][0]['tempMinC']) outlook = weatherdata['weather'][0]['weatherDesc'][0]['value'] if int(weatherdata['current_condition'][0]['cloudcover']) > 5: cloudcover = "Cloud Cover: %s%% / " % (weatherdata['current_condition'][0]['cloudcover']) else: cloudcover = "" if float(weatherdata['current_condition'][0]['precipMM']) > 0: precip = "Precipitation: %s mm / " % (weatherdata['current_condition'][0]['precipMM']) else: precip = "" if int(weatherdata['current_condition'][0]['visibility']) < 10: visibility = "Visibility: %skm / " % (weatherdata['current_condition'][0]['visibility']) else: visibility = "" if int(weatherdata['current_condition'][0]['windspeedMiles']) > 0: wind = "Wind: %s at %s mph (%s km/h) / " % (weatherdata['current_condition'][0]['winddir16Point'], weatherdata['current_condition'][0]['windspeedMiles'], weatherdata['current_condition'][0]['windspeedKmph']) else: wind = "" message = "{} / {} / {} / Humidity: {} / {}{}{}{}High: {} - Low: {} Outlook: {}".format(city, desc, temp, humidity, visibility, wind, cloudcover, precip, high, low, outlook) e.output = message return e else: return get_weather2(self, e)
def get_wolfram(self, e): #query 'input' on wolframalpha and get the plaintext result back if get_wolfram.waitfor_callback: return try: location = e.location except: location = "" if location == "" and user: location = user.get_location(e.nick) if location == "": get_wolfram.waitfor_callback = True user.get_geoIP_location(self, e, "", "", "", get_wolfram) return location = urllib.parse.quote(location) socket.setdefaulttimeout(30) url = "http://api.wolframalpha.com/v2/query?appid=%s&format=plaintext&input=%s&location=%s" % ( self.botconfig["APIkeys"]["wolframAPIkey"], urllib.parse.quote( e.input), location) dom = xml.dom.minidom.parse(urllib.request.urlopen(url)) socket.setdefaulttimeout(10) if (dom.getElementsByTagName("queryresult")[0].getAttribute("success") == "false"): try: related = dom.getElementsByTagName( "relatedexample")[0].getAttribute("input") e.input = related return get_wolfram(self, e) except Exception as inst: traceback.print_exc() print("!wolframrelated " + e.input + " : " + str(inst)) result = self.bangcommands["!error"](self, e).output e.output = result return e else: try: query = dom.getElementsByTagName("plaintext")[0].childNodes[0].data try: result = dom.getElementsByTagName( "plaintext")[1].childNodes[0].data except: result = self.bangcommands["!error"](self, e).output output = query.replace("\n", " || ") + " :: " + result.replace( "\n", " || ") unicodes = re.findall("\\\\:[0-9a-zA-Z]{4}", output) if unicodes: newchars = [] for ch in unicodes: ch = ch.encode().replace(b"\\:", b"\\u").decode("unicode-escape") newchars.append(ch) output = re.sub("\\\\:[0-9a-zA-Z]{4}", "{}", output) output = output.format(*newchars) e.output = output return e except Exception as inst: traceback.print_exc() print("!wolfram " + e.input + " : " + str(inst)) result = self.bangcommands["!error"](self, e).output e.output = result return e