-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_weather.py
152 lines (127 loc) · 4.11 KB
/
get_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import requests
import json
from datetime import datetime
import pytz
API_KEY = "b4c4ada6a2c91b2a126721df62de169d"
URL = "https://api.openweathermap.org/data/2.5/"
class GetWeather:
def __init__(self, city):
self.city = city
self.geo_json = self.get_geocode()
self.json = self.get_json()
if self.json:
self.time_zone = self.get_time_zone()
self.weather = self.get_weather()
self.icon_name = self.get_weather_icon()
self.date_time = self.get_date_time()
self.location = self.get_location()
self.day_forcast = self.get_day_forcast()
self.forcast = self.get_forcast()
def get_geocode(self):
"""
'current weather data' api call for
geocoding
"""
try:
url = URL + f"weather?q={self.city}&appid={API_KEY}"
data = requests.get(url).text
return json.loads(data)
except:
print("error")
return
def get_json(self):
"""
'one call api' call for weather details
"""
if not self.geo_json:
return None
try:
lon , lat = self.geo_json["coord"]["lon"], self.geo_json["coord"]["lat"]
url = URL + f"onecall?lat={lat}&lon={lon}&appid={API_KEY}&units=metric&exclude=minutely,alerts"
api_data = requests.get(url).text
return json.loads(api_data)
except:
print("error")
return
def get_weather(self):
temp = round(self.json["current"]["temp"])
feels_like = round(self.json["current"]["feels_like"])
pressure = self.json["current"]["pressure"]
wind = round(self.json["current"]["wind_speed"],1)
humidity = self.json["current"]["humidity"]
visibility = round((self.json["current"]["visibility"])/1000, 1)
uv = round(self.json["current"]["uvi"], 1)
min_temp = round(self.json["daily"][0]["temp"]["min"])
max_temp = round(self.json["daily"][0]["temp"]["max"])
ri_dt = self.json["current"]["sunrise"]
sunrise = datetime.fromtimestamp(ri_dt, tz=self.time_zone)
st_dt = self.json["current"]["sunset"]
sunset = datetime.fromtimestamp(st_dt, tz=self.time_zone)
weather = {
"temp": f"{temp}°C",
"feels like": f"{feels_like}°C",
"description": self.json["current"]["weather"][0]["description"],
"pressure": f"{pressure}hpa",
"wind speed": f"{wind}m/s",
"humidity": f"{humidity}%",
"visibility": f"{visibility}km",
"uv": f"{uv}",
"min": f"{min_temp}°",
"max": f"{max_temp}°",
"sunrise":sunrise.strftime("%-I:%M %p"),
"sunset": sunset.strftime("%-I:%M %p")
}
return weather
def get_weather_icon(self):
icon = self.json["current"]["weather"][0]["icon"]
return icon
def get_time_zone(self):
time_zone = pytz.timezone(self.json["timezone"])
return time_zone
def get_date_time(self):
dt = datetime.now(tz=self.time_zone)
return dt.strftime("%a %-d %b %-I:%M %p")
def get_location(self):
country = self.geo_json["sys"]["country"]
return f"{self.city},{country}"
def scrap_temp(self, temp_type):
# helper funtion
data = []
for i in range(1, 7):
temp_data = round(self.json["daily"][i]["temp"][temp_type])
data.append(f'{temp_data}°C')
return data
def get_day_forcast(self):
# daily forcast data
forcast = {"day": [], "temp": [], "icon": []}
for i in range(1, 7):
day = self.json["daily"][i]["dt"]
dt = datetime.fromtimestamp(day)
forcast["day"].append(dt.strftime("%a"))
icon_id = self.json["daily"][i]["weather"][0]["icon"]
forcast["icon"].append(icon_id)
forcast["temp"] = self.scrap_temp("day")
return forcast
def get_forcast(self):
# hourly forecast
forcast = {"time": [], "temp": [], "icon": []}
for i in range(1, 7):
day = self.json["hourly"][i]["dt"]
dt = datetime.fromtimestamp(day)
forcast["time"].append(dt.strftime("%-I:%M %p"))
temp_data = round(self.json["hourly"][i]["temp"])
forcast["temp"].append(f'{temp_data}°C')
icon_id = self.json["hourly"][i]["weather"][0]["icon"]
forcast["icon"].append(icon_id)
return forcast
if __name__ == "__main__":
wea = GetWeather("london")
#print(wea.geocode.address)
print(json.dumps(wea.json, indent=4))
if wea.json:
print(wea.weather)
print(wea.icon_name)
print(wea.date_time)
print(wea.location)
print(wea.day_forcast)
print(wea.forcast)