Skip to content

Commit

Permalink
Better distinction of report and measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Jul 2, 2023
1 parent b659127 commit eaab80f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class Weather:
get_timeframe_condition(datetime, timeframe: hours after datetime as int, optional bool shouldUpdate) # Result is an approximate "feeled" condition at this time frame

get_weather_report(optional bool shouldUpdate) # Returns the weather report for the geographical region of the station as HTML

update(self, optional bool force_hourly (default: False), optional bool with_forecast (default: True), optional bool with_measurements (default: False), optional bool with_report (default: False)) # Updates the weather data
```

#### Advanced Usage
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="simple_dwd_weatherforecast",
version="2.0.6",
version="2.0.7",
author="Max Fermor",
description="A simple tool to retrieve a weather forecast from DWD OpenData",
long_description=long_description,
Expand Down
26 changes: 16 additions & 10 deletions simple_dwd_weatherforecast/dwdforecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def is_valid_timeframe(_, timeframe: int) -> bool:
return False
return 24 % timeframe == 0

def has_report(self, station_id):
def has_measurement(self, station_id):
if load_station_id(station_id):
return stations[station_id]["report_available"] == 1
return False
Expand Down Expand Up @@ -334,11 +334,11 @@ def get_condition(self, weather_data):
return str(condition_text)

def get_reported_weather(self, weatherDataType: WeatherDataType, shouldUpdate=True):
if not self.has_report(self.station_id):
if not self.has_measurement(self.station_id):
print("no report for this station available")
return None
if shouldUpdate:
self.update()
if shouldUpdate or self.report_data is None:
self.update(with_measurements=True)
if weatherDataType == WeatherDataType.CONDITION:
return self.actual_report_codes[
self.report_data[WeatherDataType.CONDITION.value[0]]
Expand Down Expand Up @@ -543,14 +543,20 @@ def strip_to_hour(_, timestamp: datetime):
def strip_to_day(_, timestamp: datetime):
return datetime(timestamp.year, timestamp.month, timestamp.day)

def update(self, force_hourly=False):
if self.has_report(self.station_id):
def update(
self,
force_hourly=False,
with_forecast=True,
with_measurements=False,
with_report=False,
):
if with_measurements and self.has_measurement(self.station_id):
self.download_latest_report()

if self.region is not None:
if with_report and self.region is not None:
self.download_weather_report(self.region_codes[self.region])

if (
if with_forecast and (
(self.issue_time is None)
or (datetime.now(timezone.utc) - self.issue_time >= timedelta(hours=6))
or force_hourly
Expand Down Expand Up @@ -763,8 +769,8 @@ def parse_csv_row(self, row: dict):
}

def get_weather_report(self, shouldUpdate=False):
if shouldUpdate:
self.update()
if shouldUpdate or self.weather_report is None:
self.update(with_report=True)
return self.weather_report

def download_weather_report(self, region_code):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_reported_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def test_download(self):

def test_has_report_true(self):
dwdweather = dwdforecast.Weather("01008")
self.assertTrue(dwdweather.has_report("01008"))
self.assertTrue(dwdweather.has_measurement("01008"))

def test_has_report_false(self):
dwdweather = dwdforecast.Weather("X472")
self.assertFalse(dwdweather.has_report("X472"))
self.assertFalse(dwdweather.has_measurement("X472"))

def test_has_report_invalid_station(self):
dwdweather = dwdforecast.Weather("01008")
self.assertFalse(dwdweather.has_report("01"))
self.assertFalse(dwdweather.has_measurement("01"))

0 comments on commit eaab80f

Please sign in to comment.