Repository navigation
Expand file tree
/
Copy pathservices.py
More file actions
48 lines (41 loc) · 1.38 KB
/
Copy pathservices.py
File metadata and controls
48 lines (41 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from client import get_weather_data, geocode
from exceptions import CityNotFoundError, DataParsingError
from model import WeatherRequest, WeatherResponse, DailyWeather
def get_city_coordinates(city: str) -> dict:
data = geocode(city)
try:
result = data["results"][0]
return {
"latitude": result["latitude"],
"longitude": result["longitude"],
}
except (IndexError, KeyError) as e:
raise CityNotFoundError(
{
"error": f"city (${city}) not found"
}
) from e
def get_city_weather(city: str) -> WeatherResponse:
response = get_city_coordinates(city)
coordinates = WeatherRequest(**response).model_dump()
response = get_weather_data(
{
**coordinates,
"daily": "temperature_2m_max,temperature_2m_min",
"current": "relative_humidity_2m",
"timezone": "auto"
}
)
daily = DailyWeather(
time=response["daily"]["time"],
temperature_max=response["daily"]["temperature_2m_max"],
temperature_min=response["daily"]["temperature_2m_min"],
)
return WeatherResponse(
city=city,
time=response["current"]["time"],
humidity=response["current"]["relative_humidity_2m"],
daily=daily,
)
if __name__ == "__main__":
print(get_city_weather("Atareb").model_dump())