-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
64 lines (49 loc) · 1.95 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
import requests
import json
import pickle
import os
class Weather:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = "https://api.openweathermap.org/data/2.5/"
self.saved_locations_file = "saved_locations.pkl"
def get_weather(self, location):
url = f"{self.base_url}weather?q={location}&appid={self.api_key}&units=metric"
response = requests.get(url)
data = response.json()
weather_data = {
"location": f"{data['name']}, {data['sys']['country']}",
"temp": data["main"]["temp"],
"description": data["weather"][0]["description"],
"humidity": data["main"]["humidity"],
"wind_speed": data["wind"]["speed"],
"lat": data["coord"]["lat"],
"lon": data["coord"]["lon"],
}
return weather_data
def get_extended_forecast(self, location):
# Get the latitude and longitude of the location first
current_weather_data = self.get_weather(location)
lat = current_weather_data["lat"]
lon = current_weather_data["lon"]
url = f"{self.base_url}onecall?lat={lat}&lon={lon}&exclude=current,minutely,hourly,alerts&appid={self.api_key}&units=metric"
response = requests.get(url)
data = response.json()
forecast_data = data["daily"]
return forecast_data
def convert_temperature(self, temp, unit):
if unit == "F":
return (temp * 9/5) + 32
elif unit == "K":
return temp + 273.15
else:
return temp
def save_location(self, location):
if os.path.exists(self.saved_locations_file):
with open(self.saved_locations_file, "rb") as f:
saved_locations = pickle.load(f)
else:
saved_locations = {}
saved_locations[location] = True
with open(self.saved_locations_file, "wb") as f:
pickle.dump(saved_locations, f)