X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=cached%2Fweather_forecast.py;h=a413d9f424b3c76d0839a44695310516dee2cdf4;hb=592f27f4743c0a66b6634692fea6c3eb8f252814;hp=ce4725d64a6c43699ab702b85d60d1f070366cf5;hpb=d2730e42f1160d45ab6c7780987b16ae83c616f6;p=python_utils.git diff --git a/cached/weather_forecast.py b/cached/weather_forecast.py index ce4725d..a413d9f 100644 --- a/cached/weather_forecast.py +++ b/cached/weather_forecast.py @@ -3,6 +3,7 @@ from dataclasses import dataclass import datetime import logging +import os import urllib.request import astral # type: ignore @@ -26,7 +27,7 @@ cfg = config.add_commandline_args( cfg.add_argument( '--weather_forecast_cachefile', type=str, - default='/home/scott/.weather_forecast_cache', + default=f'{os.environ["HOME"]}/.weather_forecast_cache', metavar='FILENAME', help='File in which to cache weather data' ) @@ -58,15 +59,23 @@ class CachedDetailedWeatherForecast(object): self.forecasts = {} # Ask the raspberry pi about the outside temperature. - www = urllib.request.urlopen( - "http://10.0.0.75/~pi/outside_temp" - ) - current_temp = www.read().decode("utf-8") - current_temp = float(current_temp) - current_temp *= (9/5) - current_temp += 32.0 - current_temp = round(current_temp) - www.close() + www = None + try: + www = urllib.request.urlopen( + "http://10.0.0.75/~pi/outside_temp", + timeout=2, + ) + current_temp = www.read().decode("utf-8") + current_temp = float(current_temp) + current_temp *= (9/5) + current_temp += 32.0 + current_temp = round(current_temp) + except Exception: + logger.warning('Timed out reading 10.0.0.75/~pi/outside_temp?!') + current_temp = None + finally: + if www is not None: + www.close() # Get a weather forecast for Bellevue. www = urllib.request.urlopen( @@ -79,6 +88,7 @@ class CachedDetailedWeatherForecast(object): forecast = soup.find(id='detailed-forecast-body') parser = dp.DateParser() + said_temp = False last_dt = now dt = now for (day, txt) in zip( @@ -100,8 +110,10 @@ class CachedDetailedWeatherForecast(object): sunrise = s['sunrise'] sunset = s['sunset'] - if dt.date == now.date: - blurb = f'{day.get_text()}: The current outside tempterature is {current_temp}. ' + txt.get_text() + if dt.date() == now.date() and not said_temp and current_temp is not None: + blurb = f'{day.get_text()}: The current outside tempterature is {current_temp}. ' + blurb += txt.get_text() + said_temp = True else: blurb = f'{day.get_text()}: {txt.get_text()}' blurb = text_utils.wrap_string(blurb, 80)