Files
Mark-LIV/actions/weather_report.py
T

51 lines
1.4 KiB
Python
Raw Normal View History

2026-03-02 04:30:10 +03:00
import webbrowser
from urllib.parse import quote_plus
def weather_action(
parameters: dict,
player=None,
2026-04-19 15:22:49 +03:00
session_memory=None,
) -> str:
city = parameters.get("city")
when = parameters.get("time", "today")
if not city or not isinstance(city, str) or not city.strip():
2026-03-02 04:30:10 +03:00
msg = "Sir, the city is missing for the weather report."
2026-04-19 15:22:49 +03:00
_log(msg, player)
2026-03-02 04:30:10 +03:00
return msg
city = city.strip()
2026-04-19 15:22:49 +03:00
when = (when or "today").strip()
2026-03-02 04:30:10 +03:00
2026-04-19 15:22:49 +03:00
search_query = f"weather in {city} {when}"
url = f"https://www.google.com/search?q={quote_plus(search_query)}"
2026-03-02 04:30:10 +03:00
try:
2026-04-19 15:22:49 +03:00
opened = webbrowser.open(url)
if not opened:
raise RuntimeError("webbrowser.open returned False")
except Exception as e:
msg = f"Sir, I couldn't open the browser for the weather report: {e}"
_log(msg, player)
2026-03-02 04:30:10 +03:00
return msg
2026-04-19 15:22:49 +03:00
msg = f"Showing the weather for {city}, {when}, sir."
_log(msg, player)
2026-03-02 04:30:10 +03:00
if session_memory:
try:
2026-04-19 15:22:49 +03:00
session_memory.set_last_search(query=search_query, response=msg)
2026-03-02 04:30:10 +03:00
except Exception:
2026-04-19 15:22:49 +03:00
pass
2026-03-02 04:30:10 +03:00
return msg
2026-04-19 15:22:49 +03:00
def _log(message: str, player=None) -> None:
print(f"[Weather] {message}")
2026-03-02 04:30:10 +03:00
if player:
try:
player.write_log(f"JARVIS: {message}")
except Exception:
pass