generated from mwc/lab_weather
74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
ffrom weather.weather_apis import (
|
|
geocode_location,
|
|
estimate_location,
|
|
get_weather_office,
|
|
get_forecast
|
|
)
|
|
|
|
# -----------------------------------------------------------
|
|
# Choose weather emoji based on the description
|
|
# -----------------------------------------------------------
|
|
def choose_emoji(description):
|
|
desc = description.lower()
|
|
|
|
if "snow" in desc:
|
|
return "❄️"
|
|
if "rain" in desc or "shower" in desc:
|
|
return "🌧️"
|
|
if "thunder" in desc or "storm" in desc:
|
|
return "⛈️"
|
|
if "cloud" in desc:
|
|
return "☁️"
|
|
if "sunny" in desc or "clear" in desc:
|
|
return "☀️"
|
|
if "wind" in desc or "breeze" in desc:
|
|
return "💨"
|
|
if "fog" in desc or "mist" in desc:
|
|
return "🌫️"
|
|
|
|
return "🌡️" # default
|
|
|
|
# -----------------------------------------------------------
|
|
# Main weather print function
|
|
# -----------------------------------------------------------
|
|
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.
|
|
"""
|
|
|
|
# Get coordinates
|
|
if location:
|
|
coordinates = geocode_location(location)
|
|
else:
|
|
coordinates = estimate_location()
|
|
|
|
# Get NOAA office + forecast
|
|
office = get_weather_office(coordinates["lat"], coordinates["lng"])
|
|
forecast = get_forecast(office["office"], office["x"], office["y"], metric=metric)
|
|
|
|
# Pretty output
|
|
print("\n====== Weather Forecast ======")
|
|
print(f"Location: {location if location else 'Your Current Location'}")
|
|
print(f"Office: {office['office']}")
|
|
print("=================================\n")
|
|
|
|
if not forecast:
|
|
print("No forecast available.")
|
|
return
|
|
|
|
# Print only TODAY
|
|
today = forecast[0]
|
|
emoji = choose_emoji(today["description"])
|
|
|
|
print(f"{emoji} --- {today['name']} --- {emoji}")
|
|
print(f"{emoji} {today['description']}")
|
|
print(f"🌡️ Temperature: {today['temperature']}°{'C' if metric else 'F'}")
|
|
print(f"💨 Wind: {today['wind_speed']} from {today['wind_direction']}")
|
|
|
|
if verbose:
|
|
print("\nAdditional Details:")
|
|
for key, value in today.items():
|
|
if key not in ("name", "description", "temperature", "wind_speed", "wind_direction"):
|
|
print(f" - {key}: {value}") |