-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
30 lines (22 loc) · 772 Bytes
/
Copy pathweather.py
File metadata and controls
30 lines (22 loc) · 772 Bytes
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
from dotenv import load_dotenv
from pprint import pprint
from requests.exceptions import RequestException
import requests
import os
load_dotenv()
def get_current_weather(city="Trivandrum"):
requests_url = f'https://api.openweathermap.org/data/2.5/weather?appid={os.getenv("API_KEY")}&q={city}&units=metric'
try:
weather_data = requests.get(requests_url).json()
return weather_data
except RequestException:
print("Network error")
return {"cod": 500}
if __name__ == "__main__":
print('\n** Get Weather Conditions **')
city = input("\nPlease enter a city name:")
if not bool(city.strip()):
city = "Trivandrum"
weather_data = get_current_weather(city)
print('\n')
pprint(weather_data)