mg643 commited on
Commit
b269877
·
verified ·
1 Parent(s): d15abd8

Create helper.py

Browse files
Files changed (1) hide show
  1. src/helper.py +43 -0
src/helper.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import os
3
+ from dotenv import load_dotenv
4
+
5
+ # Load your API key from .env file
6
+ load_dotenv()
7
+ API_KEY = os.getenv("OPENWEATHER_API_KEY")
8
+
9
+ def get_weather(city):
10
+ """
11
+ Fetch weather for the given city and print it nicely.
12
+ """
13
+ # 1. Create the API endpoint URL
14
+ url = "https://api.openweathermap.org/data/2.5/weather"
15
+
16
+ # 2. Set query parameters
17
+ params = {
18
+ "q": city,
19
+ "appid": API_KEY,
20
+ "units": "metric" # temperature in Celsius
21
+ }
22
+
23
+ # 3. Make the request
24
+ response = requests.get(url, params=params)
25
+
26
+ # 4. Parse JSON
27
+ data = response.json()
28
+
29
+ #print(data)
30
+
31
+ # 5. Extract key info
32
+ city_name = data["name"]
33
+ temp = data["main"]["temp"]
34
+ humidity = data["main"]["humidity"]
35
+ description = data["weather"][0]["description"]
36
+
37
+ # 6. Print
38
+ return f"In {city_name}, it is {temp}°C with humidity {humidity} and {description}."
39
+
40
+ if __name__ == "__main__":
41
+ city = input("Please enter the city for which you would like to fetch the weather : ")
42
+ get_weather(city)
43
+