From 811e90f66b7fc4dd02778e44ad1ef4e82156304a Mon Sep 17 00:00:00 2001 From: Leslie Liang Date: Sat, 27 Jan 2024 01:39:45 +0000 Subject: [PATCH] minor fixes --- custom_components/nea_sg_weather/const.py | 1 + custom_components/nea_sg_weather/weather.py | 4 ++-- custom_components/nea_sg_weather/weathersg.py | 18 ++++++++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/custom_components/nea_sg_weather/const.py b/custom_components/nea_sg_weather/const.py index 779c3566e5..444698fac6 100644 --- a/custom_components/nea_sg_weather/const.py +++ b/custom_components/nea_sg_weather/const.py @@ -27,6 +27,7 @@ DEFAULT_TIMEOUT = 10 HEADERS = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36", + "referer": "https://www.nea.gov.sg/", } RAIN_MAP_HEADERS = { "authority": "www.weather.gov.sg", diff --git a/custom_components/nea_sg_weather/weather.py b/custom_components/nea_sg_weather/weather.py index 2d3491f8fe..0a48ad3647 100644 --- a/custom_components/nea_sg_weather/weather.py +++ b/custom_components/nea_sg_weather/weather.py @@ -151,11 +151,11 @@ async def async_forecast_daily(self) -> list[Forecast] | None: """Return the daily forecast in native units. Only implement this method if `WeatherEntityFeature.FORECAST_DAILY` is set """ - return self.coordinator.data.forecast24hr.forecast + return self.coordinator.data.forecast4day.forecast async def async_forecast_hourly(self) -> list[Forecast] | None: """Return the hourly forecast in native units. We do not have hourly forecast data so 2 hourly will do Only implement this method if `WeatherEntityFeature.FORECAST_HOURLY` is set """ - return self.coordinator.data.forecast2hr.forecast + return self.coordinator.data.forecast2hr.area_forecast diff --git a/custom_components/nea_sg_weather/weathersg.py b/custom_components/nea_sg_weather/weathersg.py index f5f7dc3b6d..accb5635cb 100644 --- a/custom_components/nea_sg_weather/weathersg.py +++ b/custom_components/nea_sg_weather/weathersg.py @@ -99,13 +99,18 @@ "WS": "Windy, Showers", } +_latest_timestamp = str(int(str(round(datetime.now().timestamp()))[:-2])//3*3)+"00" # API endpoint only accepts time rounded to nearest 5min +nea_headers = { + "referer": "https://www.nea.gov.sg/" +} + GET_ENDPOINTS = { "4day": "https://www.nea.gov.sg/api/Weather4DayOutlook/GetData/" - + str(round(datetime.now().timestamp())), + + _latest_timestamp, "24hr": "https://www.nea.gov.sg/api/WeatherForecast/forecast24hrnowcast2hrs/" - + str(round(datetime.now().timestamp())), + + _latest_timestamp, "current": "https://www.nea.gov.sg/api/Weather24hrs/GetData/" - + str(round(datetime.now().timestamp())), + + _latest_timestamp, "temperature": "http://www.weather.gov.sg/weather-currentobservations-temperature/", "humidity": "http://www.weather.gov.sg/weather-currentobservations-relative-humidity/", "wind": "http://www.weather.gov.sg/weather-currentobservations-wind/", @@ -167,11 +172,12 @@ def __init__(self) -> None: self.raw_resp = {} for k, v in GET_ENDPOINTS.items(): self.raw_resp[k] = {} - self.raw_resp[k]["raw"] = requests.get(GET_ENDPOINTS[k]) - try: # process NEA jsons + if GET_ENDPOINTS[k][:23] == "https://www.nea.gov.sg/" : + self.raw_resp[k]["raw"] = requests.get(GET_ENDPOINTS[k],headers=nea_headers) self.raw_resp[k]["processed"] = self.raw_resp[k]["raw"].json() # print(k + ": json stored") - except requests.exceptions.JSONDecodeError: # scrape data from weather.sg + else: # scrape data from weather.sg + self.raw_resp[k]["raw"] = requests.get(GET_ENDPOINTS[k]) self.raw_resp[k]["processed"] = {} soup = BeautifulSoup(self.raw_resp[k]["raw"].content, "html.parser") self.raw_resp[k]["obs_datetime"] = soup.find(class_="date-obs").text