lab_weather/weather/weather.py

63 lines
2.0 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.
"""
try:
if not location:
location = estimate_location()
print(f"Estimated location: {location}")
if location:
loc_data = geocode_location(location)
if loc_data:
lat, lng = loc_data['lat'], loc_data['lng']
weather_office = get_weather_office(lat, lng)
if weather_office:
forecast = get_forecast(weather_office, lat, lng)
print(forecast)
if forecast:
# print_forecast(forecast, metric, verbose)
forecast=get_forecast(forecast, metric, verbose)
print(forecast)
else:
print("No forecast available for the location.")
else:
print("Weather office not found for the given location.")
else:
print("Location not found.")
else:
print("No location provided, and estimated location failed.")
except Exception as e:
print(f"An error occurred: {e}")
# if location == None:
# if metric == False:
# return get_weather_office(estimate_location(ip_address=None))
# print("Not finished...") # YOUR CODE HERE!
# weather_cli(
# get_forecast(
# get_weather_office(