generated from mwc/lab_weather
44 lines
1.6 KiB
Python
44 lines
1.6 KiB
Python
from argparse import ArgumentParser
|
|
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.
|
|
"""
|
|
print("Not finished...")
|
|
|
|
if location:
|
|
coordinates = geocode_location(location)
|
|
if not coordinates:
|
|
print("Error: Location not found. ")
|
|
return
|
|
else:
|
|
coordinates = estimate_location()
|
|
if not coordinates:
|
|
print("Error: Unable to estimate location.")
|
|
return
|
|
office_info = get_weather_office(coordinates['lat'], coordinates['lng'])
|
|
if not office_info:
|
|
print("Error: Weather office not found for this location.")
|
|
return
|
|
forecast = get_forecast(office_info['office'], office_info['x'], office_info['y'], metric=metric)
|
|
if not forecast:
|
|
print("Error: Forecast data is unavailable.")
|
|
return
|
|
temp_unit = "°C" if metric else "°F"
|
|
print(f"Weather Forecast for {location or 'your location'}:")
|
|
for period in forecast:
|
|
|
|
temp = period['temperature']
|
|
description = period['description']
|
|
wind = period['wind_speed']
|
|
wind_dir = period['wind_direction']
|
|
print(f"{period['name']}: {temp}{temp_unit}, {description}")
|
|
if verbose:
|
|
print(f" Wind: {wind} {wind_dir}") |