-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
93 lines (75 loc) · 2.45 KB
/
weather.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# imports
import sys
import time
import requests
from urllib import response
# API setup
BASE_URL = "http://api.openweathermap.org/data/2.5/weather/"
with open('key.txt', 'r') as file:
KEY = file.read()
# globals
ERR = '\nOops! bad gateway, an error occured.'
INV = '\nInvalid command!'
# methods
def display(city, data):
city = city.capitalize()
lon = data['coord']['lon']
lat = data['coord']['lat']
loc = f"\nYour location is ({lon}, {lat})"
des = data['weather'][0]['description']
tem = round(data['main']['temp'] - 273.15, 2)
max = round(data['main']['temp_max'] - 273.15, 2)
min = round(data['main']['temp_min'] - 273.15, 2)
wea = f"\nIt's {tem}°C today in {city} with {des}.\nMax temp will be around {max} and min around {min}"
pre = round(data['main']['pressure'] / 1013.25, 2)
hum = data['main']['humidity']
vis = data['visibility'] / 1000
win = data['wind']['speed']
atm = f"\nThe pressure today is {pre} Atm. Humidity at {hum}%.\nVisibility at {vis} meters. Wind speed at {win} m/s."
ris = data['sys']['sunrise']
set = data['sys']['sunset']
tim = f'\nSunrise at {time.strftime("%H:%M", time.localtime(ris))} AM IST\nSunset at {time.strftime("%H:%M", time.localtime(set))} PM IST'
def key():
key_ = input('\n> ')
match key_:
case '1':
print(loc)
case '2':
print(wea)
case '3':
print(atm)
case '4':
print(tim)
case '0':
sys.exit()
case _:
print(INV)
key()
key()
def help(city, data):
press = '\nPress:'
loc = '1 for location' # co-ord
wea = '2 for weather' # desc / temp / max / min
atm = '3 for atmospheric details' # pressure / humidity / visibility / wind
tim = '4 for timings' # sunrise / sunset / timezone
end = '0 to exit' # exit
print(press)
print(loc)
print(wea)
print(atm)
print(tim)
print(end)
display(city, data)
# main exe
def main():
city = input("\nEnter your city: ")
req_url = f'{BASE_URL}?q={city}&appid={KEY}'
fetch = requests.get(req_url)
if (fetch.status_code == 200):
data = fetch.json()
# print(data)
help(city, data)
else:
print(ERR)
# test
main()