def weather(text, reply): """weather <location> -- Gets weather data for <location>.""" if not wunder_key: return "This command requires a Weather Underground API key." if not dev_key: return "This command requires a Google Developers Console API key." # use find_location to get location data from the user input try: location_data = find_location(text) except APIError as e: return e formatted_location = "{lat},{lng}".format(**location_data) url = wunder_api.format(wunder_key, formatted_location) response = requests.get(url).json() if response['response'].get('error'): return "{}".format(response['response']['error']['description']) forecast_today = response["forecast"]["simpleforecast"]["forecastday"][0] forecast_tomorrow = response["forecast"]["simpleforecast"]["forecastday"][1] # put all the stuff we want to use in a dictionary for easy formatting of the output weather_data = { "place": response['current_observation']['display_location']['full'], "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'], "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'] } # Get the more accurate URL if available, if not, get the generic one. if "?query=," in response["current_observation"]['ob_url']: weather_data['url'] = web.shorten(response["current_observation"]['forecast_url']) else: weather_data['url'] = web.shorten(response["current_observation"]['ob_url']) reply("{place} - \x02Current:\x02 {conditions}, {temp_f}F/{temp_c}C, {humidity}, " "Wind: {wind_kph}KPH/{wind_mph}MPH {wind_direction}, \x02Today:\x02 {today_conditions}, " "High: {today_high_f}F/{today_high_c}C, Low: {today_low_f}F/{today_low_c}C. " "\x02Tomorrow:\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))
def shorten(text): """<url> [custom] - shortens a url with [custom] as an optional custom shortlink""" args = text.split() url = args[0] custom = args[1] if len(args) > 1 else None try: return web.shorten(url, custom=custom) except web.ServiceError as e: return e.message
def gitio(text): """<url> [custom] - shortens a github URL <url> using git.io with [custom] as an optional custom shortlink, or unshortens <url> if already short""" args = text.split() url = args[0] custom = args[1] if len(args) > 1 else None try: if 'git.io' in url: return web.expand(url, 'git.io') else: return web.shorten(url, custom, 'git.io') except web.ServiceError as e: return e.message
def isgd(text): """<url> [custom] - shortens a url using is.gd with [custom] as an optional custom shortlink, or unshortens <url> if already short""" args = text.split() url = args[0] custom = args[1] if len(args) > 1 else None try: if 'is.gd' in url: return web.expand(url, 'is.gd') else: return web.shorten(url, custom, 'is.gd') except web.ServiceError as e: return e.message
def books(text): """books <query> -- Searches Google Books for <query>.""" if not dev_key: return "This command requires a Google Developers Console API key." json = requests.get(book_search_api, params={"q": text, "key": dev_key, "country": "US"}).json() if json.get('error'): if json['error']['code'] == 403: print(json['error']['message']) return "The Books API is off in the Google Developers Console (or check the console)." else: return 'Error performing search.' if json['totalItems'] == 0: return 'No results found.' book = json['items'][0]['volumeInfo'] title = book['title'] try: author = book['authors'][0] except KeyError: try: author = book['publisher'] except KeyError: author = "Unknown Author" try: description = formatting.truncate_str(book['description'], 130) except KeyError: description = "No description available." try: year = book['publishedDate'][:4] except KeyError: year = "No Year" try: page_count = book['pageCount'] pages = ' - \x02{:,}\x02 page{}'.format(page_count, "s"[page_count == 1:]) except KeyError: pages = '' link = web.shorten(book['infoLink'], service="goo.gl", key=dev_key) return "\x02{}\x02 by \x02{}\x02 ({}){} - {} - {}".format(title, author, year, pages, description, link)