battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.

battle programmers allianceLog in

the LivinGrimoire Artificial General Intelligence software design pattern forum

descriptionDiRainAlert get the weather using one line of code EmptyDiRainAlert get the weather using one line of code

more_horiz
adding the skill:
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."


cloudy

descriptionDiRainAlert get the weather using one line of code EmptyRe: DiRainAlert get the weather using one line of code

more_horiz
demo:
https://rumble.com/v4esoh0-livingrimoire-python-rain-alert-skill.html

descriptionDiRainAlert get the weather using one line of code EmptyRe: DiRainAlert get the weather using one line of code

more_horiz
the following code is a skill update. added are the abilities to get weather conditions and temperature.
you don't need to understand any of the code, just how to add the skill to the chobit object with one code line:
app.brain.logicChobit.addSkill(DiRainAlert("city"))

https://rumble.com/v4gk0fq-livingrimoire-updated-weather-skill.html
sunny

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 len(ear) == 0:
            return
        match ear:
            case "rain alerts":
                self.setSimpleAlg(self.getRainAlerts(self.apikey))
            case "get weather":
                self.setSimpleAlg(self.get_weather(self.apikey))
            case "temp":
                self.setSimpleAlg(self.get_temperature_in_celsius(self.apikey))

    def getRainAlerts(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."

    def get_weather(self, api_key):
        base_url = "http://api.openweathermap.org/data/2.5/weather?"

        complete_url = base_url + f"appid={api_key}&q={self.city}"
        response = requests.get(complete_url)
        weather_data = response.json()

        if weather_data["cod"] != "404":
            main_info = weather_data["main"]
            current_temperature_kelvin = main_info["temp"]
            current_temperature_celsius = current_temperature_kelvin - 273.15
            current_pressure = main_info["pressure"]
            current_humidity = main_info["humidity"]

            weather_description = weather_data["weather"][0]["description"]

            result_string = f"Temperature: {current_temperature_celsius:.2f}°C\n"
            result_string += f"Atmospheric pressure (in hPa): {current_pressure}\n"
            result_string += f"Humidity (in percentage): {current_humidity}\n"
            result_string += f"Description: {weather_description}"
            return result_string
        else:
            return "City Not Found"

    def get_temperature_in_celsius(self, api_key):
        base_url = "http://api.openweathermap.org/data/2.5/weather?"

        complete_url = base_url + f"appid={api_key}&q={self.city}"
        response = requests.get(complete_url)
        weather_data = response.json()

        if weather_data["cod"] != "404":
            main_info = weather_data["main"]
            current_temperature_kelvin = main_info["temp"]
            current_temperature_celsius = current_temperature_kelvin - 273.15
            return f"{current_temperature_celsius:.2f}°C"
        else:
            return "City Not Found"
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply