Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weather #78

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading