Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(blocks): Adds node to get weather from openweathermap (#7382)

Bentlye382dcf823a5
diff --git a/rnd/autogpt_server/autogpt_server/blocks/search.py b/rnd/autogpt_server/autogpt_server/blocks/search.py
index 951320ee42..afe541985e 100644
--- a/rnd/autogpt_server/autogpt_server/blocks/search.py
+++ b/rnd/autogpt_server/autogpt_server/blocks/search.py
@@ -4,7 +4,7 @@ from urllib.parse import quote
import requests
from autogpt_server.data.block import Block, BlockOutput, BlockSchema
-
+from autogpt_server.data.model import BlockSecret, SecretField
class GetRequest:
@classmethod
@@ -122,3 +122,58 @@ class WebScraperBlock(Block, GetRequest):
except requests.RequestException as e:
yield "error", f"Request to Jina-ai Reader failed: {e}"
+
+
+class GetOpenWeatherMapWeather(Block, GetRequest):
+ class Input(BlockSchema):
+ location: str
+ api_key: BlockSecret = SecretField(key="openweathermap_api_key")
+ use_celsius: bool
+
+ class Output(BlockSchema):
+ temperature: str
+ humidity: str
+ condition: str
+ error: str
+
+ def __init__(self):
+ super().__init__(
+ id="f7a8b2c3-6d4e-5f8b-9e7f-6d4e5f8b9e7f",
+ input_schema=GetOpenWeatherMapWeather.Input,
+ output_schema=GetOpenWeatherMapWeather.Output,
+ test_input={"location": "New York", "api_key": "YOUR_API_KEY", "use_celsius": True},
+ test_output=[
+ ("temperature", "21.66"),
+ ("humidity", "32"),
+ ("condition", "overcast clouds")
+ ],
+ test_mock={"get_request": lambda url, json: {
+ "main": {"temp": 21.66, "humidity": 32},
+ "weather": [{"description": "overcast clouds"}]
+ }},
+ )
+
+ def run(self, input_data: Input) -> BlockOutput:
+ try:
+ units = "metric" if input_data.use_celsius else "imperial"
+ api_key = input_data.api_key.get_secret_value()
+ location = input_data.location
+ url = f"http://api.openweathermap.org/data/2.5/weather?q={quote(location)}&appid={api_key}&units={units}"
+ weather_data = self.get_request(url, json=True)
+
+ if 'main' in weather_data and 'weather' in weather_data:
+ yield "temperature", str(weather_data['main']['temp'])
+ yield "humidity", str(weather_data['main']['humidity'])
+ yield "condition", weather_data['weather'][0]['description']
+ else:
+ yield "error", f"Expected keys not found in response: {weather_data}"
+
+ except requests.exceptions.HTTPError as http_err:
+ if http_err.response.status_code == 403:
+ yield "error", f"Request to weather API failed: 403 Forbidden. Check your API key and permissions."
+ else:
+ yield "error", f"HTTP error occurred: {http_err}"
+ except requests.RequestException as e:
+ yield "error", f"Request to weather API failed: {e}"
+ except KeyError as e:
+ yield "error", f"Error processing weather data: {e}"
\ No newline at end of file
diff --git a/rnd/autogpt_server/autogpt_server/util/settings.py b/rnd/autogpt_server/autogpt_server/util/settings.py
index 7e255a44ba..c3adb08058 100644
--- a/rnd/autogpt_server/autogpt_server/util/settings.py
+++ b/rnd/autogpt_server/autogpt_server/util/settings.py
@@ -80,6 +80,8 @@ class Secrets(UpdateTrackingModel["Secrets"], BaseSettings):
reddit_username: str = Field(default="", description="Reddit username")
reddit_password: str = Field(default="", description="Reddit password")
+ openweathermap_api_key: str = Field(default="", description="OpenWeatherMap API key")
+
# Add more secret fields as needed
model_config = SettingsConfigDict(