-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
106 lines (80 loc) · 4.9 KB
/
weather.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import openmeteo_requests
import requests
import requests_cache
import pandas as pd
from retry_requests import retry
from urllib.request import urlopen
import json
class Weather:
def get_weather(self, city):
# Setup the Open-Meteo API client with cache and retry on error
cache_session = requests_cache.CachedSession('.cache', expire_after = 3600)
retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)
openmeteo = openmeteo_requests.Client(session = retry_session)
# Take city_name sent from assistente.py as an arg and plug it into the geocoding API.
# Use geocoding API to grab the coordinates of a place so it can be plugged into the weather forecast params.
geocode_arg = "+".join(city)
url = f"https://geocoding-api.open-meteo.com/v1/search?name={geocode_arg}&count=1&language=en&format=json"
location_response = urlopen(url)
location_data = json.loads(location_response.read())
latitude = location_data['results'][0]['latitude']
longitude= location_data['results'][0]['longitude']
coordinates = [latitude, longitude]
# Make sure all required weather variables are listed here
# The order of variables in hourly or daily is important to assign them correctly below
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": coordinates[0],
"longitude": coordinates[1],
"current": ["temperature_2m", "apparent_temperature", "precipitation", "weather_code"],
"temperature_unit": "fahrenheit",
"wind_speed_unit": "mph",
"precipitation_unit": "inch",
"timeformat": "unixtime",
"timezone": "America/New_York"
}
responses = openmeteo.weather_api(url, params=params)
# Process first location. Add a for-loop for multiple locations or weather models
response = responses[0]
print(f"Coordinates: {response.Latitude()}°N {response.Longitude()}°E")
print(f"Elevation: {response.Elevation()} m asl")
# Current values. The order of variables needs to be the same as requested.
current = response.Current()
current_temperature_2m = current.Variables(0).Value()
current_apparent_temperature = current.Variables(1).Value()
current_precipitation = current.Variables(2).Value()
current_weather_code = current.Variables(3).Value()
current_weather_code = int(current_weather_code)
new_weather_code = self.sort_weather_codes(current_weather_code)
print(f"Current temperature_2m: {round(current_temperature_2m, 2)}")
print(f"Current apparent_temperature: {round(current_apparent_temperature, 2)} fahrenheit.")
print(f"Current precipitation: {round(current_precipitation, 2)} inches.")
print(new_weather_code)
def sort_weather_codes(self, current_weather_code):
# Returns a description of the weather code.
if current_weather_code == 0:
new_weather_code = "Clear"
return new_weather_code
elif current_weather_code in (1, 2, 3):
new_weather_code = "Partly Cloudy"
return new_weather_code
elif current_weather_code in (45, 48):
new_weather_code = "Fog"
return new_weather_code
elif current_weather_code in (51, 53, 55):
new_weather_code = "Light Rain"
return new_weather_code
elif current_weather_code in (61, 63, 65, 66, 67, 80, 81, 82):
new_weather_code = "Rain"
return new_weather_code
elif current_weather_code in (71, 73, 75, 77, 85, 86):
new_weather_code = "Snow"
return new_weather_code
elif current_weather_code == 95:
new_weather_code = "Thunderstorm"
return new_weather_code
elif current_weather_code in (96, 99):
new_weather_code = "Hail"
return new_weather_code
else:
print(f"Unknown weather code {current_weather_code}")