adding the skill:
chobit.addSkill(DiRainAlert("pripyat"))
skill code:
chobit.addSkill(DiRainAlert("pripyat"))
skill code:
Code:
class DiRainAlert(DiSkillV2):
def __init__(self, city: str):
super().__init__()
self.city: str = city
self.apikey: str = "" # your https://openweathermap.org/api api key. place it in a
# weather_apikey.txt in the python project's source dir
with open('weather_apikey.txt', 'r') as f:
self.apikey = f.read()
def input(self, ear, skin, eye):
if ear == "rain alerts":
self.setSimpleAlg(self.get_weather(self.apikey))
def get_weather(self, api_key) -> str:
base_url = "https://api.openweathermap.org/data/2.5/weather"
params = {
"q": self.city,
"appid": api_key,
"units": "metric", # You can change to "imperial" for Fahrenheit
}
response = requests.get(base_url, params=params)
data = response.json()
if "weather" in data:
weather_description = data["weather"][0]["description"]
if "rain" in weather_description.lower():
return f"It's going to rain in {self.city}! "
else:
return f"No rain expected in {self.city}. Enjoy the weather! "
else:
return "Unable to fetch weather data."