generated from mwc/lab_weather
60 lines
2.3 KiB
Python
60 lines
2.3 KiB
Python
# weather.py
|
|
# ------------
|
|
# By MWC Contributors
|
|
#
|
|
# Defines `print_weather`, which does all the work of fetching
|
|
# the requested weather data and printing it out to the screen
|
|
# in a sensible way.
|
|
#
|
|
# It's your job to implement this function.
|
|
|
|
from weather.weather_apis import (
|
|
geocode_location,
|
|
estimate_location,
|
|
get_weather_office,
|
|
get_forecast
|
|
)
|
|
|
|
def print_weather(location=None, metric=False, verbose=False):
|
|
"""Prints out a weather report using the provided location, or using
|
|
the user's current location if no location was provided.
|
|
When metric is True, prints out the weather in metric units.
|
|
When verbose is True, prints out a more detailed report.
|
|
"""
|
|
if location == None:
|
|
coordinates = estimate_location()
|
|
else:
|
|
coordinates = geocode_location(location)
|
|
#print(coordinates) #test/debug only
|
|
if coordinates == None:
|
|
print("Location Unknown, Try Again")
|
|
else:
|
|
|
|
|
|
|
|
|
|
|
|
office_info = get_weather_office(coordinates["lat"], coordinates["lng"])
|
|
#print(office_info) #test/debug only
|
|
|
|
if office_info == None:
|
|
print("Loooks like that location is not in the US. Try again")
|
|
pass
|
|
else:
|
|
office_only = office_info["office"]
|
|
|
|
all_forecasts = get_forecast(office_info["office"], office_info["x"], office_info["y"], metric)
|
|
if all_forecasts == None:
|
|
print("Unable to provide forecast for that location. Try another location.")
|
|
pass
|
|
else:
|
|
print("The forecast for", office_only, "is")
|
|
if verbose==False:
|
|
simple_forecast = all_forecasts[0]
|
|
print(simple_forecast["name"],"temperature of", simple_forecast["temperature"], "wind speed of", simple_forecast["wind_speed"], "out of the ", simple_forecast["wind_direction"],"conditions of", simple_forecast["description"])
|
|
print()
|
|
else:
|
|
for forecast in all_forecasts:
|
|
print(forecast["name"],"temperature of", forecast["temperature"], "wind speed of", forecast["wind_speed"], "out of the ", forecast["wind_direction"],"conditions of", forecast["description"])
|
|
print()
|