Skip to content

Commit

Permalink
Fix weather
Browse files Browse the repository at this point in the history
Now fetches data directly from the SMHI API
  • Loading branch information
Schanihbg authored and mosbth committed Oct 22, 2024
1 parent 3abe52d commit 7e262d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
32 changes: 23 additions & 9 deletions marvin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,34 @@ def marvinWeather(row):
"""
Check what the weather prognosis looks like.
"""
msg = None
msg = ""
if any(r in row for r in ["väder", "vädret", "prognos", "prognosen", "smhi"]):
url = getString("smhi", "url")
forecast = ""
observation = ""

try:
soup = BeautifulSoup(urlopen(url))
msg = "{}. {}. {}".format(
soup.h1.text,
soup.h4.text,
soup.h4.findNextSibling("p").text
)
station_req = requests.get(getString("smhi", "station_url"), timeout=5)
weather_code:int = int(station_req.json().get("value")[0].get("value"))

weather_codes_req = requests.get(getString("smhi", "weather_codes_url"), timeout=5)
weather_codes_arr: list = weather_codes_req.json().get("entry")

current_weather_req = requests.get(getString("smhi", "current_weather_url"), timeout=5)
current_w_data: list = current_weather_req.json().get("timeSeries")[0].get("parameters")

for curr_w in current_w_data:
if curr_w.get("name") == "t":
forecast = curr_w.get("values")[0]

for code in weather_codes_arr:
if code.get("key") == weather_code:
observation = code.get("value")

msg = f"Karlskrona just nu: {forecast} °C. {observation}."

except Exception as e:
LOG.error("Failed to get weather: %s", e)
msg = getString("smhi", "failed")
msg: str = getString("smhi", "failed")

return msg

Expand Down
4 changes: 3 additions & 1 deletion marvin_strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@
},

"smhi": {
"url": "http://www.smhi.se/weatherSMHI2/landvader/prognos15_2.htm",
"station_url": "https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/13/station/65090/period/latest-hour/data.json",
"current_weather_url": "https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/15.5890/lat/56.1500/data.json",
"weather_codes_url": "https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/13/codes.json",
"failed": "Något har hänt med SMHI, de svarar inte."
},

Expand Down

0 comments on commit 7e262d4

Please sign in to comment.