summaryrefslogtreecommitdiff
path: root/cached/weather_forecast.py
diff options
context:
space:
mode:
Diffstat (limited to 'cached/weather_forecast.py')
-rw-r--r--cached/weather_forecast.py48
1 files changed, 24 insertions, 24 deletions
diff --git a/cached/weather_forecast.py b/cached/weather_forecast.py
index b343938..58f53c3 100644
--- a/cached/weather_forecast.py
+++ b/cached/weather_forecast.py
@@ -1,60 +1,59 @@
#!/usr/bin/env python3
-from dataclasses import dataclass
import datetime
import logging
import os
-from typing import Any
import urllib.request
+from dataclasses import dataclass
+from typing import Any
import astral # type: ignore
+import pytz
from astral.sun import sun # type: ignore
from bs4 import BeautifulSoup # type: ignore
from overrides import overrides
-import pytz
import argparse_utils
import config
-import datetime_utils
import dateparse.dateparse_utils as dp
+import datetime_utils
import persistent
-import text_utils
import smart_home.thermometers as temps
-
+import text_utils
logger = logging.getLogger(__name__)
cfg = config.add_commandline_args(
f'Cached Weather Forecast ({__file__})',
- 'Arguments controlling detailed weather rendering'
+ 'Arguments controlling detailed weather rendering',
)
cfg.add_argument(
'--weather_forecast_cachefile',
type=str,
default=f'{os.environ["HOME"]}/cache/.weather_forecast_cache',
metavar='FILENAME',
- help='File in which to cache weather data'
+ help='File in which to cache weather data',
)
cfg.add_argument(
'--weather_forecast_stalest_acceptable',
type=argparse_utils.valid_duration,
- default=datetime.timedelta(seconds=7200), # 2 hours
+ default=datetime.timedelta(seconds=7200), # 2 hours
metavar='DURATION',
- help='Maximum acceptable age of cached data. If zero, forces a refetch'
+ help='Maximum acceptable age of cached data. If zero, forces a refetch',
)
@dataclass
class WeatherForecast:
- date: datetime.date # The date
- sunrise: datetime.datetime # Sunrise datetime
- sunset: datetime.datetime # Sunset datetime
- description: str # Textual description of weather
+ date: datetime.date # The date
+ sunrise: datetime.datetime # Sunrise datetime
+ sunset: datetime.datetime # Sunset datetime
+ description: str # Textual description of weather
[email protected]_autoloaded_singleton()
[email protected]_autoloaded_singleton() # type: ignore
class CachedDetailedWeatherForecast(persistent.Persistent):
- def __init__(self, forecasts = None):
+ def __init__(self, forecasts=None):
if forecasts is not None:
self.forecasts = forecasts
return
@@ -82,8 +81,7 @@ class CachedDetailedWeatherForecast(persistent.Persistent):
last_dt = now
dt = now
for (day, txt) in zip(
- forecast.find_all('b'),
- forecast.find_all(class_='col-sm-10 forecast-text')
+ forecast.find_all('b'), forecast.find_all(class_='col-sm-10 forecast-text')
):
last_dt = dt
try:
@@ -112,20 +110,21 @@ class CachedDetailedWeatherForecast(persistent.Persistent):
self.forecasts[dt.date()].description += '\n' + blurb
else:
self.forecasts[dt.date()] = WeatherForecast(
- date = dt,
- sunrise = sunrise,
- sunset = sunset,
- description = blurb,
+ date=dt,
+ sunrise=sunrise,
+ sunset=sunset,
+ description=blurb,
)
@classmethod
@overrides
def load(cls) -> Any:
if persistent.was_file_written_within_n_seconds(
- config.config['weather_forecast_cachefile'],
- config.config['weather_forecast_stalest_acceptable'].total_seconds(),
+ config.config['weather_forecast_cachefile'],
+ config.config['weather_forecast_stalest_acceptable'].total_seconds(),
):
import pickle
+
with open(config.config['weather_forecast_cachefile'], 'rb') as rf:
weather_data = pickle.load(rf)
return cls(weather_data)
@@ -134,6 +133,7 @@ class CachedDetailedWeatherForecast(persistent.Persistent):
@overrides
def save(self) -> bool:
import pickle
+
with open(config.config['weather_forecast_cachefile'], 'wb') as wf:
pickle.dump(
self.forecasts,