From 1fc8af09f8f5e7b9b87c701ee7ea4cd84d6c627c Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sun, 25 Apr 2021 16:34:01 -0500 Subject: [PATCH] use nearest area to produce return location --- weather/lib.py | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/weather/lib.py b/weather/lib.py index 9b858e6..267cd0d 100644 --- a/weather/lib.py +++ b/weather/lib.py @@ -1,26 +1,27 @@ # coding: utf-8 """Get results of weather queries.""" import logging -import requests from urllib.parse import quote +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}'") + logger.info("about to query wttr.in with '%s'", query) response = requests.get(f'http://wttr.in/{quote(query)}?format=j1') response.raise_for_status() weather_info = response.json() - logger.debug(f"results: {weather_info}") + logger.debug("results: %s", weather_info) return weather_info def weather_summary(query): """Create a more consumable version of the weather report.""" - logger.info(f"assembling weather summary for '{query}'") + logger.info("assembling weather summary for '%s'", query) weather_info = query_wttr_in(query) # get some common/nested stuff once now @@ -30,20 +31,46 @@ def weather_summary(query): tomorrow_forecast = weather_info['weather'][1] day_after_tomorrow_forecast = weather_info['weather'][2] - today_notes = [{'code': int(item['weatherCode']), 'desc': item['weatherDesc'][0]['value'] } + today_notes = [{'code': int(item['weatherCode']), 'desc': item['weatherDesc'][0]['value']} for item in today_forecast['hourly']] today_noteworthy = sorted(today_notes, key=lambda i: i['code'], reverse=True)[0]['desc'] - tomorrow_notes = [{'code': int(item['weatherCode']), 'desc': item['weatherDesc'][0]['value'] } + tomorrow_notes = [{'code': int(item['weatherCode']), 'desc': item['weatherDesc'][0]['value']} for item in tomorrow_forecast['hourly']] tomorrow_noteworthy = sorted(tomorrow_notes, key=lambda i: i['code'], reverse=True)[0]['desc'] - day_after_tomorrow_notes = [{'code': int(item['weatherCode']), 'desc': item['weatherDesc'][0]['value'] } + day_after_tomorrow_notes = [{'code': int(item['weatherCode']), 'desc': item['weatherDesc'][0]['value']} for item in day_after_tomorrow_forecast['hourly']] day_after_tomorrow_noteworthy = sorted(day_after_tomorrow_notes, key=lambda i: i['code'], reverse=True)[0]['desc'] + location_str = None + locations = [] + # try to get a smarter location + nearest_areas = weather_info.get('nearest_area', None) + if nearest_areas: + nearest_area = nearest_areas[0] + area_name = nearest_area.get('areaName') + if area_name: + locations.append(area_name[0]['value']) + region = nearest_area.get('region') + if region: + locations.append(region[0]['value']) + country = nearest_area.get('country') + if country: + locations.append(country[0]['value']) + + if locations: + location_str = ', '.join(locations) + else: + latitude = nearest_area.get('latitude') + longitude = nearest_area.get('longitude') + if latitude and longitude: + location_str = f'{latitude},{longitude}' + if not location_str: + location_str = query + summary = { - 'location': query, + 'location': location_str, 'current': { 'description': weather_desc.get('value'), 'temp_C': f"{current.get('temp_C')}°C", @@ -82,5 +109,5 @@ def weather_summary(query): }, } - logger.debug(f"results: {summary}") + logger.debug("results: %s", summary) return summary