-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_report_function.py
More file actions
40 lines (30 loc) · 1.29 KB
/
Copy pathweather_report_function.py
File metadata and controls
40 lines (30 loc) · 1.29 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
32
33
34
35
36
37
38
39
40
import requests
def weather_report(c):
try:
w_api_key = "YOUR_OPENWEATHER_API_KEY"
response = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={c},BD&units=metric&appid={w_api_key}")
response.raise_for_status()
content = response.json()
temp = content['main']['temp']
feels_like = content['main']['feels_like']
humidity = content['main']['humidity']
w_rep = content['weather'][0]
print(f"""
Weather Report of {c} in Bangladesh:
====================================================================
Description : {w_rep['description']}
Temperature : {temp}°C
Feels like : {feels_like}°C
Humidity : {humidity}%
====================================================================== """)
return True
except requests.HTTPError as e:
if e.response.status_code == 404:
print("Please Enter a Valid City!")
return False
else:
print(f'HTTP Error: {e.response.status_code}')
return False
except requests.RequestException:
print("Network error. Please try again.")
return False