generated from mwc/lab_weather
48 lines
1.3 KiB
Python
48 lines
1.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 verbose:
|
|
print(location)
|
|
if location is None:
|
|
mylocation=estimate_location()
|
|
else:
|
|
mylocation=geocode_location(location)
|
|
if mylocation is None:
|
|
print("Your City Cannot Be Found")
|
|
return
|
|
if verbose:
|
|
print(mylocation)
|
|
myoffice=get_weather_office(mylocation['lat'], mylocation['lng'])
|
|
if myoffice is None:
|
|
print("There is no weather station")
|
|
return
|
|
if verbose:
|
|
print(myoffice)
|
|
myforcast=get_forecast(myoffice["office"],myoffice["x"],myoffice["y"], metric)
|
|
if verbose:
|
|
print(myforcast)
|
|
else:
|
|
print(myforcast[0])
|
|
|
|
|