-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
63 lines (54 loc) · 1.8 KB
/
weather.py
File metadata and controls
63 lines (54 loc) · 1.8 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import sys
import requests
from dotenv import load_dotenv
load_dotenv('./.env')
import datetime
api_key = os.environ['OPENWEATHER_API_KEY']
def get_weather(city):
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
weather = {
'city': data['name'],
'description': data['weather'][0]['description'],
'temperature': data['main']['temp'],
'humidity': data['main']['humidity'],
'wind_speed': data['wind']['speed'],
}
return weather
else:
return None
def get_city():
today = datetime.date.today()
city = input('Enter city name: ')
weather = get_weather(city)
if weather:
print(f"\nAs of: ",today)
print(f"Current weather in {weather['city']}:")
print(f"Description: {weather['description']}")
print(f"Temperature: {weather['temperature']} °C")
print(f"Humidity: {weather['humidity']}%")
print(f"Wind speed: {weather['wind_speed']} m/s")
else:
print(f"Could not retrieve weather information for {city}")
def main():
os.system('cls' if os.name == 'nt' else 'clear')
app_name = 'Weather'
print(f'{"-" * 48}')
print(f'{" " * 12}{app_name}{" " * 12}')
print(f'{"-" * 48}')
get_city()
while True:
response = input('\nCheck another city? (Y/N): ')
if response == 'y' or response == 'Y':
main()
elif response == 'n' or response == 'N':
print('\nThank you and have a great day.\n')
sys.exit()
else:
print('\nError: Please select y or n.\n')
continue
if __name__ == '__main__':
main()