Skip to content

Commit

Permalink
More robust parsing of forecast
Browse files Browse the repository at this point in the history
  • Loading branch information
pguyot committed Oct 17, 2019
1 parent 014a658 commit b6d3a57
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions meteofrance/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,17 @@ def _format_data(self):
try:
forecast = {}
forecast["date"] = daydata.find("a").string
forecast["weather"] = daydata.find("dd").string.strip()
min_temp = re.sub(r"[^0-9\-]","",daydata.find(class_="min-temp").string)
if min_temp != '-':
forecast["min_temp"] = int(min_temp)
max_temp = re.sub(r"[^0-9\-]","",daydata.find(class_="max-temp").string)
if max_temp != '-':
forecast["max_temp"] = int(max_temp)
forecast["weather_class"] = daydata.find("dd").attrs['class'][1]
self._data["forecast"][day] = forecast
weather = daydata.find("dd").string
if weather:
forecast["weather"] = weather.strip()
min_temp = re.sub(r"[^0-9\-]","",daydata.find(class_="min-temp").string)
if min_temp != '-':
forecast["min_temp"] = int(min_temp)
max_temp = re.sub(r"[^0-9\-]","",daydata.find(class_="max-temp").string)
if max_temp != '-':
forecast["max_temp"] = int(max_temp)
forecast["weather_class"] = daydata.find("dd").attrs['class'][1]
self._data["forecast"][day] = forecast
day = day + 1
except:
raise
Expand Down
4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_pointe_a_pitre(self):
self.assertNotIn('next_rain_datetime', data)
self.assertNotIn('rain_forecast_text', data)
self.assertNotIn('rain_forecast', data)
self.assertEqual(len(data['forecast']), 10)
self.assertGreaterEqual(len(data['forecast']), 9)

# Same with world data
def test_pointe_a_pitre(self):
Expand All @@ -120,7 +120,7 @@ def test_pointe_a_pitre(self):
self.assertNotIn('next_rain', data)
self.assertNotIn('rain_forecast_text', data)
self.assertNotIn('rain_forecast', data)
self.assertEqual(len(data['forecast']), 10)
self.assertGreaterEqual(len(data['forecast']), 9)

class TestRainForecast(unittest.TestCase):
def test_rain_forecast_is_updated(self):
Expand Down

0 comments on commit b6d3a57

Please sign in to comment.