use nearest area to produce return location
This commit is contained in:
parent
53c874dc21
commit
1fc8af09f8
@ -1,26 +1,27 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
"""Get results of weather queries."""
|
"""Get results of weather queries."""
|
||||||
import logging
|
import logging
|
||||||
import requests
|
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def query_wttr_in(query):
|
def query_wttr_in(query):
|
||||||
"""Hit the wttr.in JSON API with the provided 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 = requests.get(f'http://wttr.in/{quote(query)}?format=j1')
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
weather_info = response.json()
|
weather_info = response.json()
|
||||||
logger.debug(f"results: {weather_info}")
|
logger.debug("results: %s", weather_info)
|
||||||
return weather_info
|
return weather_info
|
||||||
|
|
||||||
|
|
||||||
def weather_summary(query):
|
def weather_summary(query):
|
||||||
"""Create a more consumable version of the weather report."""
|
"""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)
|
weather_info = query_wttr_in(query)
|
||||||
|
|
||||||
# get some common/nested stuff once now
|
# get some common/nested stuff once now
|
||||||
@ -30,20 +31,46 @@ def weather_summary(query):
|
|||||||
tomorrow_forecast = weather_info['weather'][1]
|
tomorrow_forecast = weather_info['weather'][1]
|
||||||
day_after_tomorrow_forecast = weather_info['weather'][2]
|
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']]
|
for item in today_forecast['hourly']]
|
||||||
today_noteworthy = sorted(today_notes, key=lambda i: i['code'], reverse=True)[0]['desc']
|
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']]
|
for item in tomorrow_forecast['hourly']]
|
||||||
tomorrow_noteworthy = sorted(tomorrow_notes, key=lambda i: i['code'], reverse=True)[0]['desc']
|
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']]
|
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']
|
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 = {
|
summary = {
|
||||||
'location': query,
|
'location': location_str,
|
||||||
'current': {
|
'current': {
|
||||||
'description': weather_desc.get('value'),
|
'description': weather_desc.get('value'),
|
||||||
'temp_C': f"{current.get('temp_C')}°C",
|
'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
|
return summary
|
||||||
|
Loading…
Reference in New Issue
Block a user