-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_api_service.py
More file actions
84 lines (67 loc) · 2.41 KB
/
Copy pathweather_api_service.py
File metadata and controls
84 lines (67 loc) · 2.41 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from coordinates import Coordinates
import requests
from config import Config
from typing import NamedTuple
from datetime import datetime
from enum import Enum
from exeptions import ApiServiceError
from typing import Literal
from coordinates import get_coordinates
Celsius = int
class WeatherType(Enum):
THUNDERSTORM = 'THUNDERSTORM'
DRIZZLE = 'DRIZZLE'
RAIN = 'RAIN'
SNOW = 'SNOW'
CLEAR = 'CLEAR'
FOG = 'FOG'
CLOUDS = 'CLOUDS'
class Weather(NamedTuple):
temperature: Celsius
weather_type: WeatherType
sunrise: datetime
sunset: datetime
city: str
def _get_openweather_response(latitude: float, longitude: float) -> str:
try:
url = Config.OPENWEATHER_URL.format(latitude=latitude, longitude=longitude)
return requests.get(url).json()
except:
raise ApiServiceError
def _parse_openweather_response(open_weather: dict) -> Weather:
weather_type = _parse_weather_type(open_weather)
temperature = open_weather["main"]["temp"]
sunrise = _parse_sun_time(open_weather, 'sunrise')
sunset = _parse_sun_time(open_weather, 'sunset')
city = open_weather['name']
return Weather(temperature=temperature,
weather_type=weather_type,
sunrise=sunrise,
sunset=sunset,
city=city)
def _parse_weather_type(openweather_dict: dict) -> WeatherType:
try:
weather_type_id = str(openweather_dict["weather"][0]["id"])
except (IndexError, KeyError):
raise ApiServiceError
weather_types = {
'1': WeatherType.THUNDERSTORM,
'3': WeatherType.DRIZZLE,
'5': WeatherType.RAIN,
'7': WeatherType.SNOW,
'800': WeatherType.CLEAR,
'80': WeatherType.CLOUDS
}
for _id, _weather_type in weather_types.items():
if weather_type_id.startswith(_id):
return _weather_type
raise ApiServiceError
def _parse_sun_time(openweather_dict: dict, time: Literal["sunrise"] | Literal["sunset"]) -> datetime:
return datetime.fromtimestamp(openweather_dict["sys"][time])
def get_weather(coordinates: Coordinates) -> Weather:
"""Request weather in OpenWeather API and return result"""
response = dict(_get_openweather_response(coordinates.latitude, coordinates.longitude))
weather = _parse_openweather_response(response)
return weather
if __name__ == '__main__':
print(get_weather(get_coordinates()))