From 014a658b73e459b7b1b4b3be44c6d4d6fb5e24bf Mon Sep 17 00:00:00 2001 From: Paul Guyot Date: Sun, 21 Apr 2019 22:30:41 +0200 Subject: [PATCH] Save all available forecast data, including today's --- meteofrance/client.py | 13 +++++++++---- test.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/meteofrance/client.py b/meteofrance/client.py index cc69432..3647ee9 100644 --- a/meteofrance/client.py +++ b/meteofrance/client.py @@ -223,16 +223,21 @@ def _format_data(self): self._data["forecast"] = {} daydatas = soup.find(class_="liste-jours").find_all("li") - for day in range(0, 5): + day = 0 + for daydata in daydatas: try: - daydata = daydatas[day+1] forecast = {} forecast["date"] = daydata.find("a").string forecast["weather"] = daydata.find("dd").string.strip() - forecast["min_temp"] = int(re.sub(r"[^0-9\-]","",daydata.find(class_="min-temp").string)) - forecast["max_temp"] = int(re.sub(r"[^0-9\-]","",daydata.find(class_="max-temp").string)) + 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 diff --git a/test.py b/test.py index 5b232ef..e7433aa 100644 --- a/test.py +++ b/test.py @@ -67,6 +67,7 @@ def test_beynost(self): self.assertNotIn('next_rain_datetime', data) self.assertNotIn('rain_forecast_text', data) self.assertNotIn('rain_forecast', data) + self.assertEqual(len(data['forecast']), 14) # pointe-a-pitre : result from meteo-france is different and it returns less data def test_pointe_a_pitre(self): @@ -93,6 +94,33 @@ 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) + + # Same with world data + def test_pointe_a_pitre(self): + client = meteofranceClient('Tokyo') + client.need_rain_forecast = False + client.update() + data = client.get_data() + self.assertIn('name', data) + self.assertNotIn('dept', data) + self.assertIn('fetched_at', data) + self.assertIn('forecast', data) + self.assertNotIn('freeze_chance', data) + self.assertNotIn('rain_chance', data) + self.assertNotIn('snow_chance', data) + self.assertIn('temperature', data) + self.assertNotIn('thunder_chance', data) + self.assertNotIn('uv', data) + self.assertIn('weather_class', data) + self.assertIn('weather', data) + self.assertIn('wind_bearing', data) + self.assertIn('wind_speed', data) + self.assertNotIn('next_rain_intervals', data) + self.assertNotIn('next_rain', data) + self.assertNotIn('rain_forecast_text', data) + self.assertNotIn('rain_forecast', data) + self.assertEqual(len(data['forecast']), 10) class TestRainForecast(unittest.TestCase): def test_rain_forecast_is_updated(self):