-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
50 lines (40 loc) · 1.51 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
# To test the code, open a terminal and run:
# python weather.py
import requests
API_ROOT = 'https://www.metaweather.com'
API_LOCATION = '/api/location/search/?query='
API_WEATHER = '/api/location/'
def fetch_location(query):
return requests.get(API_ROOT + API_LOCATION + query).json()
def fetch_weather(woeid):
return requests.get(API_ROOT + API_WEATHER + str(woeid)).json()
def display_weather(weather):
print(f"Weather for {weather['title']}:")
for entry in weather['consolidated_weather']:
date = entry['applicable_date']
high = entry['max_temp']
low = entry['min_temp']
state = entry['weather_state_name']
print(f"For {date} the weather seemes{state} the high will be{high:2.1f}°C and the will be low {low:2.1f}°C")
def disambiguate_locations(locations):
print("Ambiguous location! Did you mean:")
for loc in locations:
print(f"\t* {loc['title']}")
def weather_dialog():
try:
where = ''
while not where:
where = input("Where in the world are you? ")
locations = fetch_location(where)
if len(locations) == 0:
print("I don't know where that is.")
elif len(locations) > 1:
disambiguate_locations(locations)
else:
woeid = locations[0]['woeid']
display_weather(fetch_weather(woeid))
except:
print("there is somthing wrong with the network, try again")
if __name__ == '__main__':
while True:
weather_dialog()