Files
lab_weather/weather/weather.py

60 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.
"""
# Determine coordinates from the provided location or estimate from IP
coords = geocode_location(location) if location else estimate_location()
if not coords:
print("Error: could not determine location.")
return None
office = get_weather_office(coords["lat"], coords["lng"])
if not office:
print("Error: no weather office found for location.")
return None
forecast = get_forecast(office["office"], office["x"], office["y"], metric=metric)
if not forecast:
print("Error: could not fetch forecast data.")
return None
def icon_for(desc: str) -> str:
d = (desc or "").lower()
if "thunder" in d:
return "Today will thunder ⚡"
if "rain" in d or "shower" in d:
return "Today will have rain showers ☔"
if "snow" in d or "sleet" in d:
return "Today will snow ❄"
if "fog" in d or "haze" in d:
return "Today will have fog, limiting your vision 🌫️"
if "sun" in d or "clear" in d:
return "Today will be Sunny, enjoy the outdoors! ☀️"
if "cloud" in d or "overcast" in d:
return "Today will be Cloudy ☁️"
return ""
for day in forecast:
symbol = icon_for(day["description"])
print(symbol)
display = day["name"]