Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
liangleslie committed Jan 27, 2024
1 parent 0de79dc commit 811e90f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions custom_components/nea_sg_weather/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions custom_components/nea_sg_weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 12 additions & 6 deletions custom_components/nea_sg_weather/weathersg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 811e90f

Please sign in to comment.