Skip to content

Commit aa192e3

Browse files
committed
add user_text_input and weather_forecast
1 parent a05dbba commit aa192e3

File tree

11 files changed

+234
-0
lines changed

11 files changed

+234
-0
lines changed

user_text_input/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.9

user_text_input/README.md

Whitespace-only changes.

user_text_input/pyproject.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[project]
2+
name = "user_text_input"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
authors = [
7+
{ name = "Roberto Gazia", email = "r.gazia@arduino.cc" }
8+
]
9+
requires-python = ">=3.9"
10+
dependencies = []
11+
12+
[build-system]
13+
requires = ["hatchling"]
14+
build-backend = "hatchling.build"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class UserTextInput:
2+
def __init__(self, prompt: str):
3+
self.prompt = prompt
4+
5+
def get(self):
6+
return input(self.prompt)
7+
8+
def produce(self):
9+
try:
10+
return input(self.prompt)
11+
except KeyboardInterrupt:
12+
return StopIteration

user_text_input/src/user_text_input/py.typed

Whitespace-only changes.

weather_forecast/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.9

weather_forecast/README.md

Whitespace-only changes.

weather_forecast/pyproject.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[project]
2+
name = "weather_forecast"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
authors = [
7+
{ name = "Roberto Gazia", email = "r.gazia@arduino.cc" }
8+
]
9+
requires-python = ">=3.9"
10+
dependencies = [
11+
"requests>=2.32.3",
12+
]
13+
14+
[build-system]
15+
requires = ["hatchling"]
16+
build-backend = "hatchling.build"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import requests
2+
3+
city_api_url = "https://geocoding-api.open-meteo.com/v1/search"
4+
forecast_api_url = "https://api.open-meteo.com/v1/forecast"
5+
6+
class WeatherForecast:
7+
def __init__(self):
8+
pass
9+
10+
def get_forecast_by_city(self, city: str) -> any:
11+
try:
12+
response = requests.get(city_api_url, params={"name": city})
13+
except:
14+
raise RuntimeError("Failed to look city up")
15+
16+
data = response.json()
17+
results = data.get("results", [])
18+
if results:
19+
result = results[0]
20+
else:
21+
raise RuntimeError("City not found")
22+
23+
return self.get_forecast_by_coords(result['latitude'], result['longitude'])
24+
25+
def get_forecast_by_coords(self, latitude: str, longitude: str, timezone = "GMT") -> any:
26+
params = {
27+
"latitude": latitude,
28+
"longitude": longitude,
29+
"timezone": timezone,
30+
"current": "temperature_2m,weather_code,precipitation",
31+
"hourly": "temperature_2m,weather_code,precipitation_probability",
32+
"forecast_days": "1",
33+
"format": "json"
34+
}
35+
try:
36+
response = requests.get(forecast_api_url, params=params)
37+
except:
38+
raise RuntimeError("Failed to get weather data")
39+
40+
data = response.json()
41+
if response.status_code == 200:
42+
return data
43+
else:
44+
raise RuntimeError(f"Failed to get weather data: {data.get('reason', 'Unknown error')}")
45+
46+
def execute(self, city, something_else=None):
47+
return self.get_forecast_by_city(city)
48+
49+
# def execute(self, msg: any) -> any:
50+
# if msg["text"]:
51+
# return self.get_forecast_by_city(msg["text"])
52+
# if msg["latitude"] and msg["longitude"]:
53+
# return self.get_forecast_by_coords(msg["latitude"], msg["longitude"])
54+
# raise RuntimeError("incompatible input")
55+
56+
class CityLookupError(Exception):
57+
pass
58+
59+
class WeatherForecastLookupError(Exception):
60+
pass

weather_forecast/src/weather_forecast/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)