Skip to content

Commit

Permalink
added windspeed
Browse files Browse the repository at this point in the history
  • Loading branch information
suvanbanerjee committed Mar 29, 2024
1 parent 9adee51 commit 4506ad1
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions weatherkit/current_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def __init__(self, lat,lon=None):
def temperature(self,units='metric'):
'''
This method is used to get the current temperature of the city.
Parameters:
units: str
The unit of the temperature. Default is 'metric'.
'metric' for Celsius and 'imperical' for Fahrenheit.
Returns:
float
The current temperature of the city.
'''
if self.lat and self.lon:
url = "https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&current=temperature_2m".format(self.lat, self.lon)
Expand All @@ -76,6 +85,10 @@ def temperature(self,units='metric'):
def humidity(self):
'''
This method is used to get the current humidity of the city.
Returns:
float
The current humidity of the city.
'''
if self.lat and self.lon:
url = "https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&current=relative_humidity_2m".format(self.lat, self.lon)
Expand All @@ -89,6 +102,14 @@ def humidity(self):
def precipitation(self,units='metric'):
'''
This method is used to get the current precipitation of the city.
Parameters:
units: str
The unit of the temperature. Default is 'metric'.
'metric' for Celsius and 'imperical' for Fahrenheit.
Returns:
float
The current precipitation of the city.
'''
if self.lat and self.lon:
url = "https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&current=precipitation".format(self.lat, self.lon)
Expand All @@ -104,6 +125,9 @@ def precipitation(self,units='metric'):
def weather_code(self):
'''
This method is used to get the current weather code of the city.
Returns:
int
The current weather code of the city.
'''
if self.lat and self.lon:
url = "https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&current=weather_code".format(self.lat, self.lon)
Expand All @@ -112,5 +136,28 @@ def weather_code(self):
return data["current"]["weather_code"]
except Exception as e:
return "Error fetching weather data:", e
else:
return "Invalid Latitude or Longitude"
def wind_speed(self,units='metric'):
'''
This method is used to get the current wind speed of the city.
Parameters:
units: str
The unit of the temperature. Default is 'metric'.
'metric' for Celsius and 'imperical' for Fahrenheit.
Returns:
tuple(float windspeed,float direction in deg)
The current wind speed and wind direction of the city.
'''
if self.lat and self.lon:
url = "https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&current=wind_speed_10m,wind_direction_10m".format(self.lat, self.lon)
if units == 'imperical':
url = url + "&wind_speed_unit=mph"
try:
data = requests.get(url).json()
return data["current"]["wind_speed_10m"], data["current"]["wind_direction_10m"]
except Exception as e:
return "Error fetching weather data:", e
else:
return "Invalid Latitude or Longitude"

0 comments on commit 4506ad1

Please sign in to comment.