# coding: utf-8 """Get results of weather queries.""" import logging import requests logger = logging.getLogger(__name__) def query_wttr_in(query): """Hit the wttr.in JSON API with the provided query.""" logger.info(f"about to query wttr.in with '{query}") response = requests.get('http://wttr.in/{query}?format=j1') response.raise_for_status() weather_info = response.json() logger.debug(f"results: {weather_info}") return weather_info def weather_summary(query): """Create a more consumable version of the weather report.""" weather_info = query_wttr_in(query) # get some common/nested stuff once now current = weather_info['current_condition'][0] weather_desc = current['weatherDesc'][0] if 'weatherDesc' in current else {} today_forecast = weather_info['weather'][0] tomorrow_forecast = weather_info['weather'][1] day_after_tomorrow_forecast = weather_info['weather'][2] summary = { 'location': query, 'current': { 'description': weather_desc.get('value'), 'temp_C': f"{current.get('temp_C')}°C", 'temp_F': f"{current.get('temp_F')}°F", 'feels_like_temp_C': f"{current.get('FeelsLikeC')}°C", 'feels_like_temp_F': f"{current.get('FeelsLikeF')}°F", 'cloud_cover': f"{current.get('cloudcover')}%", 'humidity': f"{current.get('humidity')}%", 'precipitation': f"{current.get('precipMM')} mm", 'visibility': f"{current.get('visibility')} mi", 'wind_speed': f"{current.get('windspeedMiles')} MPH", 'wind_direction': f"{current.get('winddir16Point')}", 'pressure': f"{current.get('pressure')} mb", 'uv_index': f"{current.get('uvIndex')}", }, } return summary