forked from CloudLabsAI-Azure/Code-Generation-Refactoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_script.py
More file actions
31 lines (28 loc) · 1.07 KB
/
weather_script.py
File metadata and controls
31 lines (28 loc) · 1.07 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
import requests
class WeatherFetcher:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = "https://api.openweathermap.org/data/2.5/weather"
def get_weather(self, city):
params = {
"q": city,
"appid": self.api_key,
"units": "metric" # Use metric units for temperature in Celsius
}
response = requests.get(self.base_url, params=params)
if response.status_code == 200:
return response.json()
else:
response.raise_for_status()
# Example usage:
if __name__ == "__main__":
api_key = "d9660777d8703f83462782ab4f0f72d1"
weather_fetcher = WeatherFetcher(api_key)
city = input("Enter the city name: ")
try:
weather_data = weather_fetcher.get_weather(city)
print(f"Weather in {city}:")
print(f"Temperature: {weather_data['main']['temp']}°C")
print(f"Description: {weather_data['weather'][0]['description']}")
except requests.exceptions.RequestException as e:
print(f"Error fetching weather data: {e}")