-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_info.py
74 lines (57 loc) · 2.49 KB
/
weather_info.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
#!/usr/bin/env python3
import requests
# Public APIs can are available in:
# https://github.com/public-apis/public-apis#weather
# MetaWeather provides an API that delivers JSON over HTTPS for
# access to their data.
API_URL = 'https://www.metaweather.com'
# Location search URL. Argument should be a string. This provides a WOEID which
# we will use later to get the weather.
API_LOCATION = '/api/location/search/?query='
# After taking the WOEID from the location query, we will use that to get the
# actual weather for that location.
API_WEATHER = '/api/location/' # + WOEID
# Block where program will send requests to the web API.
def location(input):
return requests.get(API_URL + API_LOCATION + input).json()
def weather(woeid):
return requests.get(API_URL + API_WEATHER + str(woeid)).json()
# Block of code which I intergrated from my Udacity course.
# This helps eliminate ambiguity in user input.
def disambiguate_locations(locations):
print("Ambiguous location! Did you mean:")
for loc in locations:
print(f"\t* {loc['title']}")
# This is the terminal output which prints out the weather for the user to see.
def forecast_printout(weather):
print(f"The weather for {weather['title']}:")
for entries in weather['consolidated_weather']:
date = entries['applicable_date']
high = entries['max_temp']
low = entries['min_temp']
state = entries['weather_state_name']
print(f"On {date},it will have {state}, with highs of {high}°C and"
f"lows of {low}°C.")
# Program asks user for which place they would like to know the weather of.
def get_forecast():
try:
# I integrated this block of code from my Udacity
# Python course which improves error handling in the
# weather program. This elimiates ambiguity in user input.
which_place = ''
while not which_place:
which_place = input('Which place would you like to '
'know the weather of? \n')
this_place = location(which_place)
if len(this_place) == 0:
print('I don\'t think that\'s a real location.')
elif len(this_place) > 1:
disambiguate_locations(this_place)
else:
woeid = this_place[0]['woeid']
forecast_printout(weather(woeid))
except requests.exceptions.ConnectionError:
print("Please check your connection status. Cannot connect to server.")
if __name__ == '__main__':
while True:
get_forecast()